Chrome Extension

Select second highest column value MySQL

You can find the second-highest value from a table column in MYSQL by ordering the column values in descending order and using LIMIT and OFFSET.
SELECT
    (SELECT DISTINCT
            column_name
        FROM
            table_name
        ORDER BY column_name DESC
        LIMIT 1 OFFSET 1
    ) AS second_highest_value
;
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

For Example, If there is a table named subject_scores,  and we want to find the second-highest score from the column 'score'.

+----+--------+
| Id | score  |
+----+--------+
| 1  | 10     |
| 2  | 30     |
| 3  | 20     |
| 3  | 50     |
+----+--------+

To get the second-highest score from the above table the query will be

SELECT
   (SELECT DISTINCT
          score
       FROM
          subject_scores
       ORDER BY score DESC
       LIMIT 1 OFFSET 1
   ) AS second_highest_score
;

The output from the above table will be:

+----------------------+
| second_highest_score |
+----------------------+
| 20                   |
+----------------------+

Search Index Data (The code snippet can also be found with below search text)

Find second highest value form column MYSQL
Was this helpful?
0 Comments