← All articles

Articles

SQL bugs that look like mysteries until you see the one rule behind them. Each one is a real failure mode, explained properly.

Why Your LEFT JOIN Returns Too Many Rows

A LEFT JOIN doesn't return "one row per left-side thing." It returns one row per match. Here's the mental model that fixes duplicated totals for good.

WHERE vs HAVING — The Mistake Everyone Makes Exactly Once

"Aggregate function calls cannot appear in WHERE clause" isn't a random rule — it's a direct consequence of the order SQL actually executes in. Here's the model.

NULL Is Not Zero, and NOT IN Is Not Safe

A NOT IN subquery that silently returns zero rows is one of the most common production SQL bugs. It's caused by a single NULL, and it's invisible until it happens.

The N+1 Query Problem, Explained With the Bug You Already Shipped

Your app works fine in dev with 10 rows and falls over in production with 10,000. It's not a scaling mystery — it's one query hiding inside a loop.

Window Functions Explained With the One Mental Model That Actually Sticks

Window functions aren't a fancier GROUP BY. GROUP BY collapses rows. Window functions let every row see beyond itself without losing a single one.

Why Adding an Index Made Your Query Slower

An index isn't a universal speed boost. It's a trade — faster reads for slower writes and extra storage — and the database won't always take the deal you expect.

The UPDATE That Changed Every Row (And How to Never Ship It Again)

UPDATE without a WHERE clause doesn't fail. It succeeds, instantly, on every row in the table. Here's why it happens and the habits that make it structurally impossible.

EXISTS vs IN vs JOIN: How to Actually Choose

Three ways to check "does a matching row exist" in SQL, and they are not interchangeable. Here's what actually differs between EXISTS, IN, and JOIN.

OFFSET Pagination Breaks at Scale — Here's What to Use Instead

LIMIT/OFFSET pagination gets dramatically slower as the offset grows, and can silently skip or duplicate rows if data changes between pages. Keyset pagination fixes both.

Your Timestamps Are Wrong — The UTC Mistake Almost Every App Makes

A timestamp with no timezone attached isn't neutral, it's ambiguous. Here's why that ambiguity causes real bugs, and the one rule that prevents almost all of them.