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
Release: 2025-01-14 15:08:56
Original
534 people have browsed it

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

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

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!

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