user2166307
user2166307

Reputation:

SQL query to get max id

I have a table named test in which two fields and there data is listed as below

id    test_no
---   -------
1     2
2     2
3     2
4     2 
5     3
6     3

now I want to get maximum test_no in my case I wan to get now 3 and 3 (because there are two counts of 3) so I use this query but it gives me 3 and 2 I want this

id    test_no
---   -------
5     3
6     3

my query is

SELECT MAX( `test_no` )
FROM `test`
GROUP BY `test_no`

Upvotes: 1

Views: 89

Answers (3)

hedgesky
hedgesky

Reputation: 3311

You could try this query:

select * from test 
where test_no = (select max(test_no) from test)

Upvotes: 0

Manoj
Manoj

Reputation: 883

try this way :

SELECT * FROM test
WHERE TEST_NO = (SELECT MAX(TEST_NO) FROM test)

Thanks Manoj

Upvotes: 0

Robert
Robert

Reputation: 25753

Try this way:

select `id`,`test_no`
from `tab`
where `test_no` = (
                  SELECT MAX( `test_no` )
                  FROM `test`
                )

Upvotes: 2

Related Questions