Skip to content
SPM Tuition
Computer Science chapter guide

SQL reasoning in a database

You can write a query, but you cannot say what it will return before running it.

SQL reasoning means working out what a query returns before you run it. The fictional databases here are small enough to trace on paper.

This page sits inside database development and builds on joining related tables.

What are the four habits?

  1. Write the tables with their rows before reading the query.
  2. Follow the evaluation order: FROM, JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY.
  3. Count rows at each step.
  4. Predict edge cases: no match, empty group and missing value.

What is an example of reasoning?

A fictional table Sales has these rows: (Ampang, Bag, 45), (Ampang, Pen, 12), (Bangi, Bag, 50).

The query SELECT Branch FROM Sales WHERE Amount > 20; keeps two rows, then shows the Branch of each. The output is Ampang and Bangi, one row each, because only the Bag sales pass.

Move the filter and the answer changes. Filtering on Amount > 50 returns no rows at all, even though the table has three.

What are the lessons?

Lesson The skill
Predicting which rows a WHERE condition will include filtering
Explaining why a join produces more rows than expected row counts in joins
Distinguishing row filtering from group filtering WHERE versus HAVING
Testing an aggregate query with empty groups and missing values edge cases

Then try the SQL reasoning practice set.

Who should start where?

A student new to queries should begin with WHERE. A student who can join but gets row counts wrong should start with the join lesson. A student who mixes up WHERE and HAVING should go straight to the filtering lesson.

If you want a teacher to watch you predict output, see online one-to-one Computer Science tuition. The one-hour trial class (from RM50) is a taught lesson on the topic you choose.

Common questions

Why predict the output by hand?

Exam questions give you a table and a query and ask what comes back. No system runs the query for you. Predicting by hand also catches errors in your own queries before you submit them.

What order should I work in?

FROM and JOIN first, then WHERE, then GROUP BY, then HAVING, then SELECT, then ORDER BY. Following this order stops you filtering groups too early or reading columns that have not been formed yet.

Does this page need a database program?

No. All tables here are small enough to work on paper. If you want to test yourself, write your prediction before you run the query in any system your school provides.

If predicting query output is where marks slip, one-to-one Computer Science lessons let a teacher ask you for the row count first and then compare it with the real result.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.