Data Lab · Unit 6 of 8
Unit 6: SQL basics
Databases and tables, SELECT and FROM, filtering with WHERE, and sorting with ORDER BY and LIMIT, using a real database in your browser.
Opens the first lesson of this unit in Data Lab. Every lesson is open from the start.
Lesson 1: Databases, tables and your first query
Goal: Explain what a database is and run a SELECT query.
Spreadsheets are great up to a point. Companies with millions of orders keep data in databases, and talk to them with SQL (Structured Query Language, often pronounced "sequel"). SQL is one of the most requested skills in data job postings, and the basics take an afternoon to learn.
A relational database is a set of tables, much like spreadsheet tabs, where every row has the same columns. Our practice database is for an invented shop and has three tables:
| Table | Columns | One row is |
|---|---|---|
customers | id, name, city, joined | one customer |
products | id, name, category, price | one product |
orders | id, customer_id, product_id, qty, order_date | one order |
Your first query asks for data with SELECT (which columns) and FROM (which table):
SELECT name, price
FROM products;
SELECT * means every column. SQL keywords are not case-sensitive, but writing them in capitals makes queries easier to read. The semicolon marks the end of a query.
Hands-on lab in the room: SQL lab. This is a real SQL database running in your browser. Type a query and press Run.
- Database
- An organized store of data, usually made of tables, built to hold a lot of data safely and answer questions fast.
- SQL
- Structured Query Language: the standard language for asking questions of relational databases.
- Query
- A request for data written in SQL.
Remember: SELECT the columns, FROM the table. SELECT * means all columns.
Lesson 2: Filtering rows with WHERE
Goal: Filter with comparisons, AND, OR and text matching.
WHERE keeps only the rows that meet a condition. It goes after FROM.
SELECT name, price
FROM products
WHERE price > 10;
In our shop, that returns 4 products: Backpack (35.00), Water bottle (12.00), Lunch box (15.00) and Desk lamp (28.00).
| Operator | Meaning | Example |
|---|---|---|
= | equals | city = 'Riverton' |
<> or != | not equal | category <> 'Tech' |
> < >= <= | comparisons | qty >= 3 |
AND | both must be true | price > 5 AND category = 'Tech' |
OR | either can be true | city = 'Riverton' OR city = 'Lakeside' |
LIKE | pattern match; % means any characters | name LIKE '%pen%' |
BETWEEN | within a range, ends included | price BETWEEN 5 AND 15 |
Text goes in single quotes: 'Riverton'. Numbers do not. Dates written as '2025-03-01' compare correctly because the year-month-day format sorts in time order.
Hands-on lab in the room: SQL lab. Filter the shop's data.
- WHERE
- The part of a query that keeps only the rows meeting a condition.
- LIKE
- A text pattern match in SQL, where % stands for any run of characters.
Worked example: combining conditions
SELECT name, price
FROM products
WHERE category = 'Kitchen' AND price < 14;Only the Water bottle (12.00) matches. The Lunch box is in Kitchen too, but at 15.00 it fails the price condition, and AND needs both to be true.
Remember: WHERE filters rows. Text in single quotes, numbers without. AND narrows, OR widens.
Lesson 3: Sorting with ORDER BY and LIMIT
Goal: Sort results and return the top or bottom few rows.
ORDER BY sorts the results. Ascending (ASC, smallest first) is the default. Add DESC for largest first. LIMIT keeps just the first few rows of the result.
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;
That returns Backpack (35.00), Desk lamp (28.00) and Lunch box (15.00).
The parts of a query always go in this order, even though the database thinks about them a little differently:
SELECT columns
FROM table
WHERE condition
ORDER BY column [ASC|DESC]
LIMIT number;
You can sort by more than one column: ORDER BY city, name sorts by city, and alphabetically by name within each city. Without ORDER BY, a database is free to return rows in any order, so always sort when the order matters.
Hands-on lab in the room: SQL lab. Sort and limit the results.
- ORDER BY
- Sorts query results by one or more columns, ascending by default.
- LIMIT
- Returns only the first N rows of the result.
Worked example: sorting by date
SELECT name, joined
FROM customers
ORDER BY joined
LIMIT 2;The two earliest customers: Ava Brooks (2024-02-11) and Ben Okafor (2024-03-02). Dates stored as YYYY-MM-DD sort correctly in time order.
Remember: ORDER BY ... DESC LIMIT 5 is how you get a "top 5". Clause order: SELECT, FROM, WHERE, ORDER BY, LIMIT.
Lesson 4: Reading a query like a database
Goal: Predict the result of a query before running it.
You write SELECT first, but it helps to read a query in the order the database works through it:
- FROM: start with the table.
- WHERE: throw away rows that fail the condition.
- SELECT: keep only the listed columns (and calculate anything new).
- ORDER BY: sort what is left.
- LIMIT: cut it down to the first few rows.
Worked example: predict before you run
- FROM customers: all 10 customers.
- WHERE city = 'Riverton': Ava Brooks, Chloe Tran, Farah Haddad and Isaac Kim remain.
- SELECT name: keep just the name column.
- ORDER BY name: Ava Brooks, Chloe Tran, Farah Haddad, Isaac Kim.
SELECT name
FROM customers
WHERE city = 'Riverton'
ORDER BY name;Predicting the result first, then running it, is the fastest way to catch your own mistakes.
You can also calculate new columns: SELECT name, price * 1.08 AS price_with_tax FROM products; The AS keyword gives the calculated column a readable name, called an alias.
- Alias (AS)
- A temporary name for a column or table in a query, such as price * 1.08 AS price_with_tax.
Remember: Read queries as FROM → WHERE → SELECT → ORDER BY → LIMIT. Predict, then run.
Practise it in Data Lab
Each lesson ends with two or three questions that check you can use the idea, not just recognise it. Wrong answers explain why and come back in Review. The unit ends with a 6-question quiz; pass it with 80% or more to master the unit. Hands-on lab: SQL lab.
Every lesson is free: no ads, no lives and no paywall.