SQL for BAs: SELECT and WHERE
Ask a database questions with SELECT and WHERE. Learn conditions, null, sorting, limits, the second-highest trick, the five kinds of SQL statement and safe habits for analysts.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: The librarian. You ask a librarian, "Give me every book by one author published after 2020." The librarian walks to the shelves, picks out the books that fit, and brings them to you. A good librarian also understands a vague ask, such as "something about boats".
A database is not so kind. It does exactly what the question says, so every word must be exact.
Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Its tasks sit in a table called tasks. Sam, the , wants to know which open tasks are due on the example date. Sam writes the question in (Structured Query Language).
SELECT title FROM tasks WHERE status = 'open' AND due_date = '2026-10-20';
What it is: SQL (Structured Query Language) is a language for asking questions of a database. A database keeps its data in tables. A table is a grid where each row is one thing, such as one task, and each column is one fact about it, such as its title. SELECT picks the columns to show, and WHERE keeps only the rows that pass a test. You type SQL into a tool that is connected to a database such as PostgreSQL, MySQL or SQLite. WHERE is easy to mix up with HAVING, which filters groups of rows after they are counted.
Why it matters: Without SQL, Sam must wait for a developer each time Nina, the , asks "how many tasks are due on the example date?". With SELECT and WHERE, Sam can check a report figure or a screen against the real data in minutes. A wrong WHERE gives a neat answer that is wrong, so Sam also learns to count the rows that come back.
The librarian understands a vague ask. SQL does not.
PLAIN WORDS · 1 in this section
- sqlite
- a small free database tool
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the work.
THE REAL JOB The picture helps a newcomer remember that a query is a precise ask, and that the database does not guess what was meant.
SELECTwhatFROMwhereWHEREwhich rowsORDER BYhow- Check the row count
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| Asking for books by one author | SELECT, choosing what to show |
| The shelves the librarian walks to | FROM, the table to look in |
| After a chosen year as a test on each book | WHERE, the condition on each row |
| A book with no printed year | A NULL, which ordinary equality cannot match; IS NULL can |
| Counting the books you got | Checking the number of rows |
WHERE THIS PICTURE STOPS
- A librarian understands a vague ask, but Structured Query Language (SQL) does not.
- A librarian points out a book with no year. Structured Query Language (SQL) leaves it out of an ordinary comparison unless missing values are handled.
- A librarian only fetches books. Structured Query Language (SQL) can also change them if given the power.
HOOK
Pick the columns, name the shelf, test each row.
REMEMBER IT THIS WAY
A rule of thumb: if you see this, think that.
If a row has no value in the tested column, a test quietly leaves it out, so check blanks.
| In the trick | What it stands for |
|---|---|
| blank | a book with no year |
| WHERE | a test |
A column that must be filled in cannot hide rows this way.
TELL A FRIEND
Structured Query Language () asks a database questions. SELECT chooses what to show, FROM names the table, and WHERE keeps only the rows that pass a test, with null needing IS NULL.
OFTEN MIXED UP WITH Entities and relationships (ERD). That topic shows the things and links in the data. This one asks the database questions about them.
PRACTISE AND KEEP
Sources for this topic — 6 sources
- Market practice
SELECT command reference (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, SELECT: WHERE/HAVING/output aliases, functional dependence, NULL ordering and LIMIT; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: SELECT: WHERE/HAVING/output aliases, functional dependence, NULL ordering and LIMIT. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
Conditional expressions (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, §9.18 CASE/COALESCE/NULLIF; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §9.18 CASE/COALESCE/NULLIF. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
SQLite: SQL expressions ↗ — SQLite project · SQLite: SQL expressions; CASE/BETWEEN/IN/EXISTS, scalar cardinality, NULL-safe equality and parameter values. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Market practice
PostgreSQL18: SET TRANSACTION ↗ — PostgreSQL Global Development Group · PostgreSQL 18: SET TRANSACTION; Read-only mode and isolation/snapshot characteristics. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Market practice
PostgreSQL18: PREPARE ↗ — PostgreSQL Global Development Group · PostgreSQL 18: PREPARE; Parameter values separate from statement text and placeholder syntax; session scope. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Simplified educational illustration
Payments Signal editorial teaching models — Payments Signal
What this simplifies: Daily List people, data and exercises are invented for teaching. Query results belong to the declared fixture and database version. Authorization, business identity, time bounds and unknown-value policy must be agreed in context; no universal mandatory query template is implied.
Used wherever diagrams, scenarios, figures, or example values are didactic constructions rather than sourced facts; every such use carries a simplifications disclosure. All people, companies, banks, and list entries in examples are fictional.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.