The DISTINCT clause is used in SQL to remove duplicate rows from the result set and return only unique records.
Basic Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column;column1, column2, ...: The columns to select with duplicates removed.WHERE condition: Optional filtering condition.ORDER BY column: Optional sorting.

Examples:
-
Selecting Unique Supplier IDs:
SELECT DISTINCT SupplierID FROM Products;- This returns a list of unique
SupplierIDvalues from theProductstable, removing any duplicates.
- This returns a list of unique
-
Selecting Unique First Names:
SELECT DISTINCT firstName FROM Person ORDER BY firstName;- This returns distinct first names in alphabetical order, ignoring duplicates.
-
Counting Unique Values:
SELECT COUNT(DISTINCT firstName) AS uniqueFirstNames FROM Person;- This counts the number of distinct first names in the
Persontable.
- This counts the number of distinct first names in the
Count + Distinct
في SQL Server، عشان تعمل COUNT للقيم المميزة (Distinct) في عمود معين، ممكن تستخدم دالة COUNT مع الكلمة المفتاحية DISTINCT. الكود هيبقى كالتالي:
SELECT COUNT(DISTINCT column_name) AS DistinctCount
FROM table_name;شرح الكود:
COUNT(DISTINCT column_name): بتحسب عدد القيم المميزة (بدون تكرار) في العمود.AS DistinctCount: بتدي اسم للنتيجة (اختياري).FROM table_name: بتحدد الجدول اللي فيه العمود.
مثال عملي:
لو عندك جدول اسمه Employees فيه عمود Department، وعايز تعرف عدد الأقسام المختلفة:
SELECT COUNT(DISTINCT Department) AS DistinctDepartments
FROM Employees;هيطلعلك عدد الأقسام المختلفة في الجدول Employees.
Key Considerations:
-
NULL Values:
DISTINCTconsiders allNULLvalues as the same, so only oneNULLvalue will be returned. -
Performance:
DISTINCTis typically faster than usingGROUP BYwhen you only need unique values and not aggregate functions likeSUM,COUNT, etc. -
DISTINCT Across Multiple Columns: When used with multiple columns,
DISTINCTreturns unique combinations of those column values.SELECT DISTINCT gender, firstName FROM Person;
DISTINCT vs. GROUP BY:
- DISTINCT: Faster when you need unique values from selected columns without performing aggregate calculations.
- GROUP BY: More powerful, used for grouping rows and calculating aggregates like
SUM,AVG,COUNT, etc., but slower thanDISTINCTfor just removing duplicates.
Handling Duplicates with Multiple Columns:
When applying DISTINCT to multiple columns, it treats the combination of those columns as unique values:
SELECT DISTINCT gender, firstName
FROM Person
ORDER BY gender, firstName;This removes duplicate combinations of gender and firstName.
Important Notes:
- DISTINCT does not consider the entire row for uniqueness unless all specified columns are included.
- The result will show distinct combinations of values from the columns listed in the
SELECTclause. - If you’re only interested in eliminating duplicates for a single column,
DISTINCTcan be very useful and efficient compared toGROUP BY.
In summary, DISTINCT is a simple and efficient way to remove duplicate records from a result set based on one or more columns, providing a list of unique values or combinations of values.