Home >Database >Mysql Tutorial >How to Find Distinct Rows with Minimum Values in SQL?

How to Find Distinct Rows with Minimum Values in SQL?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-24 08:08:10323browse

How to Find Distinct Rows with Minimum Values in SQL?

Find the only row with the minimum value in SQL

When processing the data in the relationship database, it is usually necessary to retrieve a line with minimum value in a specific column. This is especially useful when you want to identify the minimum value associated with different identifiers.

To achieve this purpose in SQL, you can use the following methods:

This query effectively retrieves all lines with the minimum point value of the minimum point value of its respective identification (ID) of the Tableneame table (alias TBL).
<code class="language-sql">SELECT tbl.*
FROM TableName tbl
INNER JOIN
(
    SELECT Id, MIN(Point) MinPoint
    FROM TableName
    GROUP BY Id
) tbl1
ON tbl1.id = tbl.id
WHERE tbl1.MinPoint = tbl.Point</code>

Let's use the table provided to explain this concept:

Execute the query with this table will produce the following results:
<code>id  game   point
1    x      5
1    z      4
2    y      6
3    x      2
3    y      5
3    z      8</code>

As you can see, the query successfully identifies those lines with minimum dots in each game.
<code>id  game   point    
1    z      4
2    y      6
3    x      2   </code>

Now, whenever you need to find those rows with the lowest value in specific columns (groups according to one or more identifiers), this technology can be used.

The above is the detailed content of How to Find Distinct Rows with Minimum Values in SQL?. 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