Home >Database >Mysql Tutorial >How Can I Retrieve Only Unique Records Using SQL's DISTINCT Keyword?

How Can I Retrieve Only Unique Records Using SQL's DISTINCT Keyword?

Susan Sarandon
Susan SarandonOriginal
2025-01-14 15:08:56535browse

How Can I Retrieve Only Unique Records Using SQL's DISTINCT Keyword?

Using SQL's DISTINCT Keyword to Retrieve Unique Data

Dealing with duplicate entries in SQL queries can be problematic when you need unique results. The DISTINCT keyword provides a simple solution for filtering out these duplicates and returning only unique values.

To utilize DISTINCT, incorporate it into your SELECT statement before the column name(s) you want unique results from. For example, if you need unique entries from the "item" column in a table:

<code class="language-sql">SELECT DISTINCT item
FROM table_name;</code>

This query would yield the following unique items:

ID Item
1 item1
2 item2
3 item3

You can also specify multiple columns to retrieve unique combinations based on all selected columns:

<code class="language-sql">SELECT DISTINCT item, data
FROM table_name;</code>

This would return unique pairings of "item" and "data":

ID Item Data
1 item1 data1
2 item2 data3
3 item3 data4

The above is the detailed content of How Can I Retrieve Only Unique Records Using SQL's DISTINCT Keyword?. 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