user3329081 user3329081 - 5 months ago 12
SQL Question

How to write the SQL query?

Suppose I have the following table, How can I write the SQL query to find a set of results which are the minimum c for each b? for example, I want (7, 45) and (16, 69) for the following table.

a | b | c
-----+--------+----
3 | 7 | 53
2 | 7 | 49
1 | 7 | 45
5 | 7 | 61
13 | 16 | 69
15 | 16 | 77
6 | 7 | 65
4 | 7 | 57
14 | 16 | 73

Answer

Just use GROUP BY:

SELECT b, MIN(c)
FROM yourTable
GROUP BY b
Comments