sql group by and select all columns 1

sql group by and select all columns

select t1.*
from sch.mytable t1
join
(
    SELECT min(id) as id
    FROM sch.mytable 
    GROUP BY key
) t2 on t1.id = t2.id

Here is what the above code is Doing:
1. It’s creating a temporary table (t2) that contains the minimum id for each key.
2. It’s joining the original table (t1) to the temporary table (t2) on the id column.
3. It’s selecting all columns from the original table (t1).

Similar Posts