How to get latest 2 records of each group
With MySQL 8

SELECT sn, color, value
FROM (
  SELECT
    sn,
    color,
    value,
    DENSE_RANK() OVER (PARTITION BY color ORDER BY sn) AS r
  FROM table
) AS t
WHERE t.r &lt;= 2;


Using MySQL &lt; 8

You need to use their special variables.. something like this