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

  • The clause to combine JOIN data in SQLThe clause to combine JOIN data in SQL
    in sql, the join clause is used to combine records from two or more tables in a database using common values ​​from each table.
  • Attach database in Microsoft SQL Server 2008Attach database in Microsoft SQL Server 2008
    this article will illustrate the different usage methods of the 'for attach' clause to overcome the limitations encountered when using sp_attach_db and sp_attach_single_file_db.
  • HAVING clause in SQLHAVING clause in SQL
    the having clause in sql is used to filter records and retrieve only those records that match the requirements or are actually needed.
  • Interesting Facts About The Santa ClauseInteresting Facts About The Santa Clause
    the santa clause is one of the best christmas movies of all time. however, behind this christmas movie are surprising, little-known facts about the making process.
  • GROUP BY command in SQLGROUP BY command in SQL
    the group by clause in sql is used in conjunction with the select statement to sort data uniformly into groups.
  • ORDER BY command in SQLORDER BY command in SQL
    in sql, the order by clause is used to sort data in ascending order or in descending order on one or more columns.
  • The difference between web server and app serverThe difference between web server and app server
    you have probably seen that the terms web server and app server are often used interchangeably as if they are related to the same thing and also facilitate the website to function properly. but in reality, they are not the same.
  • Network basics: Part 3 - DNS ServerNetwork basics: Part 3 - DNS Server
    a dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
  • Sort results in SQLSort results in SQL
    to sort data in sql, we use the order by clause.
  • The clause combines UNION data in SQLThe clause combines UNION data in SQL
    in sql, you can combine the same structured data from multiple tables into one query using the union and union all operators.