How can I find every row tied for the highest value in a group?
Quick answer
First calculate the maximum value for each group, then join those maxima back to the original table using both the group key and the value. This returns every original row matching its group’s maximum, including ties. A grouped MAX() by itself returns one summary row per group, not all the records that reached that value. In SQLite, avoid relying on a bare column beside MAX() to choose tied rows; it may select an arbitrary one. [S1]
The question, the details, the next step.
A practical checklist
Identify the table, group key, value column, and record fields you need.
Group by every column that distinguishes one group from another.
Join the maximum back using both the group key and the maximum value.
Keep identifying fields in the output if tied records could otherwise look alike.
Add
ORDER BYwhen readers need a predictable display order.
A few more useful answers
Why must the join include the group key as well as the maximum value?
Different groups can share the same maximum number. Matching on the group key and value ensures a row is compared only with its own group’s maximum.
Does the query return one row or several when there is a tie?
It returns every matching original row. A tie at the maximum therefore produces multiple rows for that group.
Can I use this pattern for more than one grouping column?
Yes. Include all grouping columns in the subquery’s GROUP BY and match each of them in the join, along with the value.
Sources
References used for this guide.
- [S1] SQLite SELECT: aggregate queries and maximum-value ties sqlite.org · Checked
