Predict a query's row count, then run it
Task
Write four queries against the practice database, and before running each one, write down how many rows you expect. Then run them and compare. One of the four differs from a near-identical query only by its parentheses, and the prediction is where you notice.
Steps
- Write your predictions in
lab/sql/predictions.txtbefore running anything. - Query A: customers WHERE region = 'North'. Query B: orders WHERE order_date BETWEEN '2025-03-01' AND '2025-03-31'. Query C: SELECT DISTINCT region FROM customers.
- Query D: orders WHERE discount_pct > 0 AND (quantity > 3 OR product_id = 'P001'). Query E: the same without the parentheses.
- Record each query's row count in
lab/sql/results.csvwith headerquery,rows, using the letters A to E.
Verify
These checks run in a POSIX shell with Python 3: Terminal on macOS or Linux, and on Windows Git Bash (it comes with Git for Windows) or WSL. On Windows, python3 is often a Microsoft Store placeholder that prints “Python was not found”; run alias python3=py in Git Bash first.
awk -F, 'NR>1 {print $1": "$2}' lab/sql/results.csv
awk -F, '$1=="D" {d=$2} $1=="E" {e=$2} END {print "the parentheses change the result by "e-d" rows"}' lab/sql/results.csv
A returns 77 customers, B 176 orders, C 4 regions, D 298 orders and E 343. The 45-row difference between D and E is the parentheses: without them, AND binds first and every P001 order is included whatever its discount.
This is an independent study companion for CompTIA Data+ DA0-002 and is not produced by or endorsed by CompTIA.