Saturday 15 November 2014

sql query to find second largest value

1>  SELECT MAX( col ) FROM table  WHERE col < ( SELECT MAX( col ) FROM table );

or

2> SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table);

or

3> SELECT `column` FROM `table` GROUP BY `column` ORDER BY `column` DESC LIMIT 1,1;

or

4>SELECT * FROM Table ORDER BY NumericalColumn DESC LIMIT 1 OFFSET 1;

or

5> SELECT * FROM Table ORDER BY NumericalColumn DESC LIMIT (1, 1);

or

6> SELECT `Column` FROM `Table` ORDER BY `Column` DESC LIMIT 1,1;

No comments:

Post a Comment