WK4 Query a database Flashcards

1
Q

Basic SQL query

A

There are two essential keywords in any SQL query: SELECT and FROM. You will use these keywords every time you want to query a SQL database. Using them together helps SQL identify what data you need from a database and the table you are returning it from.

The video demonstrated this SQL query:

SELECT employee_id, device_id

FROM employees;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ORDER BY

A

Database tables are often very complicated, and this is where other SQL keywords come in handy. ORDER BY is an important keyword for organizing the data you extract from a table.

ORDER BY sequences the records returned by a query based on a specified column or columns. This can be in either ascending or descending order.

SELECT customerid, city, country
FROM customers
ORDER BY city;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Sorting in descending order

A

SELECT customerid, city, country
FROM customers
ORDER BY city DESC;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Sorting based on multiple columns

A

You can also choose multiple columns to order by. For example, you might first choose the country and then the city column. SQL then sorts the output by country, and for rows with the same country, it sorts them based on city. You can run this to explore how SQL displays this:

SELECT customerid, city, country
FROM customers
ORDER BY country, city;

Key takeaways

SELECT and FROM are important keywords in SQL queries. You use SELECT to indicate which columns to return and FROM to indicate which table to query. You can also include ORDER BY in your query to organize the output. These foundational SQL skills will support you as you move into more advanced queries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Lab best example:

Task 3. Order login attempts data

A

In this task, you need to use the ORDER BY keyword. You’ll sequence the data that your query returns according to the login date and time.

SELECT *
FROM log_in_attempts
ORDER BY login_date, login_time;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly