I am using MySQL. Here is my schema:
Table b
Column Name | Type | Primary key
id | int | Yes
seq | int | Yes
amt | int
Dummy data
id | seq | amt
1 | 1 | 4000
1 | 2 | 3000
1 | 3 | 2000
1 | 4 | 5000
2 | 1 | 4000
2 | 2 | 3000
3 | 1 | 2000
3 | 2 | 5000
I want to select the record with equivalent id and max value of seq. HERE is my SQL
SELECT b.id, b.seq, b.amt
FROM b
WHERE b.id = 1
AND b.seq =
(SELECT max(b.seq) FROM b WHERE b.id = 1)
But I wonder if there is more elegant way of achieving what I want. For example,
SELECT b.id, b.seq, b.amt
FROM b
WHERE b.id = 1
HAVING b.seq = max(b.seq)
But it doesn't work as expected. It returns 0 rows.