Data Analyst Program

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 with AS (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.

OperatorMeaningExample
= , <>equal, not equalWHERE category = 'Books'
> < >= <=comparisonsWHERE price >= 50
AND, OR, NOTcombine testsWHERE category = 'Books' AND price < 20
INmatches any value in a listWHERE city IN ('Chicago','Denver')
BETWEENwithin a range (inclusive)WHERE order_date BETWEEN '2026-01-01' AND '2026-03-31'
LIKEpattern match; % means any charactersWHERE name LIKE 'A%' (starts with A)
IS NULLvalue is missingWHERE 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

Practice

Write a query for each question against the course database:

  1. List every product's name and price.
  2. Which products cost more than 25?
  3. Which customers live in Chicago or Denver?
  4. Which customers' names start with the letter J?
  5. Which orders were placed in March 2026?
  6. What are the three most expensive products?
  7. 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.