Before you start, you need
Read a query as a question
A query can be read as a complete question: «Show the name and price of the available products, starting with the cheapest». Each part of the SQL expresses one decision in that question.
Start with FROM: it names the table we are going to read. Then look at WHERE: it sets which rows we want to keep. In the example, stock > 0 means units are available.
SELECT lists the columns that will appear in the answer. ORDER BY price puts the products in order from lowest to highest price. The code is written starting with SELECT, even though, to understand it, it helps to look for the source first.
You do not need to learn how to build filters or sort yet. In the practice tasks those lines are already written: you will fill in the columns and the table. The new tools are taught separately later on.
SELECT name, price
FROM products
WHERE stock > 0
ORDER BY price;See it in action
Which available products cost least? Read this query before completing it.
SELECT name, price
FROM products
WHERE stock > 0
ORDER BY price;| name | price |
|---|---|
| Cable USB-C | 12 |
| Mouse | 24.9 |
| Keyboard | 49.5 |
| Headphones | 69.9 |
| SSD Drive | 89 |
| Monitor 24 | 179 |
| Monitor 27 | 249 |
- FROM products supplies the catalogue. WHERE stock > 0 keeps the items in stock.
- SELECT name, price shows which product it is and what it costs. ORDER BY price shows the lowest prices first.
- Pro Laptop does not appear because its stock is 0. It is still stored: the filter only decides what we see.
- The code is written SELECT, FROM, WHERE and ORDER BY. To understand it, you start from the source of the data.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Fill in the columns and the source
Complete the query you have been given: select name and price from products. Keep WHERE stock > 0 and ORDER BY price. Before running it, explain what each block does.
Expected result: 7 rows with the columns name, price, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
Fill in the two visible attributes and the catalogue that supplies the data; keep the filter and the ordering you were given.
Replace the comments at the start with name, price and products in the right clauses.
Keep WHERE stock > 0 and ORDER BY price; those are the parts you are now learning to read.
Solution explained
SELECT name, price
FROM products
WHERE stock > 0
ORDER BY price;The query is read in blocks: columns, source, filter and ordering. Each clause covers a specific part of the task.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Complete and explain
Complete SELECT and FROM to show name and stock. Keep the filter and ordering already in the draft.
Expected result: 7 rows with the columns name, stock, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
Tell apart what the columns show from what the filter selects.
Fill in only the SELECT list and the name after FROM.
The attributes are name and stock; the source is products.
Solution explained
SELECT name, stock FROM products WHERE stock > 0 ORDER BY stock;The source supplies the products, the filter keeps the available ones, SELECT shows two attributes and the ordering puts the lowest stock first.
Key takeaways
- FROM finds the source.
- WHERE decides which rows get through.
- SELECT chooses the visible columns.
- ORDER BY sorts the output.