21 May 2012

Mysql Interview Questions

 

Write a simple query to get the second largest number of a table column?

 

               There are many ways that we can write this,
But the simplest way in mysql is,
  • SELECT * FROM country ORDER BY id DESC LIMIT 1, 1
plus there are some other methods as well,
  • SELECT * FROM country ORDER BY id DESC LIMIT 1 OFFSET 1
  • select max(id) from country where id not in(select max(id) from country)
  • SELECT min(id) FROM (SELECT DISTINCT id FROM country ORDER BY id DESC LIMIT 2) AS t
Only in sql server
  • SELECT min([column]) AS [column]
    FROM (
    SELECT TOP 2 [column]
    FROM [Table]
    GROUP BY [column]
    ORDER BY [column] DESC
    ) a

No comments:

Post a Comment