Table of Contents
MS SQL Server: DISTINCT Clause in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
In SQL Server (Transact-SQL), the DISTINCT clause helps remove duplicates in the result set. The DISTINCT clause can only be used in the SELECT statement.
DISTINCT clause syntax
SELECT DISTRINCT 'bi?u th?c'FROM 'b?ng'[WHERE '?i?u ki?n'];
Variable name or variable value
'expression'
The column or calculated value you want to retrieve
'table'
Table used to retrieve records. Must have at least 1 table in the FROM clause.
WHERE 'condition'
Option. Conditions that the record must meet to be selected.
Note
- When the DISTINCT clause has only one expression, the query returns unique values for that expression.
- When the DISTINCT clause has more than 1 expression, the query returns the unique combination of expressions.
In SQL Server, the DISTINCT clause does not ignore the NULL value. So when using this clause in the command, the returned result will have a unique NULL value.
Example - 1 expression
The simplest example with the DISTINCT clause in SQL Server has only one expression.
SELECT DISTINCT hoFROM nhanvienWHERE nhanvien_id >= 50;
This example will return all employee surname values from the table with the table number greater than or equal to 50.
As an example, - multiple expressions
SELECT DISTINCT ten, hoFROM nhanvienWHERE nhanvien_id >= 50ORDER BY ho;
FAQ
What is MS SQL Server: DISTINCT Clause in SQL Server?
In SQL Server (Transact-SQL), the DISTINCT clause helps remove duplicates in the result set.
Why is MS SQL Server: DISTINCT Clause in SQL Server important?
A clear understanding of MS SQL Server: DISTINCT Clause in SQL Server helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
How should beginners approach MS SQL Server: DISTINCT Clause in SQL Server?
Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.
Reader Comments 0
Sign in with email or Google to join the discussion.