Foundations & querying — quiz
14 questions covering this module. Relational concepts, SELECT, filtering, sorting, nulls and duplicates.
What makes a column a primary key?
- It is the first column
- It uniquely identifies each row and is never null
- It is numeric
- It is indexed
Answer: It uniquely identifies each row and is never null — Uniqueness and not-null are the defining properties.
orders.customer_id referencing customers.customer_id is:
- A primary key
- A foreign key
- An index
- A constraint violation
Answer: A foreign key — A column pointing at another table's primary key is a foreign key.
Why avoid SELECT * in production queries?
- It is slower to type
- It breaks silently when the table structure changes
- It is not valid SQL
- It cannot be used with WHERE
Answer: It breaks silently when the table structure changes — It also moves unneeded data and hides intent.
Which correctly renames a column?
- SELECT name = customer_name
- SELECT name AS customer_name
- SELECT name 'customer_name'
- RENAME name TO customer_name
Answer: SELECT name AS customer_name — AS is the standard alias syntax. Single quotes denote string values.
BETWEEN 100 AND 500 includes:
- 101 to 499
- 100 to 500
- 100 to 499
- 101 to 500
Answer: 100 to 500 — BETWEEN is inclusive on both endpoints.
Which LIKE pattern can use an index?
- LIKE '%son'
- LIKE 'Jo%'
- LIKE '%ann%'
- None of them
Answer: LIKE 'Jo%' — Only a trailing wildcard allows a seek; a leading one forces a scan.
city = 'A' OR city = 'B' AND total > 100 returns:
- All A and B rows over 100
- All A rows, plus B rows over 100
- Only B rows over 100
- An error
Answer: All A rows, plus B rows over 100 — AND binds tighter, so it groups with the B condition.
How do you force OR to evaluate first?
- Write it first
- Use parentheses
- Use NOT
- It always does
Answer: Use parentheses — Parentheses override the default precedence.
Why can ORDER BY use a SELECT alias when WHERE cannot?
- Aliases are strings
- ORDER BY is evaluated after SELECT
- WHERE only takes columns
- It is a syntax quirk
Answer: ORDER BY is evaluated after SELECT — Logical processing order: WHERE runs second, SELECT fifth, ORDER BY seventh.
SQL Server's equivalent of LIMIT 10 is:
- LIMIT 10
- TOP 10
- ROWNUM <= 10
- FIRST 10
Answer: TOP 10 — SQL Server uses TOP; Oracle uses FETCH FIRST or ROWNUM.
Which correctly finds rows with no email?
- WHERE email = NULL
- WHERE email IS NULL
- WHERE email = ''
- WHERE NOT email
Answer: WHERE email IS NULL — NULL cannot be compared with =; IS NULL is the test.
AVG over 10 rows where 3 are null divides by:
- 10
- 7
- 3
- It errors
Answer: 7 — Aggregates ignore nulls, so the divisor is the non-null count.
SELECT DISTINCT city, state returns:
- Unique cities only
- Each unique city-state combination
- Unique states only
- An error
Answer: Each unique city-state combination — DISTINCT operates on the full row combination.
A query suddenly returns duplicates. Best first step?
- Add DISTINCT
- Check whether a join is fanning out rows
- Add LIMIT
- Add GROUP BY
Answer: Check whether a join is fanning out rows — DISTINCT hides the symptom while any SUM stays double-counted.