How To Use Distinct In Sql in SQL
How to Use DISTINCT in SQL
The DISTINCT keyword is used to return only unique (different) values from a column or a set of columns, eliminating duplicates in the result set.
Syntax:
SELECT DISTINCT column1, column2, ...FROM table_name;Example 1: Distinct Values in One Column
Suppose you have a table employees with a department column, and you want to find all unique departments:
SELECT DISTINCT departmentFROM employees;Example 2: Distinct Combinations of Multiple Columns
To get unique combinations of department and job_title:
SELECT DISTINCT department, job_titleFROM employees;Notes:
DISTINCTapplies to all selected columns together.It removes duplicate rows from the result.
Useful when you want to count or list unique values.
If you want, I can show you how to use DISTINCT with aggregation or other clauses!