Home >Database >Mysql Tutorial >How to Find the Second Largest Value in a SQL Column with Duplicates?

How to Find the Second Largest Value in a SQL Column with Duplicates?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-13 14:38:09653browse

How to Find the Second Largest Value in a SQL Column with Duplicates?

Extracting the Second Highest Value from a SQL Column

This guide demonstrates how to retrieve the second largest integer value from a specific table column, handling potential duplicate values.

A concise and efficient SQL query achieves this:

<code class="language-sql">SELECT MAX(col)
FROM table
WHERE col < (SELECT MAX(col) FROM table);</code>

This query leverages a subquery to find the maximum value and then uses the MAX function again to find the largest value that is strictly less than the maximum. This approach effectively addresses the issue of duplicate maximum values, ensuring accurate retrieval of the second largest value.

The above is the detailed content of How to Find the Second Largest Value in a SQL Column with Duplicates?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn