October 2022
Intermediate to advanced
380 pages
9h 35m
English
The sections that follow describe several ways you can resolve this antipattern and write unambiguous queries.
The most straightforward solution is to eliminate ambiguous columns from the query.
| | SELECT product_id, MAX(date_reported) AS latest |
| | FROM Bugs JOIN BugsProducts USING (bug_id) |
| | GROUP BY product_id; |
The query reveals the date of the latest bug per product, even though it doesn’t report the bug_id corresponding to that latest bug. Sometimes this is enough, so don’t overlook a simple solution.
Modern SQL products implement window functions, which you can use to filter for the first (or last) ...