DISTINCT clause in SQL Server

In SQL Server (Transact-SQL), the DISTINCT clause is used to 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

  1. When the DISTINCT clause has only one expression, the query returns unique values ​​for that expression.
  2. 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 ho 
FROM nhanvien
WHERE nhanvien_id >= 50;

This example will return all employee surname values ​​from the table with the table number greater than or equal to 50.

For example - multiple expressions

 SELECT DISTINCT ten, ho 
FROM nhanvien
WHERE nhanvien_id >= 50
ORDER BY ho;

In this example, the returned result will be the combination of the first and last name from the table when the value is greater than or equal to 50. The result is sorted in ascending order of the employee.

In this case, DISTINCT applies to each information field behind the DISTINCT keyword, so it will create different name combinations - they are different.

Previous article: Combine AND and OR conditions in SQL Server

Next article: IN conditions in SQL Server

5 ★ | 1 Vote

May be interested

  • Introduction to SQL Server Reporting ServicesPhoto of Introduction to SQL Server Reporting Services
    sql server 2005, 2008 and 2008 r2 software packages are pre-packaged with sql server reporting services (ssrs) - a specialized report creation solution for businesses. with ssrs, you can completely create, publish, and manage a huge number of reports from various data sources ...
  • Instructions for creating and editing reports in SQL Server Reporting ServicesPhoto of Instructions for creating and editing reports in SQL Server Reporting Services
    sql server 2005, 2008 and 2008 r2 product suite, all packaged with sql server reporting services (ssrs) - a dedicated solution for creating reports for businesses. with ssrs, users can create, schedule, post and manage various reports from various sources of original data.
  • SQL Server setup is always availablePhoto of SQL Server setup is always available
    database mirroring solution helps build a high-availability database management system in sql server which is quite simple and suitable for medium-sized and lower-level databases.
  • Segment tables in SQL ServerPhoto of Segment tables in SQL Server
    table partitioning technique (table partitioning) to effectively manage the database with large capacity.
  • The difference between Truncate and Delete in Microsoft SQL ServerPhoto of The difference between Truncate and Delete in Microsoft SQL Server
    in the following article, we will help you distinguish some basic differences between two delete syntax and truncate table in microsoft sql server application. basically, both of these statements help us to remove the data, but in essence it is not so.
  • AND conditions in SQL ServerPhoto of AND conditions in SQL Server
    in sql server, the and condition (or and operator) is used to test two or more conditions.