A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/mysql/mysql-distinct-clause/ below:

MySQL DISTINCT Clause - GeeksforGeeks

MySQL DISTINCT Clause

Last Updated : 23 Jul, 2025

When working with databases, you often need to filter out duplicate records to get a clear and accurate result set. MySQL offers a straightforward solution for this with the DISTINCT clause. This clause helps you retrieve only unique rows from your query results, making it an essential tool for data analysis and reporting.

In this article, we'll explore the DISTINCT clause, its syntax, and practical examples to help you use it effectively in your database operations.

MySQL DISTINCT Clause

The DISTINCT clause is used in a SELECT statement to remove duplicate rows from the result set. When applied, it ensures that only unique records are returned, eliminating any redundant data. This is particularly useful when you need to analyze or report on unique values within a dataset.

MySQL DISTINCT Clause

The above figure demonstrates the working of MySQL SELECT DISTINCT, as it excludes all the duplicate records and fetches only single instances of each type.

Syntax:

SELECT DISTINCT column1, column2, ...

FROM table_name

WHERE condition;

Examples of MySQL DISTINCT Clause Example 1: Selecting Distinct Values from a Single Column

Consider a table named employees with the following data:

employee_id employee_name department 1 John Doe Sales 2 Jane Smith HR 3 Bob Johnson Sales 4 Alice Williams Marketing 5 Charlie Brown HR

To retrieve a list of unique departments, you can use the DISTINCT clause:

SELECT DISTINCT department
FROM employees;

Output:

department Sales HR Marketing

This query returns a list of unique departments from the employees table, eliminating any duplicates.

Example 2: Selecting Distinct Values From Multiple Columns

Suppose you want to retrieve unique combinations of department and employee_name. You can modify the query to include multiple columns:

SELECT DISTINCT department, employee_name
FROM employees;

Output:

department employee_name Sales John Doe HR Jane Smith Sales Bob Johnson Marketing Alice Williams HR Charlie Brown

In this case, the query returns distinct combinations of department and employee_name.

Example 3: Using DISTINCT with WHERE Clause

You can also combine the DISTINCT clause with a WHERE clause to filter the results. For example, to get unique departments where the department name starts with 'S':

SELECT DISTINCT department
FROM employees
WHERE department LIKE 'S%';

Output:

This query filters the departments to only those starting with 'S' and then returns unique values.

Considerations When Using DISTINCT
  1. Performance Impact: Using the DISTINCT clause can impact query performance, especially on large datasets, because it requires MySQL to process and compare each row to identify duplicates.
  2. Order of Columns: The distinctness of rows is determined based on the order of columns in the SELECT statement. Different orders can yield different results.
  3. NULL Values: The DISTINCT clause treats NULL values as equal. If multiple rows have NULL in the same column, they are considered duplicates.
Conclusion

The MySQL DISTINCT clause is a powerful tool that helps you retrieve unique records from your database queries, ensuring that your results are free from duplicates. Whether you're working with single columns or multiple columns, using DISTINCT can greatly enhance the accuracy and clarity of your data. By understanding how and when to use this clause, you can improve your database management and data analysis tasks, leading to more efficient and reliable outcomes. Always consider the potential performance implications, but rest assured that DISTINCT is an essential feature for any database professional's toolkit.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4