Functions & transformation — quiz
8 questions covering this module. String, numeric, date and null-handling functions.
Why does a join fail on values that look identical?
- Wrong data type
- Trailing whitespace or case differences
- Missing index
- Too many rows
Answer: Trailing whitespace or case differences — TRIM and case normalisation on import prevent both.
WHERE UPPER(city) = 'HOUSTON' is slow because:
- UPPER is expensive
- The function prevents an index seek
- Strings are slow
- It needs a subquery
Answer: The function prevents an index seek — Wrapping the column in a function blocks the index.
SELECT 7 / 2 in PostgreSQL returns:
- 3.5
- 3
- 4
- An error
Answer: 3 — Integer division discards the remainder. Cast to get 3.5.
Which prevents a division-by-zero error?
- COALESCE(x, 0)
- NULLIF(x, 0)
- ABS(x)
- ROUND(x)
Answer: NULLIF(x, 0) — NULLIF makes the denominator null, so the result is null rather than an error.
Why does BETWEEN '2024-01-01' AND '2024-01-31' miss rows?
- It excludes the first day
- The end date means midnight, excluding most of the 31st
- BETWEEN is exclusive
- Dates cannot use BETWEEN
Answer: The end date means midnight, excluding most of the 31st — With a timestamp column, the end bound is 00:00:00 on the 31st.
Grouping monthly by a formatted string causes:
- An error
- Alphabetical sorting instead of chronological
- Duplicate rows
- Slower joins
Answer: Alphabetical sorting instead of chronological — April sorts before January as text. Truncate to a date instead.
price - discount returns null when discount is null. Fix:
- NULLIF(discount, 0)
- COALESCE(discount, 0)
- ISNULL(price)
- Cast to numeric
Answer: COALESCE(discount, 0) — COALESCE substitutes zero so the arithmetic works.
NULLIF(a, b) returns:
- b when a is null
- null when a equals b, otherwise a
- The first non-null
- Always null
Answer: null when a equals b, otherwise a — It generates a null on a match — the opposite job to COALESCE.