SQL is the language every analyst, data engineer, and backend developer needs. It's been around since the 1970s and isn't going anywhere — every major database speaks a dialect of it. This chapter covers the four statements that handle most everyday work.
Why SQL?
- Universal. Postgres, MySQL, SQL Server, BigQuery, Snowflake, Redshift, ClickHouse — they all speak SQL.
- Declarative. You describe what you want, not how to get it. The query planner figures out the rest.
- Hire-able. Every analytics, data, and BI role lists SQL as a primary requirement.
Anatomy of a SELECT
SELECT name, department, salaryFROM employeesWHERE salary > 100000ORDER BY salary DESCLIMIT 10;
SQL clauses run in a fixed logical order, even though you write them in a different order:
| Written order | Logical order |
|---|---|
| SELECT | FROM |
| FROM | WHERE |
| WHERE | GROUP BY |
| GROUP BY | HAVING |
| HAVING | SELECT |
| ORDER BY | ORDER BY |
| LIMIT | LIMIT |
Filtering with WHERE
-- Comparison operatorsSELECT * FROM employeesWHERE salary >= 100000;-- AND / ORSELECT * FROM employeesWHERE department = 'Engineering' AND salary > 80000;-- IN — match any value in a listSELECT * FROM employeesWHERE department IN ('Engineering', 'Design', 'Product');-- BETWEEN — inclusive rangeSELECT * FROM ordersWHERE order_date BETWEEN '2026-01-01' AND '2026-03-31';-- LIKE — wildcardsSELECT * FROM customersWHERE email LIKE '%@gmail.com'; -- ends withSELECT * FROM productsWHERE name LIKE 'Apple%'; -- starts with-- NULL checks — = NULL never matches!SELECT * FROM usersWHERE referral_code IS NULL;
ORDER BY & LIMIT
-- Sort ascending (default)SELECT name, salary FROM employees ORDER BY salary;-- Sort descendingSELECT name, salary FROM employees ORDER BY salary DESC;-- Multi-column sortSELECT name, department, salary FROM employeesORDER BY department ASC, salary DESC;-- Top 5SELECT name, salary FROM employeesORDER BY salary DESCLIMIT 5;-- Pagination — Postgres / MySQLSELECT name FROM employeesORDER BY nameLIMIT 20 OFFSET 40; -- skip 40, take 20
Aggregations & GROUP BY
The other half of SQL: collapsing many rows into one.
| Function | Returns |
|---|---|
| COUNT(*) | number of rows |
| COUNT(col) | non-NULL values in col |
| SUM(col) | total of numeric col |
| AVG(col) | mean of col |
| MIN(col) / MAX(col) | extremes |
-- Count rowsSELECT COUNT(*) FROM orders;-- Group by departmentSELECT department,COUNT(*) AS headcount,AVG(salary) AS avg_salary,MAX(salary) AS top_salaryFROM employeesGROUP BY departmentORDER BY headcount DESC;-- Filter on the aggregate — use HAVING, not WHERESELECT department, COUNT(*) AS cntFROM employeesGROUP BY departmentHAVING COUNT(*) >= 10;
Key takeaways
- 01SQL is declarative — describe what you want; the engine figures out how.
- 02Logical evaluation order is FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.
- 03Always use IS NULL / IS NOT NULL — never = NULL.
- 04WHERE filters rows before aggregation; HAVING filters groups after.
- 05Five aggregates handle 90% of work: COUNT, SUM, AVG, MIN, MAX.
