Latticework

Command Palette

Search for a command to run...

SQL

SELECT & WHERE

16 min

Explanation

Every SQL query starts from the same shape: SELECT picks which columns you want, FROM picks the table, and WHERE filters which rows survive.

SELECT name, salary
FROM employees
WHERE department = 'Engineering';

Column order in SELECT is up to you — it doesn't have to match the table's actual column order. SELECT * means "every column," useful for exploring a table but usually avoided in real queries (it breaks if the table's columns ever change).

Try it

Every exercise in this course seeds its own small table first, then asks you to write the query — run this one to see the pattern.

Loading editor…
Explanation

WHERE supports the comparison operators you'd expect (=, !=, >, <, >=, <=), plus a few SQL-specific ones:

WHERE department IN ('Sales', 'Marketing')     -- matches either
WHERE name LIKE 'A%'                            -- starts with "A"
WHERE department = 'Sales' AND salary > 100000  -- combine with AND/OR
WHERE department IS NOT NULL                    -- never use = NULL, it never matches

ORDER BY column DESC (or no DESC for ascending, the default) controls row order — SQL doesn't guarantee any particular order without it, so a query without ORDER BY can legitimately return rows in a different sequence each time.

Exercise

The `employees` table has columns `id, name, department, salary`. Write a query that returns the `name` and `salary` of every employee in the 'Engineering' department, ordered by salary descending.

Exercise

Write a query that returns the `name` and `department` of every employee earning more than 100000, ordered by name (alphabetically).

Quiz

Which SQL clause filters individual rows BEFORE any grouping happens?

Checkpoint

You can write a SELECT with a WHERE filter and an ORDER BY, and know why queries you care about the order of always need an explicit ORDER BY.