A library service across three branches in Mazandaran: 200,000 members, 400,000 loans. One question, asked twice, on 2026-09-01.
SELECT id, title FROM loans WHERE member_id = ?- before any index: 0.0119 s
- after
CREATE INDEX loans_member ON loans(member_id): 0.000003 s
The same lookup took 0.0119 s without an index and 0.000003 s with one, and the database says why in one line. Put EXPLAIN QUERY PLAN in front of the query and read the answer: EXPLAIN QUERY PLAN said SCAN loans before the index and SEARCH loans USING INDEX after it. SCAN means every one of the four hundred thousand rows was read to find the handful that matched. SEARCH means it went straight there.
That one word is the most useful thing in this language for anybody wondering why a query is slow, because it answers the question directly rather than by guesswork. Building the index took 0.116 s, and it is paid once, and an index makes reads faster and writes slower, because it has to be kept in order. That is the whole trade, and it is why a table is not simply indexed on every column, in why a query is slow, and what an index costs.
Joining, and the row that disappears
Loans are in one table and members in another, and a question about both needs them joined on the column they share. Counting open loans per branch is one statement:
The four joins
Two questions that name every join there is
Not a picture of overlapping circles. The only thing that separates the four is what happens to a row with nothing to match.
drop unmatched rows from the right table
- 0 rows for that member
- 1 row for that member
keep them
- The mirror of the cell above
- Rare, and usually a sign of two systems being reconciled
MySQL has no FULL OUTER JOIN and it is written as two joins combined. That is an engine difference rather than a gap in the idea, and it is the sort of thing that only appears when a query moves.
Interactive Practice Lab
Edit and run this snippet directly in your browser (zero setup)
CREATE TABLE workshop_orders (id INTEGER PRIMARY KEY, supplier TEXT, qty INTEGER, city TEXT);INSERT INTO workshop_orders VALUES (1, 'Sepehr', 15, 'Ardabil'), (2, 'Aria', 47, 'Tabriz'), (3, 'Sepehr', 22, 'Ardabil'), (4, 'Nima', 31, 'Tehran');SELECT supplier, COUNT(*) AS deliveries, SUM(qty) AS total_qtyFROM workshop_ordersGROUP BY supplierORDER BY total_qty DESC;Live Big-O Complexity & Binary Search Race Lab
Compare complexity curves at scale and watch Linear vs Binary Search race live.
SELECT m.branch, COUNT(*) FROM loans l JOIN members m ON m.id = l.member_id WHERE l.returned = 0 GROUP BY m.branch- gives Babol 100222, Sari 100079, Amol 99699
The word that decides everything about a join is which rows survive when there is nothing to match. An inner join drops rows with no match, and a left join keeps them. A member registered at a fourth branch who has never borrowed anything makes the point exactly: the member with no loans gave 0 rows from an inner join and 1 from a left join.
That is not a detail, it is the commonest wrong answer in reporting. Count members per branch with an inner join and every member who has never borrowed is silently absent, so the branch with the most new members looks like the smallest one. The four kinds and what each keeps are combining tables, and the four kinds.

Collapsing rows, and where the filter goes
GROUP BY collapses rows and every selected column must be grouped or aggregated, and SQLite is the one that lets you forget, which is worth knowing before a query that works on a laptop is run against something stricter.
Order of work
What filters rows, what collapses them, and what filters groups
Putting a row filter in HAVING or a group filter in WHERE produces a plausible wrong number instead of an error.
- Table rowsAll loan records before any filtering
- WHERE clauseDrops individual rows such as returned loans first
- COUNT(*) vs COUNT(col)Counts every row in the group, or skips nulls in that column
- HAVING clauseDrops whole groups, such as branches below a threshold
SQLite allows ungrouped columns in SELECT without raising an error, so a query that appears to work locally can fail or mislead on stricter engines.
Two more rules cover most reporting mistakes. WHERE filters rows before grouping and HAVING filters groups after, so excluding returned loans is a WHERE and excluding branches with fewer than a hundred open loans is a HAVING, and putting either in the other place gives a wrong number rather than an error. And COUNT(*) counts rows and COUNT(column) skips the nulls, which is the difference between how many loans there are and how many have a due date recorded. Those are counting, summing and grouping by, on top of choosing rows and columns.
A question inside a question is sometimes the clearest way to write one of these and sometimes a join in disguise that the engine cannot optimise, which is a query inside a query, and when to avoid one.

What to run in 2026
EXPLAIN, in whatever spelling your engine uses, is the tool on this page. It is free, it is already there, and it replaced guessing which part of a query was slow, which people are consistently bad at: the slow part is almost never the part that looks complicated.
A local SQLite file is the cheapest place to practise, free and in Python's standard library with nothing installed, and the four hundred thousand rows above were built in it in a few seconds. It replaced needing a server running before you could write your first SELECT.
A migration tool, free in every ecosystem, is the third. It replaced changing a table by hand on one machine and forgetting on the other, and it belongs here rather than in a deployment list because a query is written against a shape that has to be the same everywhere. That is deciding what the tables are before you fill them.
The mistake: asking the database for everything and finishing the job in code
The stall is not syntax. It is that people learn SELECT, stop, and do the rest in the language they already know: fetch the loans, fetch the members, match them in a loop, count them in a dictionary. It works on the test data and it is exactly the wrong shape.
Where work runs
Pulling every row into code versus asking the engine
Fetching tables to join and count in application loops works on tiny test sets and degrades linearly as data grows.
- Finish the job in application codeThe whole table crosses the connection
- SELECT without WHERE on a growing table
- Join loans and members inside app loops
- Count matches in an application dictionary
- Gets slower with every row added
- Compute inside the SQL statementOnly the answer crosses the connection
- Filter rows early with WHERE
- Join tables in the query engine
- Collapse and count with GROUP BY
- Response stays the size of the answer
Send the question, not the table
A loop in application code that only adds values up is the clearest sign that work belongs inside the SQL statement.
Four hundred thousand rows crossing a connection to be counted in a loop is slower than the same count done where the data already is, and it gets slower with every row added while the SQL version barely moves. The tell is a loop in the application whose body only adds things up, or a query with no WHERE clause on a table that grows.
The rule that fixes it: send the question, not the data. Filtering, joining, grouping and ordering all belong in the statement, and the answer that comes back should be the size of the answer rather than the size of the table.
What this page has not touched is why the loans and the members are in two tables at all, which is a decision made before any of this and is hard to undo afterwards, in storing a fact once, and the forms that get you there. The engines differ in exactly the ways that bite later, in the databases you will actually be handed, and the one thing never to do with a query string is build it by joining text, which is why you never build a query by concatenation.
