SELECT, WHERE, ORDER BY & LIMIT: Reading Rows

SQL & Querying
23 / 47

A SQL query is a pipeline: pick a table, keep the rows that pass a test (WHERE), choose the columns you want (SELECT), sort them, and cut the list short (LIMIT).

The Analogy (Read This First)

Think of a table as a giant spreadsheet. WHERE hides every row that doesn't match, SELECT hides every column you didn't ask for, ORDER BY re-sorts what is left, and LIMIT keeps only the first few rows.

Deep Dive Analysis

SELECT ... FROM names the columns you want and the table they come from. SELECT name, city FROM customers returns two columns for every row; SELECT * returns every column. Prefer naming columns: it is clearer, and the query does not silently change shape when someone adds a column.

WHERE keeps only the rows for which the condition is true. Comparisons use =, <> (or !=), <, <=, > and >=. Text goes in single quotes, numbers do not. You can compare a column to another column the same way: WHERE paid_amount > invoice_amount finds rows where two stored numbers disagree, a classic way to spot a doctored record.

SELECT name, city
FROM customers
WHERE city = 'Austin';

Combine conditions with AND (both must be true), OR (either) and NOT. AND binds tighter than OR, so whenever you mix them, add parentheses to say what you mean:

SELECT id, amount
FROM orders
WHERE status = 'paid'
  AND (amount > 1000 OR customer_id = 7);

Shortcuts for common tests. IN ('a', 'b', 'c') matches any value in a list and replaces a chain of ORs. BETWEEN 100 AND 500 includes both ends. LIKE matches text patterns: % means any run of characters (including none) and _ means exactly one character, so LIKE 'Al%' finds names starting with Al and LIKE '%invoice%' finds the word anywhere.

SELECT id, amount, status
FROM orders
WHERE status IN ('paid', 'refunded')
  AND amount BETWEEN 100 AND 500;

DISTINCT removes duplicate rows from the result: SELECT DISTINCT city FROM customers lists each city once, however many customers live there. It applies to the whole combination of selected columns, not just the first one.

ORDER BY sorts the result (ascending by default, add DESC for biggest or newest first) and accepts several columns: ORDER BY city, created_at DESC. LIMIT n keeps only the first n rows after sorting, so ORDER BY created_at DESC LIMIT 1 is the standard way to ask for the most recent row. Without ORDER BY, 'first' has no defined meaning: LIMIT alone returns arbitrary rows.

Written order vs. run order. You write SELECT first, but the database logically works through FROM, then WHERE, then SELECT, then ORDER BY, then LIMIT. That is why, in standard SQL, a WHERE clause cannot use a column alias you only defined in SELECT, and why WHERE is the first lever to reach for when a result is too big.

Key Bullets
  • WHERE filters rows, SELECT picks columns, ORDER BY sorts, LIMIT trims - and they run in that logical order.
  • Text goes in single quotes, numbers stay bare: city = 'Austin', amount > 1000.
  • AND binds tighter than OR - add parentheses whenever you mix them.
  • BETWEEN includes both ends; IN replaces a chain of ORs.
  • LIKE: % = any run of characters, _ = exactly one character.
  • LIMIT without ORDER BY returns arbitrary rows - always sort first when you mean "top" or "latest".
Real-World Examples
Most recent event: ORDER BY created_at DESC LIMIT 1Rows for a set of ids: WHERE id IN (3, 7, 12)Names with a prefix: WHERE name LIKE 'Al%'Spot inconsistent records: WHERE paid_amount > invoice_amount

Curated Curation & Deep Insights

ScaleDojo Certified
Video Tutorial Pending

Our system architects are vetting high-quality, authorized video guides for SELECT, WHERE, ORDER BY & LIMIT: Reading Rows with zero third-party platform links.

Verification in Progress
Reading Material Pending

We are preparing premium, zero-competitor deep-dives for SELECT, WHERE, ORDER BY & LIMIT: Reading Rows. Authorized reading references will appear automatically.

Verification in Progress