Combining data — quiz
12 questions covering this module. Every join type, multi-table joins, and set operations.
Joining orders to line_items inflates SUM(total). Why?
- The join is wrong type
- Each order repeats once per line item
- Nulls are counted
- Missing index
Answer: Each order repeats once per line item — The one-to-many relationship duplicates the order row.
The reliable fix is to:
- Add DISTINCT
- Pre-aggregate line_items before joining
- Use RIGHT JOIN
- Add an index
Answer: Pre-aggregate line_items before joining — DISTINCT would not fix an already-inflated SUM.
An INNER JOIN between orders and customers excludes:
- Nothing
- Customers with no orders, and orders with no matching customer
- Only null rows
- Duplicate rows
Answer: Customers with no orders, and orders with no matching customer — Anything without a match on both sides is dropped.
JOIN with no keyword prefix means:
- LEFT JOIN
- INNER JOIN
- FULL JOIN
- CROSS JOIN
Answer: INNER JOIN — INNER is the default.
LEFT JOIN with WHERE right.status = 'x' behaves like:
- LEFT JOIN
- INNER JOIN
- CROSS JOIN
- FULL OUTER JOIN
Answer: INNER JOIN — The NULL-extended rows fail the WHERE and are discarded.
After a LEFT JOIN, a customer with no orders and COUNT(*) shows:
- 0
- 1
- NULL
- An error
Answer: 1 — COUNT(*) counts the null-extended row. Use COUNT(o.order_id) for 0.
Which join keeps unmatched rows from both tables?
- INNER
- LEFT
- FULL OUTER
- CROSS
Answer: FULL OUTER — FULL OUTER preserves both sides.
A query unexpectedly returns millions of rows. Check first for:
- A missing index
- A missing join condition
- A wrong data type
- Too many columns
Answer: A missing join condition — A missing ON produces a Cartesian product.
An INNER JOIN placed after a LEFT JOIN can:
- Speed up the query
- Eliminate the rows the LEFT JOIN preserved
- Change column order
- Have no effect
Answer: Eliminate the rows the LEFT JOIN preserved — The inner join requires a match, discarding null-extended rows.
To find which join causes extra rows:
- Add DISTINCT
- Add joins one at a time and count after each
- Use EXPLAIN only
- Reorder the SELECT
Answer: Add joins one at a time and count after each — Incremental counting isolates the offending join.
Which is faster and why?
- UNION, it does less work
- UNION ALL, it skips deduplication
- They are identical
- Depends on indexes
Answer: UNION ALL, it skips deduplication — Deduplication needs a sort or hash across the whole result.
Which returns rows in A but not in B?
- UNION
- INTERSECT
- EXCEPT
- UNION ALL
Answer: EXCEPT — EXCEPT (MINUS in Oracle) is the difference operator.