Your first query
SQL · Unit 1: First queries
- Free
- SQL
- 10 exercises
Who are this store's customers? The answer is stored in a table named customers, and this line asks for it. Each row is a person and each column is one fact about them.
See the NULL in Chloë's city? It's not a city: it's how the table marks a value it doesn't have.
SELECT * FROM customers;Returns
| id | name | city | age |
|---|---|---|---|
| 1 | Ann | Denver | 34 |
| 2 | Luke | Austin | 27 |
| 3 | Chloë | NULL | 41 |
| 4 | Mason | Tampa | 19 |
| 5 | Valerie | Denver | 52 |
| 6 | Dylan | Boston | 27 |
| 7 | Hannah | Austin | NULL |
| 8 | Brian | Denver | 38 |
That line is a query, and it reads almost like a sentence. SELECT says what you want to see, and the * means every column.
FROM says which table. And what does the store sell? Same query, different table.
SELECT * FROM products;Returns
| id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Keyboard | Accessories | 45.5 | 12 |
| 2 | Mouse | Accessories | 19.9 | 30 |
| 3 | Monitor | Displays | 189 | 5 |
| 4 | Cable | Accessories | 5.25 | 0 |
| 5 | Laptop | Devices | 899 | 3 |
| 6 | Tablet | Devices | 320 | 7 |
| 7 | Headphones | Audio | 59.9 | 15 |
| 8 | Speaker | Audio | 74 | 0 |
SQL doesn't care about line breaks, which is why it needs the ; to know where a query ends.
You do care. With SELECT on one line and FROM on another it's easier to read, and when something breaks you'll know which line to check.
SELECT *
FROM customers;Returns
| id | name | city | age |
|---|---|---|---|
| 1 | Ann | Denver | 34 |
| 2 | Luke | Austin | 27 |
| 3 | Chloë | NULL | 41 |
| 4 | Mason | Tampa | 19 |
| 5 | Valerie | Denver | 52 |
| 6 | Dylan | Boston | 27 |
| 7 | Hannah | Austin | NULL |
| 8 | Brian | Denver | 38 |
When a query is written wrong, SQL stops and tells you the word where it got lost.
That word is almost never the mistake: it's the first one that no longer fit. Your mistake is usually right before it, so read the message and then look back.
SQL is about fifty years old, and almost every database speaks it: MySQL, PostgreSQL, SQLite, and many more.
Here your queries run on SQLite, inside your phone. Each database has its quirks, and we'll tell you when something changes from one to another.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Multiple choice
In the
customerstable, what is a row?2. Multiple choice
Tomorrow a new customer, Emily, comes in and the store adds her. What does the
customerstable gain?3. Complete the code
Someone new at the store wants to see everything it knows about each customer. Fill in the blank.
SELECT ___ FROM customers;4. Complete the code
The store also records every purchase, in the
orderstable. Fill in the blank to see it.SELECT * ___ orders;5. Put the lines in order
What does the store sell? Put in order the query that answers it. One line doesn't belong.
6. Multiple choice
Which query is written correctly?
7. Find the bug
Here are two queries, one to see who buys and one to see what's for sale. SQL complains about the second SELECT. Which line is the real error on?
SELECT * FROM customers SELECT * FROM products;8. Put the lines in order
Put the two queries in order to see first who buys and then what each person has ordered. One line doesn't belong.
9. Multiple choice
You're learning SQL here, with SQLite. Tomorrow a job has you using PostgreSQL. Will what you know still help?
10. Write the code
The store wants to go over everything it sells. Write the query that shows the whole
productstable, with all its columns.SELECT
It opens in your browser. It is free and you do not need an account to start.