Sam needs every order above £500 from the Electronics category, placed last quarter. In Excel he would filter three columns manually. In SQL he writes one WHERE clause — and it runs against ten million rows in seconds.

The SELECT clause in detail

SELECT controls which columns appear in your results. Use * to get all columns (fine for exploration, bad for production) or list specific column names separated by commas.

-- All columns (exploration only)
SELECT * FROM SalesFact;

-- Specific columns (production queries)
SELECT OrderDate, Salesperson, Revenue FROM SalesFact;
💡
Analyst tip: Never use SELECT * in a query others will consume. Column order and count can change when a table is altered, silently breaking downstream reports.

Column aliasing with AS

AS renames a column in the output. It does not rename the column in the database — only in your results.

SELECT
  Salesperson AS "Sales Rep",
  Revenue AS "Total Revenue (£)"
FROM SalesFact;

Comparison operators

Combining conditions — AND, OR, NOT

-- AND: both conditions must be true
WHERE Region = 'North' AND Revenue > 1000

-- OR: either condition can be true
WHERE Category = 'Electronics' OR Category = 'Furniture'

-- NOT: exclude matching rows
WHERE NOT Region = 'South'
⚠️
Common mistake: Mixing AND and OR without brackets. WHERE a = 1 OR b = 2 AND c = 3 is evaluated as a = 1 OR (b = 2 AND c = 3) because AND has higher precedence. Always use brackets when mixing both.

The IN operator

IN is a cleaner alternative to multiple OR conditions on the same column.

-- Without IN (verbose)
WHERE Category = 'Electronics' OR Category = 'Furniture' OR Category = 'Sports'

-- With IN (clean)
WHERE Category IN ('Electronics', 'Furniture', 'Sports')

The BETWEEN operator

WHERE Revenue BETWEEN 500 AND 2000
-- Equivalent to: WHERE Revenue >= 500 AND Revenue <= 2000

Pattern matching with LIKE

WHERE Salesperson LIKE 'A%' -- starts with A
WHERE Product LIKE '%Chair%' -- contains Chair
WHERE Product LIKE '%er' -- ends with er

NULL handling

NULL means unknown — not zero, not empty string. You cannot compare to NULL with = — use IS NULL or IS NOT NULL.

WHERE Revenue IS NOT NULL -- correct
WHERE Revenue != NULL -- WRONG — always returns nothing
🎯 Your Challenge

Write a query that returns all sales from salespersons whose names start with 'P' or 'A', in the Electronics or Sports category, with revenue above 200. Use LIKE, IN, AND, and a comparison operator together.

FAQ

WHERE always comes before ORDER BY. The SQL clause order is: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT. You must follow this order or the query will error.
Not in the WHERE clause of the same query — the alias is not yet defined when WHERE is evaluated. Use the original column name in WHERE, or wrap the query in a subquery and filter on the alias in the outer query.
`