A question I got on my last interview:
Following is the sql table data:
Column1 Column2 Column3
1 8 9
2 8 7
3 9 5
4 9 6
5 9 2
6 10 3
7 8 1
8 9 4
9 10 8
10 8 10
Asked to write a sql query to show the columns with highest value in column3 for each different value in column 2 without using sub query like following output:
Column1 Column2 Column3
4 9 6
9 10 8
10 8 10
I have tried using group by on column2 as :
SELECT column1,column2, MAX(column3) AS col3 FROM test1 GROUP BY column2
But it is giving following result:
Column1 Column2 Column3
1 8 10
3 9 6
6 10 8
Can anyone please help me to get my query correct ?