SQL Query to Find Unique Column Values From Table
Last Updated :
31 Dec, 2024
Finding unique column values is a common task in SQL when analysing data or ensuring data integrity. By using the DISTINCT
clause, we can efficiently retrieve unique values from a specified column, avoiding duplicate results.
In this article, we will explain the use of the DISTINCT
clause with detailed examples, syntax, and practical use cases. By the end of this guide, we will have a thorough understanding of how to apply the DISTINCT
clause in SQL to improve our data queries and analysis.
What is the DISTINCT
Clause in SQL?
The DISTINCT
clause is a powerful feature in SQL that allows us to fetch unique values from a specified column in a table. It removes duplicate entries from the result set, ensuring that each value appears only once.
Syntax
SELECT DISTINCT column_name
FROM table_name;
Finding Unique Column Values in SQL
Let's look at an example, where we will retrieve unique column values from a table using the DISTINCT clause. We take a sample table named Geeks and return unique column values by applying various SQL queries.
Geeks TableRetrieve Unique Departments
The query retrieves distinct department names from the Geeks
table, ensuring that no duplicate department names appear in the result. This is particularly useful for summarising data or understanding the available categories within a column.
Query:
SELECT DISTINCT DEPARTMENT
FROM Geeks;
Output
DEPARTMENT |
---|
Admin |
DBA |
Review |
Writer |
Retrieving Additional Details
This query fetches all rows from the Geeks
table where the DEPARTMENT
column contains values that start with "DBA". Using the LIKE
operator allows partial matching, making it flexible for filtering records based on patterns.
Query:
Select * from Geeks
where DEPARTMENT like 'DBA%';
Output
G_ID | FIRSTNAME | LASTNAME | JOININGDATE | DEPARTMENT |
---|
1 | Mohan | Arora | 7-08-2019 | DBA |
3 | Vishal | Gupta | 9-01-2020 | DBA |
Explanation:
LIKE
** Clause**: Used to filter rows with the DEPARTMENT
column matching "DBA".
- Result: Returns all rows where the department is "DBA".
Conclusion
The DISTINCT
clause is an essential tool in SQL for retrieving unique column values. By eliminating duplicates, it simplifies data analysis and ensures clean result sets. Whether we’re working with a large dataset or generating reports, understanding how to use DISTINCT
effectively can save time and improve accuracy.
Through practical examples and best practices discussed in this article, we are now equipped to usage the power of the DISTINCT
clause in our SQL queries. Start implementing it today to enhance your data workflows and insights.
Similar Reads
How to Query Two Tables For Duplicate Values in SQL? When working with relational databases, it's common to identify duplicate values across multiple tables. SQL provides efficient ways to query such data using different techniques. These methods help streamline data analysis and ensure data consistency.In this article, we demonstrate how to query two
3 min read
How to Find Most Frequent Value in Column in SQL? To find the most frequent value in a column in SQL, use the COUNT() function to get a count of each unique value, sort the result in descending order, and select the first value in the final results set.In SQL, sometimes we need to find frequent values in a column. Finding the Most Frequent Value in
1 min read
SQL Query to Exclude Multiple Values To exclude multiple values to be fetched from a table we can use multiple OR statements but when we want to exclude a lot of values it becomes lengthy to write multiple AND statements, To avoid this we can use the NOT IN clause with the array of values that need to be excluded with the WHERE stateme
2 min read
SQL Query to Find Duplicate Names in a Table Duplicate records in a database can create confusion, generate incorrect results, and waste storage space. Itâs essential to identify and remove duplicates to maintain data accuracy and database performance. In this article, weâll discuss the reasons for duplicates, how to find duplicate records in
3 min read
SQL Query to Add Unique key Constraints Using ALTER Command Here we will see how to add unique key constraint to a column(s) of a MS SQL Server's database with the help of a SQL query using ALTER clause. For the demonstration purpose, we will be creating a demo table in a database called "geeks". Creating the Database : Use the below SQL statement to create
2 min read