Reading a database
SQL · Unit 1: First queries
- Free
- SQL
- 10 exercises
Ann says she bought a Monitor on January 4. Where do you check that? A database keeps each kind of thing in its own table, and this one has a third for purchases.
Look for that date, written 2026-01-04 in the table: there are two rows, and neither says Ann or Monitor. There are only numbers.
SELECT * FROM orders;Returns
| id | customer_id | product_id | quantity | placed |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | 2026-01-03 |
| 2 | 1 | 3 | 1 | 2026-01-04 |
| 3 | 2 | 2 | 5 | 2026-01-04 |
| 4 | 4 | 4 | 10 | 2026-01-09 |
| 5 | 5 | 1 | 1 | 2026-01-10 |
| 6 | 2 | 3 | 2 | 2026-01-11 |
| 7 | 1 | 2 | 1 | 2026-01-12 |
| 8 | 5 | 4 | 3 | 2026-01-12 |
| 9 | 6 | 5 | 1 | 2026-01-14 |
| 10 | 3 | 7 | 2 | 2026-01-15 |
| 11 | 6 | 7 | 1 | 2026-01-15 |
| 12 | 2 | 6 | 1 | 2026-01-20 |
In this database, every table has an id column with a number that never repeats. It's a very common convention, because two customers can both be named Ann, and they'll never have the same id.
This store's Ann is customer 1.
SELECT id, name
FROM customers;Returns
| id | name |
|---|---|
| 1 | Ann |
| 2 | Luke |
| 3 | Chloë |
| 4 | Mason |
| 5 | Valerie |
| 6 | Dylan |
| 7 | Hannah |
| 8 | Brian |
That's why an order doesn't store names: it stores ids. customer_id says who bought and product_id says what.
Ann is customer 1 and the Monitor is product 3. Her January 4 purchase is order 2, and she was telling the truth.
Each column stores a single type of value: number, like price and stock, or text, like name and category.
The type decides what you can do. Numbers are for calculations, and text gets glued with ||. Dates, in SQLite, are stored as text.
SELECT name, price, stock
FROM products;Returns
| name | price | stock |
|---|---|---|
| Keyboard | 45.5 | 12 |
| Mouse | 19.9 | 30 |
| Monitor | 189 | 5 |
| Cable | 5.25 | 0 |
| Laptop | 899 | 3 |
| Tablet | 320 | 7 |
| Headphones | 59.9 | 15 |
| Speaker | 74 | 0 |
You've asked for prices cut in half, stock doubled, and quantities times two. None of that got stored.
SELECT only reads. The result is a new table that's put together on the spot and then disappears. The real one never finds out.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Complete the code
Which days had sales? Fill in the blank to see the date of each purchase.
SELECT placed FROM ___;2. Put the lines in order
Today is inventory day. Put the query in order to see each product's name and how many are left. Two tables don't belong.
3. Find the bug
Planning the shipments takes each person's name and city. SQL says there's no column called city. Which line has the error?
SELECT name, city FROM products;4. Complete the code
Who placed each order? Fill in the blank to see each order's id next to the value that says whose it is.
SELECT id, ___ FROM orders;5. Multiple choice
In
customers, Ann hasid1 and Luke hasid2. Order 12 hascustomer_id2 andquantity1. Whose is it?6. Write the code
What was each purchase for, and how many? Write a query that shows two columns for each order,
product_idand thenquantity.SELECT FROM orders;7. Complete the code
The supplier offers to double what's in the warehouse. Fill in the blank with the column it makes sense to double.
SELECT name, ___ * 2 FROM products;8. Predict the output
What does this query return?
SELECT 7 + 3, '7' || '3';9. Put the lines in order
During two-for-one week, every order is doubled. Put the query in order. It should show each order's id and double its quantity. One line doesn't belong.
10. Multiple choice
The Keyboard costs 45.5. Someone runs this query by mistake. How much does the Keyboard cost in the table afterward?
SELECT name, price * 0 FROM products;
It opens in your browser. It is free and you do not need an account to start.