SQL Query Output Questions for the NSCT
SQL query output questions show a small table and a query, then ask which rows or values come back. Most hinge on three things: the logical order in which the clauses run, how aggregates treat NULL, and how a subquery is evaluated. Databases carries 10% of the NSCT according to HEC's Areas of Competencies, and candidates averaged 42.3% accuracy in it in 2026, per the HEC NSCT report.
The official SQL topic introduces SELECT, WHERE, GROUP BY and HAVING, and Advanced SQL adds subqueries. This page drills output tracing on one table. Every result below was produced by running the query in SQLite 3.50.
The method, step by step
- FROM and joins. Build the working set of rows.
- WHERE. Remove rows. Any comparison with NULL is UNKNOWN, and UNKNOWN rows are dropped.
- GROUP BY. Put the surviving rows into groups. All NULLs in the grouping column form a single group.
- Aggregates. Compute them per group. Every aggregate except COUNT(*) skips NULLs.
- HAVING. Remove whole groups.
- SELECT, then DISTINCT. Choose the output columns.
- ORDER BY, then LIMIT/OFFSET. Sort, then cut.
Write the intermediate table at each step. It takes seconds and prevents most errors.
Every example uses orders(oid, cust, city, amount):
| oid | cust | city | amount |
|---|---|---|---|
| 1 | Ali | LHR | 300 |
| 2 | Sara | KHI | 150 |
| 3 | Ali | LHR | 200 |
| 4 | Omar | ISB | NULL |
| 5 | Hina | KHI | 400 |
| 6 | Sara | KHI | 250 |
| 7 | Zain | LHR | 100 |
| 8 | Omar | ISB | 50 |
Aggregates and NULL
SELECT COUNT(*), COUNT(amount), SUM(amount), AVG(amount), MIN(amount), MAX(amount)
FROM orders;
| COUNT(*) | COUNT(amount) | SUM | AVG | MIN | MAX |
|---|---|---|---|---|---|
| 8 | 7 | 1450 | 207.14 | 50 | 400 |
AVG is 1450 / 7, not 1450 / 8, because the NULL is skipped in both the sum and the count. Over zero non-NULL values, SUM, AVG, MIN and MAX return NULL, while COUNT returns 0.
GROUP BY
SELECT city, COUNT(*), COUNT(amount), SUM(amount), AVG(amount)
FROM orders GROUP BY city ORDER BY city;
| city | COUNT(*) | COUNT(amount) | SUM | AVG |
|---|---|---|---|---|
| ISB | 2 | 1 | 50 | 50.0 |
| KHI | 3 | 3 | 800 | 266.67 |
| LHR | 3 | 3 | 600 | 200.0 |
ISB's average is 50, not 25, because the NULL order counts as a row for COUNT(*) but not for AVG.
HAVING
HAVING filters groups after aggregation. Customer totals are Ali 500, Hina 400, Omar 50, Sara 400 and Zain 100.
SELECT cust, SUM(amount) FROM orders
GROUP BY cust HAVING SUM(amount) > 300 ORDER BY cust;
Output: Ali 500, Hina 400 and Sara 400.
Full pipeline worked example.
SELECT city, COUNT(*) AS n, SUM(amount) AS total
FROM orders
WHERE amount >= 150
GROUP BY city
HAVING COUNT(*) >= 2
ORDER BY total DESC;
| Step | Result |
|---|---|
| WHERE | keeps oids 1, 2, 3, 5 and 6 (100, 50 and NULL are dropped) |
| GROUP BY | LHR: 300, 200. KHI: 150, 400, 250 |
| HAVING | both groups have at least 2 rows |
| ORDER BY | KHI 3 800, then LHR 2 500 |
ISB disappears at the WHERE stage, before it ever becomes a group.
Scalar subqueries
A scalar subquery returns one value and can stand wherever a value is allowed.
SELECT oid, cust, amount FROM orders
WHERE amount > (SELECT AVG(amount) FROM orders);
The subquery gives 207.14, so the output is oids 1 (300), 5 (400) and 6 (250). If a scalar subquery returns no rows, its value is NULL. If it returns more than one row, the query fails.
Correlated subqueries
A correlated subquery refers to the outer row, so it is re-evaluated for each row.
SELECT o.oid, o.cust, o.amount FROM orders o
WHERE o.amount > (SELECT AVG(i.amount) FROM orders i WHERE i.city = o.city);
| City | City average | Orders above it |
|---|---|---|
| LHR | 200.0 | oid 1 (300) |
| KHI | 266.67 | oid 5 (400) |
| ISB | 50.0 | none (50 is not greater than 50) |
Output: oids 1 and 5. Sara's 250 is above the overall average but below KHI's.
IN, EXISTS and derived tables
IN (subquery) tests membership. For example, city IN (SELECT city FROM orders GROUP BY city HAVING SUM(amount) > 650) keeps only KHI. NOT EXISTS finds rows with no partner. With a table vip holding Ali, Hina and Bilal, the query SELECT v.cust FROM vip v WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.cust = v.cust) returns Bilal.
A derived table (a subquery in FROM) lets you aggregate twice:
SELECT AVG(t) FROM (SELECT cust, SUM(amount) AS t FROM orders GROUP BY cust);
The customer totals are 500, 400, 50, 400 and 100, so the average per customer is 290.0. Compare that with 207.14 per order.
ORDER BY and LIMIT
ORDER BY amount DESC LIMIT 2 OFFSET 1 skips the top row (400) and returns oids 1 (300) and 6 (250).
NULL placement differs between systems. ORDER BY amount LIMIT 3 in SQLite returns oid 4 (NULL) first, then 8 (50) and 7 (100). MySQL and SQL Server also put NULLs first in ascending order, but PostgreSQL and Oracle put them last, which would return 8, 7 and 2. Standard SQL lets you choose with NULLS FIRST or NULLS LAST.
Speed tricks
- Cross out WHERE-rejected rows in the table before you group anything.
- Two counts per group: COUNT(*) counts rows and COUNT(col) counts non-NULL values. Questions love the gap between them.
- An aggregate with no GROUP BY always returns exactly one row, even on an empty table.
- "Second highest" is MAX of the values below the MAX.
- For correlated subqueries, pre-compute the per-group value once (as in the city average table), then scan the rows.
Common mistakes
- Dividing by all rows when computing AVG over a column with NULLs.
- Expecting
WHERE amount <> 100orNOT IN (100, 150)to return the NULL row. It never does. - Applying HAVING to rows rather than groups, or putting an aggregate in WHERE.
- Forgetting that
LIMIT 1silently drops tied rows. - Assuming the output order without an ORDER BY. Without it, the order is not guaranteed.
- Treating an alias defined in SELECT as usable in WHERE. Standard SQL does not allow it.
Practice set
Use the orders table above.
SELECT COUNT(DISTINCT city) FROM orders;SELECT city, MAX(amount) FROM orders GROUP BY city HAVING MAX(amount) < 300;SELECT COUNT(*) FROM orders WHERE amount < 200;SELECT cust FROM orders GROUP BY cust HAVING COUNT(*) = 2;SELECT SUM(amount) FROM orders WHERE city = 'ISB';SELECT MAX(amount) FROM orders WHERE amount < (SELECT MAX(amount) FROM orders);SELECT city FROM orders GROUP BY city ORDER BY AVG(amount) DESC LIMIT 1;SELECT cust, SUM(CASE WHEN amount >= 200 THEN 1 ELSE 0 END) FROM orders GROUP BY cust;What is Ali's value?SELECT COUNT(*) FROM orders WHERE amount NOT IN (100, 150);
Show answers
- 3 (ISB, KHI and LHR).
- ISB 50. LHR's maximum is exactly 300, which is not below 300.
- 3 (150, 100 and 50). The NULL row fails the comparison.
- Ali, Omar and Sara. Omar's NULL order still counts as a row.
- 50. SUM skips the NULL.
- 300.
- KHI (266.67).
- 2 (300 and 200).
- 5. The NULL is excluded, as are 100 and 150.
Practise: 20 questions
FAQs
Why does COUNT(*) differ from COUNT(column)?
COUNT(*) counts rows, whatever they contain. COUNT(column) counts only the rows where that column is not NULL. COUNT(DISTINCT column) also skips NULLs and then counts unique values. Exam tables almost always include a NULL precisely to test this difference.
Can a subquery return more than one column?
Yes, in FROM (a derived table), in EXISTS, and in row comparisons such as (a, b) IN (SELECT x, y ...), which most systems support. A scalar subquery used as a single value must return one column and at most one row. More than one row raises an error in standard SQL.
Is the order of rows guaranteed without ORDER BY?
No. Without ORDER BY, the DBMS may return rows in any order, and the order can change between runs or versions. GROUP BY does not promise sorted output either. An exam question that asks for "the second row" must have an ORDER BY to be well defined.
Do subqueries run once or once per row?
An uncorrelated subquery logically runs once, and its result is reused. A correlated subquery logically runs once for each outer row, because it uses that row's values. Optimisers often rewrite correlated subqueries as joins, but the result must be the same as the row-by-row meaning.
Next step
Go back to the official SQL topic, then practise SQL joins with the same tracing method. Browse the Databases MCQs hub or take an NSCT mock test.