Home > Database > Mysql Tutorial > How Can DISTINCT in SQL Help Me Select Only Unique Records?

How Can DISTINCT in SQL Help Me Select Only Unique Records?

Barbara Streisand
Release: 2025-01-14 15:09:46
Original
342 people have browsed it

How Can DISTINCT in SQL Help Me Select Only Unique Records?

Using DISTINCT in SQL for Unique Record Selection

Standard SQL SELECT * FROM table queries often return duplicate rows. To ensure data accuracy and efficient analysis, SQL provides the DISTINCT keyword. This allows you to retrieve only unique values from specified columns.

Illustrative Example:

Consider this sample table:

<code>| Column1 | Column2 | Column3 |
|---------|---------|---------|
| 1        | item1    | data1   |
| 2        | item1    | data2   |
| 3        | item2    | data3   |
| 4        | item3    | data4   |</code>
Copy after login

A SELECT * query would return all rows, including duplicates based on Column2.

To retrieve only unique rows, use the DISTINCT keyword:

<code class="language-sql">SELECT DISTINCT Column1, Column2, Column3
FROM table;</code>
Copy after login

This produces the following result, eliminating the duplicate item1 entry:

Column1 Column2 Column3
1 item1 data1
3 item2 data3
4 item3 data4

The DISTINCT keyword is a powerful tool for data cleaning and ensuring the accuracy of your SQL queries. It guarantees that only unique combinations of the specified columns are returned.

The above is the detailed content of How Can DISTINCT in SQL Help Me Select Only Unique Records?. 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