pbPassingBI
/
Query basics beginner 9 min

SELECT, WHERE and logical order

The clauses, and the order the database actually evaluates them in.

What you'll be able to do
  • Write a filtered, sorted query
  • Recite the logical processing order of a SELECT
  • Explain why you cannot use a SELECT alias in WHERE

The clauses

SELECT   customer_id, order_total
FROM     orders
WHERE    order_date >= '2024-01-01'
ORDER BY order_total DESC
LIMIT    10;

LIMIT is PostgreSQL/MySQL/SQLite. SQL Server uses TOP 10 after SELECT, or OFFSET ... FETCH. Oracle uses FETCH FIRST 10 ROWS ONLY. Interviewers often ask which dialect you know — say so explicitly.

Logical processing order

You write a query in one order and the database evaluates it in another:

  1. FROM / JOIN
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. DISTINCT
  7. ORDER BY
  8. LIMIT

This single list answers a lot of interview questions. SELECT runs fifth, which is why an alias defined there is not visible to WHERE:

-- fails
SELECT price * qty AS revenue FROM sales WHERE revenue > 100;

Repeat the expression in WHERE, or wrap the query in a subquery or CTE. ORDER BY runs after SELECT, so aliases do work there.

NULL is not a value

NULL means unknown, so comparisons return unknown rather than true or false. WHERE col = NULL matches nothing — use IS NULL / IS NOT NULL.

The subtle one: NOT IN with a NULL in the list returns no rows at all, because the comparison can never be proven true. NOT EXISTS does not have this problem, which is why it is the safer default.

COALESCE(col, 0) substitutes a fallback. Aggregates ignore NULLs — AVG divides by the count of non-null values, not the row count, which is a common source of surprise.

Filtering specifics

IN (…) for a value list, BETWEEN a AND b inclusive on both ends, LIKE 'abc%' for prefix matching (a leading % usually prevents index use).

For dates, prefer a half-open range — >= '2024-01-01' AND < '2024-02-01' — over BETWEEN, which includes the endpoint and will silently drop or double-count timestamps at midnight.

Key points
  • FROM → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT
  • SELECT aliases work in ORDER BY but not in WHERE
  • NOT IN breaks on NULL; prefer NOT EXISTS
Check yourself