I'm a beginner in MySQL. I tried to write a query for finding the max N members per group. The following code does the job, which in this case is to get the top 2 persons per country with greatest id:
SELECT co.id, co.person, co.country
FROM person co
WHERE (
SELECT COUNT(*)
FROM person ci
WHERE co.country = ci.country -- controlling grouping column
AND co.id < ci.id -- controlling min or max
) < 2 -- controlling number of return per group
;
However, I was unable to understand why this is working. Could someone please explain to me? It would be great if you can tell me:
- what is the result returned by the inner
SELECT COUNT(*)
? Isn't it just a single number? - why use this particular inner
WHERE ... AND ...
clause? - why the outer
WHERE () < 2
controls the number of return per group?
Thank you so much for your help.
DDLs
CREATE TABLE `person` (
`id` INT(11) NULL DEFAULT NULL,
`country` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_bin',
`person` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_bin'
)
COLLATE='utf8_bin'
ENGINE=InnoDB
;
INSERT INTO `person` (`id`, `country`, `person`) VALUES
(1, 'Austria', 'Sue'),
(2, 'Austria', 'Anie'),
(3, 'Australia', 'John'),
(4, 'Australia', 'Brian'),
(5, 'UK', 'Jim'),
(6, 'UK', 'Tim'),
(7, 'USA', 'David'),
(8, 'USA', 'Mike'),
(9, 'USA', 'Tom'),
(10, 'N. Korea', 'Joe'),
(11, 'N. Korea', 'Hue'),
(12, 'N. Korea', 'Rick'),
(13, 'N. Korea', 'Jamy'),
(14, 'Finland', 'Kimi');