Query Processing & Optimization MCQs for NSCT

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

Query processing is how a DBMS turns an SQL statement into a result. It parses the query, the optimiser picks the cheapest of many equivalent execution plans, and the engine runs that plan. It is topic 13 of the Databases area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 42.3% in Databases, per the HEC NSCT report and APP.

This topic builds on relational algebra, because plans are algebra trees, and on indexing, because indexes are the access paths a plan chooses between. The formulas below follow Silberschatz (Database System Concepts). Elmasri-Navathe uses the same ideas with slightly different notation.

Execution plans

An execution (evaluation) plan is a relational algebra expression in which every operation carries an algorithm and an access path. Query processing has three steps: parsing and translation, then optimisation, then evaluation.

Two plans for "names of CS students taking DB" might be:

Plan Steps
A student × takes, then σdept='CS' ∧ course='DB', then πname
B σdept='CS'(student) using an index, then join with σcourse='DB'(takes) by hash join, then πname

Both give the same answer. Plan B is far cheaper because it filters before joining. Heuristic optimisers apply rules like this one: push selections and projections down, and avoid Cartesian products. Cost-based optimisers (System R style) estimate the cost of many plans and keep the cheapest. Intermediate results are either materialised, meaning written to temporary relations, or pipelined straight to the parent operator. Sorting, the build phase of a hash join, and aggregation are blocking operators.

Exam angle: know which equivalence rules are valid. σ distributes over ∪ and −, and π distributes over ∪, but πA(R − S) ≠ πA(R) − πA(S) in general. You can also push a selection below a join only if it uses attributes of one side alone.

Cost estimation

Silberschatz counts block transfers (time tT each) and disk seeks (tS each). The estimates use catalogue statistics: n_r (number of tuples), b_r (number of blocks) and V(A, r) (number of distinct values of A).

Expression Estimated size
σA=v(r) n_r / V(A, r)
σA≥v(r), uniform values n_r × (max − v) / (max − min)
σθ1∧θ2(r) n_r × (s1/n_r) × (s2/n_r)
σθ1∨θ2(r) n_r × (1 − (1 − s1/n_r)(1 − s2/n_r))
r ⋈ s, with the common attribute a key of r at most n_s (exactly n_s with a non-null foreign key)
r ⋈ s on A, no keys min(n_r·n_s / V(A, s), n_r·n_s / V(A, r))

Example: a relation of 10,000 tuples where dept = 'CS' matches 500 and year = 4 matches 2,000 gives 100 tuples for AND and 2,400 for OR. Exam angle: a disjunction is not s1 + s2, because that counts the overlap twice.

Join algorithms

Here b_r and b_s are block counts, n_r is the tuple count, r is the outer relation and M is the number of memory blocks.

Algorithm Block transfers (Silberschatz) Works for
Nested-loop (worst case) n_r × b_s + b_r Any condition
Nested-loop (inner fits in memory) b_r + b_s Any condition
Block nested-loop (worst case) b_r × b_s + b_r Any condition
Block nested-loop with M buffers ⌈b_r / (M − 2)⌉ × b_s + b_r Any condition
Merge join (inputs already sorted) b_r + b_s, plus sorting cost if unsorted Equi-joins and natural joins
Hash join (no recursive partitioning) 3(b_r + b_s), plus 4n_h for partly filled blocks Equi-joins and natural joins

With enrol (6,000 tuples, 300 blocks) as outer and course (40 blocks) as inner:

Method Cost
Nested-loop 6,000 × 40 + 300 = 240,300 transfers
Block nested-loop 300 × 40 + 300 = 12,300 transfers
Hash join 3 × 340 = 1,020 transfers

Exam angle: for block nested-loop, put the smaller relation on the outside. A hash join avoids recursive partitioning roughly when M > √b_s, where s is the build input.

Common traps

Worked examples

Understanding. Which operator blocks a pipeline: selection, projection without DISTINCT, or sort? Answer: sort. It cannot output its first tuple until it has read all of its input. Selection and plain projection pass tuples along as they arrive.

Application. r has 1,000 tuples and s has 4,000. They join on B, which is a key of neither. V(B, r) = 20 and V(B, s) = 80. Estimate |r ⋈ s|. Answer: 50,000. min(4,000,000 / 80, 4,000,000 / 20) = min(50,000, 200,000) = 50,000. Divide by the larger distinct-value count.

Analysis. r has 400 blocks and s has 60 blocks, both unsorted, with M = 32. Which is cheaper: block nested-loop with s as outer, or hash join? Answer: block nested-loop, 860 transfers. s outer: ⌈60/30⌉ × 400 + 60 = 2 × 400 + 60 = 860. Hash join: 3 × (400 + 60) = 1,380. M = 32 exceeds √60, so the hash join needs no recursive partitioning, but nested-loop still wins because s needs only two chunks. With M = 12 the result flips: block nested-loop needs ⌈60/10⌉ × 400 + 60 = 2,460 transfers, so the 1,380-transfer hash join wins.

Practise

The quiz below has 100 Query Processing & Optimization questions, which you can filter by tier and difficulty.

Practise: 100 questions

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

  1. In the order a DBMS performs them, which are the three main steps of query processing?

    1. Optimisation, parsing and translation, evaluation
    2. Evaluation, optimisation, parsing and translation
    3. Compilation, linking, execution
    4. Parsing and translation, optimisation, evaluation
    Show answer

    Answer: D. Parsing and translation, optimisation, evaluation

    The parser checks syntax and translates the SQL into an internal form such as relational algebra. The optimiser then picks a low-cost evaluation plan, and the evaluation engine runs that plan and returns the result. Compilation and linking belong to program builds, not query processing.

  2. What is a query evaluation plan (execution plan)?

    1. The SQL text after the parser has removed comments and spaces
    2. A list of the tables a query reads, in alphabetical order
    3. The final result table that the query returns to the user
    4. A relational algebra expression annotated with the algorithm and access path for each operation
    Show answer

    Answer: D. A relational algebra expression annotated with the algorithm and access path for each operation

    A plan says not only which operations run (selection, join, projection) but how: for example 'use the B+-tree index on dept for the selection, then a hash join'. Two plans for the same query give the same answer but can differ hugely in cost.

  3. Silberschatz measures the cost of a query evaluation plan mainly by which quantity?

    1. The number of rows in the final answer
    2. The number of disk block transfers and disk seeks
    3. The number of clauses in the SQL statement
    4. The number of CPU registers the query uses
    Show answer

    Answer: B. The number of disk block transfers and disk seeks

    Disk I/O dominates query cost for disk-resident data, so Silberschatz counts block transfers (each taking time tT) and seeks (each taking tS). CPU cost is real but usually left out for simplicity. The size of the answer or of the SQL text does not measure the work done.

  4. Which rule is a standard heuristic in query optimisation?

    1. Perform Cartesian products before selections
    2. Perform selection operations as early as possible
    3. Delay projections until after every join
    4. Always join the two largest relations first
    Show answer

    Answer: B. Perform selection operations as early as possible

    Pushing selections down the tree shrinks intermediate results early, so later joins process fewer tuples. Heuristic optimisers also push projections down and avoid Cartesian products. Joining the largest relations first usually makes intermediates bigger, not smaller.

  5. In query evaluation, what does pipelining mean?

    1. Running each operation to completion and storing its full result on disk
    2. Passing tuples from one operation straight to its parent without writing a temporary relation to disk
    3. Splitting one query across several CPUs that each hold a full copy of the data
    4. Sorting every relation before any operation starts
    Show answer

    Answer: B. Passing tuples from one operation straight to its parent without writing a temporary relation to disk

    With pipelining, the output of, say, a selection feeds directly into the next join or projection as it is produced. This saves the cost of writing and re-reading temporary relations, which materialised evaluation pays for every intermediate result.

  6. Which join algorithm can evaluate any join condition, including a non-equality condition such as r.a < s.b?

    1. Hash join
    2. Merge (sort-merge) join
    3. Nested-loop join
    4. Grace hash join
    Show answer

    Answer: C. Nested-loop join

    Nested-loop join simply tests the condition on every pair of tuples, so it works for any theta condition. Hash join (including the Grace variant) and merge join rely on matching equal values of the join attributes, so they handle equi-joins and natural joins only.

  7. In the catalogue statistics used for cost estimation, what does V(A, r) denote?

    1. The number of tuples in relation r
    2. The number of distinct values of attribute A in relation r
    3. The number of blocks holding relation r
    4. The size in bytes of one tuple of r
    Show answer

    Answer: B. The number of distinct values of attribute A in relation r

    V(A, r) is the number of distinct values that appear for A in r. The other statistics are n_r (number of tuples), b_r (number of blocks) and l_r (tuple size in bytes). V(A, r) drives the estimate for an equality selection, n_r / V(A, r).

  8. What does a cost-based query optimiser try to do?

    1. Find the plan that is provably the fastest on every run
    2. Choose the equivalent plan with the lowest estimated cost
    3. Rewrite the query so it returns fewer rows
    4. Choose the plan with the fewest operators regardless of cost
    Show answer

    Answer: B. Choose the equivalent plan with the lowest estimated cost

    The optimiser generates equivalent plans and picks the one with the lowest estimated cost, using catalogue statistics. Estimates can be wrong, so the chosen plan is not provably the fastest. It must never change the result, so returning fewer rows is not allowed.

  9. In materialised evaluation, what happens to the result of each intermediate operation?

    1. It is sent to the user as soon as the first tuple is produced
    2. It is discarded and recomputed whenever the parent needs a tuple
    3. It is converted into an index on the base relation
    4. It is stored as a temporary relation, usually on disk, before the parent operation reads it
    Show answer

    Answer: D. It is stored as a temporary relation, usually on disk, before the parent operation reads it

    Materialisation evaluates the operator tree bottom-up and writes each intermediate result to a temporary relation, which the parent then reads. Its cost is the cost of the operations plus writing and re-reading those temporaries, which pipelining avoids.

  10. Which operation is blocking, meaning it cannot output its first result tuple until it has read all of its input?

    1. Sorting
    2. Selection on a single relation
    3. Projection without duplicate elimination
    4. UNION ALL
    Show answer

    Answer: A. Sorting

    A sort cannot know which tuple comes first until it has seen every tuple, so it blocks the pipeline. Selection, projection without DISTINCT and UNION ALL can each pass tuples onwards one at a time as they arrive.

FAQs

What is the difference between heuristic and cost-based optimisation?

Heuristic optimisation applies fixed rules, such as performing selections early, pushing projections down and avoiding Cartesian products, without measuring cost. Cost-based optimisation lists alternative plans, estimates each one's I/O cost from catalogue statistics, and picks the cheapest. Real optimisers combine both: heuristics prune the search, and costs pick between the plans that remain.

When is merge join better than hash join?

When both inputs are already sorted on the join attribute, or when sorted output is useful later (an "interesting order" for ORDER BY or another merge join). Merge join then costs just b_r + b_s, while hash join costs about 3(b_r + b_s). For unsorted inputs, hash join is usually cheaper.

How can I see a query's execution plan?

Most DBMSs show it with EXPLAIN. In SQLite, EXPLAIN QUERY PLAN prints lines such as "SCAN emp" for a full scan or "SEARCH emp USING INDEX idx_dept (dept=?)" for an index lookup. PostgreSQL and MySQL use EXPLAIN with their own formats, and EXPLAIN ANALYZE also runs the query.

Next step

Continue with Database Security MCQs, go back to the Databases hub, or try a timed NSCT mock test.