Sanity Checks

A query that runs is not a query that is right. SQL will happily give you a confident, well-formatted, entirely wrong number, and nothing about the output looks different.

The checks worth running every time:

Does the row count change after a join, and did you expect that?

Does the sum of the groups equal the total?

Are there rows with NULL in the column you filtered on, which the filter silently excluded?

Does an inner join drop rows a left join would keep?

Does the number change if you write the query a second way?

Try it

Now do it via a join to authors, three ways in total, and check all three agree. Then break one on purpose by changing the join to inner and adding a book with a NULL author_id.

loading editor…
real SQLite, in your browser

Two routes to the same number. When they disagree, something in the grouping or joining is not doing what you think.

✦ The most valuable check in SQL

Compute it a second way. It is the same discipline as the APIs & Datasets module, and it is even more valuable here, because a join silently multiplying or dropping rows produces a plausible number rather than an error.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Reading a Query You Did Not Write →
← Back