| SQL - Repository This forum is for you to save your favorite SQL queries in a safe place so you will always know where to find them |
|
|||
|
I came to SQL Recipes looking for this answer; didn't find it here, but I thought I'd come back to leave the answer I eventually figured out.
Records in TableB refer to specific records in TableA, many to one. You want to know the TableA ids that have the most references in TableB to create a top 10 list. TableA - id is primary key TableB - fk is foreign key to TableA; not unique Code:
SELECT `fk` FROM `TableB`
GROUP BY `fk`
ORDER BY COUNT(`fk`) DESC
LIMIT 10;
Hope it helps. |