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
Release: 2025-01-13 14:38:09
Original
651 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template