Module 0 · Orientation

How to read a SQL query

By the end you will be able to explain what an already written query asks for.

About 10 min

0 of 52 lessons

Before you start, you need

Understand tables, rows and columns

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.

Minimal syntax
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.

SQL
SELECT name, price
FROM products
WHERE stock > 0
ORDER BY price;
Example result: 7 rows
nameprice
Cable USB-C12
Mouse24.9
Keyboard49.5
Headphones69.9
SSD Drive89
Monitor 24179
Monitor 27249
  1. FROM products supplies the catalogue. WHERE stock > 0 keeps the items in stock.
  2. SELECT name, price shows which product it is and what it costs. ORDER BY price shows the lowest prices first.
  3. Pro Laptop does not appear because its stock is 0. It is still stored: the filter only decides what we see.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Complete and explain

Not started

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.

Key takeaways

  • FROM finds the source.
  • WHERE decides which rows get through.
  • SELECT chooses the visible columns.
  • ORDER BY sorts the output.

Next: Select data with SELECT and FROM