SQL Query Output Questions for the NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

  1. FROM and joins. Build the working set of rows.
  2. WHERE. Remove rows. Any comparison with NULL is UNKNOWN, and UNKNOWN rows are dropped.
  3. GROUP BY. Put the surviving rows into groups. All NULLs in the grouping column form a single group.
  4. Aggregates. Compute them per group. Every aggregate except COUNT(*) skips NULLs.
  5. HAVING. Remove whole groups.
  6. SELECT, then DISTINCT. Choose the output columns.
  7. 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

Common mistakes

Practice set

Use the orders table above.

  1. SELECT COUNT(DISTINCT city) FROM orders;
  2. SELECT city, MAX(amount) FROM orders GROUP BY city HAVING MAX(amount) < 300;
  3. SELECT COUNT(*) FROM orders WHERE amount < 200;
  4. SELECT cust FROM orders GROUP BY cust HAVING COUNT(*) = 2;
  5. SELECT SUM(amount) FROM orders WHERE city = 'ISB';
  6. SELECT MAX(amount) FROM orders WHERE amount < (SELECT MAX(amount) FROM orders);
  7. SELECT city FROM orders GROUP BY city ORDER BY AVG(amount) DESC LIMIT 1;
  8. SELECT cust, SUM(CASE WHEN amount >= 200 THEN 1 ELSE 0 END) FROM orders GROUP BY cust; What is Ali's value?
  9. SELECT COUNT(*) FROM orders WHERE amount NOT IN (100, 150);
Show answers
  1. 3 (ISB, KHI and LHR).
  2. ISB 50. LHR's maximum is exactly 300, which is not below 300.
  3. 3 (150, 100 and 50). The NULL row fails the comparison.
  4. Ali, Omar and Sara. Omar's NULL order still counts as a row.
  5. 50. SUM skips the NULL.
  6. 300.
  7. KHI (266.67).
  8. 2 (300 and 200).
  9. 5. The NULL is excluded, as are 100 and 150.

Practise: 20 questions

Interactive quiz loading. The first 10 questions are shown below.

  1. How many rows (groups) does this query return?

    CREATE TABLE emp (name TEXT, dept TEXT);
    INSERT INTO emp VALUES ('A','IT'), ('B',NULL), ('C','HR'), ('D',NULL), ('E','IT'), ('F','Ops');
    SELECT dept, COUNT(*) FROM emp GROUP BY dept;
    1. 4
    2. 3
    3. 5
    4. 6
    Show answer

    Answer: A. 4

    GROUP BY treats all NULLs in the grouping column as one group, even though NULL = NULL is not TRUE in a WHERE clause. The groups are IT, HR, Ops and NULL, so 4 rows. B and D form the NULL group with a count of 2.

  2. The scalar subquery finds no salary above 100. What does the query show in the top column?

    CREATE TABLE emp (name TEXT, salary INT);
    INSERT INTO emp VALUES ('A', 40), ('B', 60);
    SELECT name, (SELECT MAX(salary) FROM emp WHERE salary > 100) AS top FROM emp WHERE name = 'A';
    1. NULL
    2. 0
    3. An error: the subquery returned no rows
    4. 40
    Show answer

    Answer: A. NULL

    MAX over zero rows returns NULL. Even a scalar subquery with no aggregate that returns no rows gives NULL rather than an error. An error occurs only when a scalar subquery returns more than one row.

  3. Every value in column x is NULL. What do COUNT(x) and SUM(x) return?

    CREATE TABLE t (x INT);
    INSERT INTO t VALUES (NULL), (NULL);
    SELECT COUNT(x), SUM(x) FROM t;
    1. 0 and 0
    2. NULL and NULL
    3. 0 and NULL
    4. 2 and NULL
    Show answer

    Answer: C. 0 and NULL

    COUNT(column) counts non-NULL values, so it returns 0. SUM, AVG, MIN and MAX over no non-NULL values return NULL, not 0. Use COALESCE(SUM(x), 0) if you need a zero. COUNT(*) would give 2.

  4. The table is empty and there is no GROUP BY. What does the query return?

    CREATE TABLE t (x INT);
    SELECT COUNT(*), MAX(x) FROM t;
    1. No rows
    2. One row: 0 and 0
    3. One row: NULL and NULL
    4. One row: 0 and NULL
    Show answer

    Answer: D. One row: 0 and NULL

    An aggregate query without GROUP BY always returns exactly one row, even over an empty table. COUNT(*) is 0 and MAX(x) is NULL. With GROUP BY x, an empty table would give no rows at all.

  5. What does ORDER BY 2 DESC sort by, and which name comes first?

    CREATE TABLE p (name TEXT, city TEXT, age INT);
    INSERT INTO p VALUES ('Ali','LHR',22), ('Sara','KHI',25), ('Omar','ISB',21);
    SELECT name, age FROM p ORDER BY 2 DESC;
    1. The second column of the table (city); Omar first
    2. The constant 2, so the order is unchanged; Ali first
    3. The second row; Sara first
    4. The second column in the SELECT list (age); Sara first
    Show answer

    Answer: D. The second column in the SELECT list (age); Sara first

    An integer in ORDER BY refers to a position in the SELECT list, not the table. Column 2 of the result is age, so descending order puts Sara (25) first, then Ali (22), then Omar (21).

  6. There is HAVING but no GROUP BY. What does the query return?

    CREATE TABLE t (x INT);
    INSERT INTO t VALUES (5), (10), (15);
    SELECT SUM(x) FROM t HAVING SUM(x) > 100;
    1. One row: 30
    2. No rows
    3. One row: NULL
    4. An error: HAVING needs GROUP BY
    Show answer

    Answer: B. No rows

    With no GROUP BY, the whole table is one group. SUM(x) = 30 fails HAVING SUM(x) > 100, so that single group is removed and the result is empty. Standard SQL allows HAVING without GROUP BY. With > 20 you would get one row, 30.

  7. What does the query return?

    CREATE TABLE orders (oid INT, cust TEXT, city TEXT, amount INT);
    INSERT INTO orders VALUES
     (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);
    SELECT city, COUNT(*) FROM orders GROUP BY city ORDER BY city;
    1. ISB 1, KHI 3, LHR 3
    2. ISB 2, KHI 2, LHR 3
    3. KHI 3, LHR 3
    4. ISB 2, KHI 3, LHR 3
    Show answer

    Answer: D. ISB 2, KHI 3, LHR 3

    COUNT(*) counts rows, including Omar's order with a NULL amount, so ISB has 2. KHI has orders 2, 5 and 6, and LHR has 1, 3 and 7. ORDER BY city sorts them alphabetically.

  8. What does the query return?

    CREATE TABLE marks (sid TEXT, course TEXT, score INT);
    INSERT INTO marks VALUES ('S1','DB',72), ('S1','OS',45), ('S2','DB',88),
     ('S2','OS',91), ('S3','DB',39), ('S3','OS',58), ('S3','AI',64);
    SELECT sid, AVG(score) FROM marks
    WHERE score >= 50
    GROUP BY sid HAVING COUNT(*) >= 2
    ORDER BY sid;
    1. S1 58.5, S2 89.5 and S3 53.67
    2. S2 89.5 and S3 61.0
    3. S2 89.5 only
    4. S1 72.0, S2 89.5 and S3 61.0
    Show answer

    Answer: B. S2 89.5 and S3 61.0

    WHERE drops the rows with scores below 50 (S1-OS 45 and S3-DB 39) before grouping. S1 is left with one row and fails COUNT(*) >= 2. S2 averages (88 + 91) / 2 = 89.5 and S3 averages (58 + 64) / 2 = 61.0.

  9. Which order ids are returned?

    CREATE TABLE orders (oid INT, cust TEXT, city TEXT, amount INT);
    INSERT INTO orders VALUES
     (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);
    SELECT oid FROM orders WHERE amount > (SELECT AVG(amount) FROM orders) ORDER BY oid;
    1. 1 and 5
    2. 1, 3, 5 and 6
    3. 1, 5 and 6
    4. 5 only
    Show answer

    Answer: C. 1, 5 and 6

    AVG ignores the NULL: 1450 / 7 ≈ 207.14, not 1450 / 8. The amounts above 207.14 are 300 (oid 1), 400 (oid 5) and 250 (oid 6). Dividing by 8 would give 181.25 and wrongly include 200 (oid 3).

  10. Which names does the correlated subquery return?

    CREATE TABLE emp (name TEXT, dept TEXT, salary INT);
    INSERT INTO emp VALUES ('Ali','IT',90), ('Sara','IT',120), ('Omar','HR',70),
     ('Hina','HR',70), ('Zain','Ops',55);
    SELECT e.name FROM emp e
    WHERE e.salary = (SELECT MAX(salary) FROM emp i WHERE i.dept = e.dept)
    ORDER BY e.name;
    1. Omar, Sara and Zain
    2. Sara only
    3. Hina, Omar, Sara and Zain
    4. Hina, Sara and Zain
    Show answer

    Answer: C. Hina, Omar, Sara and Zain

    For each employee, the subquery finds the top salary in that employee's own department. IT's top is 120 (Sara), HR's is 70 (shared by Omar and Hina) and Ops's is 55 (Zain). Ties are all returned, giving 4 names.

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.