Query Processing & Optimization MCQs for NSCT
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
- Using n_r × b_s (tuple nested-loop) when the question says block nested-loop.
- Adding the selectivities of an OR condition instead of using 1 − (1 − s1/n)(1 − s2/n).
- Assuming an index is always faster. With many unclustered matches, a secondary index needs about one seek per record and loses to a linear scan.
- Using hash or merge join for a non-equality condition such as r.a < s.b. Only nested-loop variants handle it.
- Confusing (2(n−1))!/(n−1)! join orders (120 for n = 4) with n! left-deep orders (24).
- Treating the optimiser's plan as provably the fastest. It is only the cheapest estimated plan.
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
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.