https://towardsdatascience.com/mysql-how-to-write-a-query-that-returns-the-top-records-in-a-group-12865695f436 SET @group_rank := NULL; SELECT * FROM ( SELECT *, @group_hash := CONCAT(`column1`, '-', `column2`) AS _group_hash, @group_rank := IF(@group_value = @group_hash, @group_rank + 1, 1) AS _group_rank, @group_value := @group_hash AS _group_value FROM `your_table` AS `_your_table` ORDER BY `column1` ASC, `column2` ASC ) AS `_sub_table` WHERE `_sub_table`.`_group_rank` <= 2 ;