Skip to content
SQLSimplified

Blog

Longer-form writing on SQL that goes beyond the lessons: the mistakes that survive code review, how the database actually runs your query, and the techniques worth learning next. Every example runs against a live database in your browser.

Aggregation
Why Averaging Your Averages Gives the Wrong Number in SQL
AVG(AVG(x)) is not the same as AVG(x) unless every group has the same row count. See the query that proves it and the weighted-average fix.
Aggregation
How Do You Find the Second Highest Salary in SQL?
Compare the MAX subquery, LIMIT OFFSET, and DENSE_RANK approaches, and see why each one returns something different when there's a tie or no runner-up.
CTE
When Should You Use a CTE Instead of a Subquery?
Correlation is the real dividing line between a CTE and a subquery, not readability, and the "CTEs always run once" claim is not a language rule.
Aggregation
Why COUNT(*) and COUNT(column) Give Different Answers in SQL
COUNT(*), COUNT(column), and AVG() each follow a different rule for NULLs, so mixing them in one query can quietly produce the wrong number.
String Functions
REPLACE in SQL Is Two Different Things
REPLACE() rewrites text inside a string. REPLACE INTO rewrites a whole row. They share a keyword and nothing else, and mixing them up breaks queries.
Aggregation
Where vs Having in SQL
WHERE and HAVING filter different sets of rows, so moving a condition between them can change your numbers, not just your syntax.
GROUP BY
Why SQL Says a Column Must Appear in the GROUP BY Clause
Fix the GROUP BY "column must appear" error, understand why it fires, and see why MySQL's silent version of the same bug is worse than an error.
Date Functions
Why DATEDIFF Gives the Wrong Age in SQL
DATEDIFF counts calendar boundaries crossed, not elapsed time. See the exact bug that inflates age and tenure, and the one-line fix.
Window Functions
Window Functions Explained by Example
Ranking, running totals and month-over-month change, built one clause at a time, plus the frame default that quietly breaks running totals on ties.
Performance
How to Actually Read a Query Plan
A practical guide to reading execution plans inside out, comparing estimated rows against actual rows, and fixing the one operator that matters.
SQL Basics
Six SQL Mistakes Beginners Make
The six bugs that bite almost every new SQL writer (NULL comparisons, filtered LEFT JOINs, WHERE versus HAVING) and how to catch each one yourself.