SQL · Week 2 of 4 · 5 hours
SELECT, WHERE and ORDER BY
Retrieve exactly the rows and columns you need, in the order you want.
Learning objectives
By the end of this week you will be able to:
- Choose columns with SELECT and rename them with AS.
- Filter rows with WHERE using comparison and logical operators.
- Use IN, BETWEEN, LIKE and IS NULL.
- Sort with ORDER BY and limit results with LIMIT.
Lessons
SELECT Statements
SELECT chooses which columns to show, and FROM says which table they come from.
SELECT * FROM products;shows every column. Handy for a first look, but name the columns you need in real work.SELECT product_name, price FROM products;shows just two columns.SELECT product_name, price * 1.1 AS price_with_tax FROM products;calculates a new column and names it withAS(an alias).SELECT DISTINCT category FROM products;lists each category once, removing repeats.
A SELECT never changes the data in the table. It only shows you a result, so you can experiment freely.
WHERE Clause
WHERE keeps only the rows that pass a test.
| Operator | Meaning | Example |
|---|---|---|
| = , <> | equal, not equal | WHERE category = 'Books' |
| > < >= <= | comparisons | WHERE price >= 50 |
| AND, OR, NOT | combine tests | WHERE category = 'Books' AND price < 20 |
| IN | matches any value in a list | WHERE city IN ('Chicago','Denver') |
| BETWEEN | within a range (inclusive) | WHERE order_date BETWEEN '2026-01-01' AND '2026-03-31' |
| LIKE | pattern match; % means any characters | WHERE name LIKE 'A%' (starts with A) |
| IS NULL | value is missing | WHERE city IS NULL |
Watch out: missing values (NULL) cannot be found with = NULL; you must write IS NULL or IS NOT NULL. And when you mix AND with OR, use parentheses: WHERE (city = 'Chicago' OR city = 'Denver') AND signup_date >= '2026-01-01'.
ORDER BY
ORDER BY sorts the result. ASC (ascending, A to Z, smallest first) is the default; DESC sorts largest first.
SELECT product_name, price FROM products ORDER BY price DESC;
Sort by more than one column by separating them with commas: ORDER BY category, price DESC sorts by category first, then by price within each category.
Add LIMIT to keep only the first few rows. "What are our five most expensive products?" becomes ORDER BY price DESC LIMIT 5; (SQL Server uses SELECT TOP 5 instead.)
Watch
- VideoThe ORDER BY and LIMIT Clauses in SQL to Help With Sorting
Sorting results and keeping the top rows.
- PlaylistMySQL Basics Tutorial Series (Alex The Analyst)
Optional playlist if you want extra practice with each clause.
Practice
Write a query for each question against the course database:
- List every product's name and price.
- Which products cost more than 25?
- Which customers live in Chicago or Denver?
- Which customers' names start with the letter J?
- Which orders were placed in March 2026?
- What are the three most expensive products?
- Which customers have no city recorded?
Check your understanding
Try each question first, then open it to see the answer.
How do you find rows where email is missing?
WHERE email IS NULL.
What does ORDER BY price DESC LIMIT 1 return?
The single most expensive row.
What does LIKE '%phone%' match?
Any text that contains the word phone anywhere.
Assignment
Submit a text or .sql file with your seven queries, each preceded by a comment stating the question it answers.