Distinct clause in MS SQL Server
Last Updated :
18 Apr, 2023
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values and sometimes we only want to list the different (distinct) values.
Consider a simple database and we will be discussing distinct clauses in MS SQL Server. Suppose a table has a maximum of 1000 rows constituted.The probability of repeated rows otherwise called duplicates in SQL terms might occur in the table.
In SQL Server, distinct is a term used to remove duplicates from a table.
Syntax
SELECT DISTINCT column1, column2, …
FROM table_name
WHERE conditions;
Parameter Explanation
- DISTINCT: It will return only unique values in the column.
Query:
CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Australia', 'Spain','22','xxxxxxxxxx');
Select * from Customer;
Output:
To remove the duplicates, the query must be written as follows:
Query:
select distinct
Country
from Customer
Output:
As there are no duplicates, the same number of rows are returned. Let us see an example having duplicates.In our database “Age” having a duplicate values. So the query should be written as follows:
Query:
select distinct
Age
from student
Output:
There was a duplicate Age 21 but by using distinct, the duplicates are removed.
Consider another example of null values. Here we will create a table with the name:
Query:
CREATE TABLE my_table (
id INT,
name VARCHAR(50),
age INT,
city VARCHAR(50),
email VARCHAR(100)
);
INSERT INTO my_table (id, name, age, city, email)
VALUES
(1, 'John Doe', NULL, 'New York', 'john.doe@example.com'),
(2, 'Jane Smith', 28, NULL, 'jane.smith@example.com'),
(3, 'Bob Johnson', 35, 'Los Angeles', NULL),
(4, 'Sarah Lee', NULL, NULL, NULL);
Output:
Note that the name and the city are null in the case of Aditya, so the query is written as:
Query:
select distinct
name, age , city
from my_table
Output:
In the case of null values, distinct removes all the other null values and restores only one null value as shown in the output.
Note: In SQL by default, it will not default we have to mention it separately in the query.
Distinct vs Group By
There is a difference between Distinct and Group in SQL. Let’s see a sample example to see the differences.
Query:
CREATE TABLE my_table (
id INT,
name VARCHAR(50),
age INT,
city VARCHAR(50),
email VARCHAR(100)
);
INSERT INTO my_table (id, name, age, city, email)
VALUES
(1, 'Shubham', 35, 'New York', 'john.doe@example.com'),
(2, 'Aditya', 28, NULL, 'jane.smith@example.com'),
(3, 'Shubham', 35, 'Los Angeles', NULL),
(4, 'Naveen', NULL, NULL, NULL);
Output:
Group By:
Distinct:

Similar Reads
Distinct clause in MS SQL Server
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values and sometimes we only want to list the different (distinct) values. Consider a simple database and we will be discussing distinct clauses in MS SQL Server.
3 min read
SQL Distinct Clause
The SQL DISTINCT keyword is used in queries to retrieve unique values from a database. It helps in eliminating duplicate records from the result set. It ensures that only unique entries are fetched. Whether you're analyzing datasets or performing data cleaning, the DISTINCT keyword is Important for
4 min read
Group by clause in MS SQL Server
Group by clause will be discussed in detail in this article. There are tons of data present in the database system. Even though the data is arranged in form of a table in a proper order, the user at times wants the data in the query to be grouped for easier access. To arrange the data(columns) in fo
3 min read
Find Duplicates in MS SQL Server
Finding duplicate values in a database is a common task when managing data integrity. In SQL, several methods can be employed to identify and handle duplicate entries. In this article, We will explore two effective techniques for locating duplicates using SQL queries: the GROUP BY clause and the ROW
4 min read
Unique Constraint in MS SQL Server
A table might have repetitive data in form of rows. This might cause glitches while retrieving data from the query. To avoid them, a unique constraint is used. Unique allows sorting a column or set of columns uniquely meaning, a user cannot insert a duplicate or repeated value in a column as it resu
2 min read
Delete Duplicate Rows in MS SQL Server
In MS SQL Server, managing duplicate rows is a common task that can affect the integrity and performance of a database. To address this issue, SQL Server provides several methods for identifying and deleting duplicate rows. In this article, We will explore three effective approaches: using the GROUP
5 min read
COUNT() Function in SQL Server
The COUNT() function in SQL Server is a fundamental aggregate function used to determine the number of rows that match a specific condition. Counting rows provides valuable insights into data sets such as the total number of records, distinct values, or records meeting certain criteria. In this arti
3 min read
Introduction of MS SQL Server
Data is a collection of facts and figures and we have humungous data available to the users via the internet and other sources. To manipulate the data, Structured Query Language (SQL) in short has been introduced years ago. There are different versions of SQL available in the market provided by diff
2 min read
How to SELECT DISTINCT on Multiple Columns in SQL?
In the world of databases, data duplication can lead to confusion and inefficiency. SQL provides a powerful tool, SELECT DISTINCT, to retrieve unique values from columns. However, when dealing with multiple columns, the approach becomes more detailed. In this article, we will explain how to use SELE
4 min read
RANK() Function in SQL Server
The RANK() function is a powerful window function in SQL Server used to assign a rank to each row within a partition of a result set. Unlike other ranking functions, the RANK() function introduces gaps in the ranking sequence for rows with duplicate values, making it particularly useful for scenario
3 min read
Self Join and Cross Join in MS SQL Server
Prerequisite - Introduction of MS SQL Server 1. Self Join : Self-join allows us to join a table itself. It is useful when a user wants to compare the data (rows) within the same table. Syntax - select select_list from T t1 [Inner|Left] Join on T t2 on join_predicate. Here T refers to the table we us
2 min read
Having vs Where Clause in SQL
In SQL, filtering data is important for extracting meaningful insights from large datasets. While both the WHERE and HAVING clauses allow us to filter data, they serve distinct purposes and operate at different stages of the query execution process. Understanding the difference between these clauses
4 min read
Having vs Where Clause in SQL
In SQL, filtering data is important for extracting meaningful insights from large datasets. While both the WHERE and HAVING clauses allow us to filter data, they serve distinct purposes and operate at different stages of the query execution process. Understanding the difference between these clauses
4 min read
SQL Server DIFFERENCE() Function
SQL Server DIFFERENCE() function compares two different SOUNDEX values and returns an integer. This integer value measures the degree to which the SOUNDEX values match, on a scale of 0 to 4. A value of 0 indicates weak or no similarity between the SOUNDEX values; 4 indicates that the SOUNDEX values
2 min read
Change Primary Key Column in SQL Server
Primary Key refers to the column of a table that uniquely identifies a row in a table. It contains unique values and can not contain NULL values. For the purpose of the demonstration, we will be creating geeks for geeks table in a database called âgeeksâ. Step 1: Creating the database The database i
2 min read
DISTINCT vs GROUP BY in SQL
SQL (Structured Query Language) is used to manage and manipulate the data in relational databases. It can be used for tasks such as database querying, data editing, database and table creation and deletion, and granting user permissions. We can use the DISTINCT keyword and GROUP BY clause when we wa
4 min read
DISTINCT vs GROUP BY in SQL
SQL (Structured Query Language) is used to manage and manipulate the data in relational databases. It can be used for tasks such as database querying, data editing, database and table creation and deletion, and granting user permissions. We can use the DISTINCT keyword and GROUP BY clause when we wa
4 min read
SQL Server Architecture
Microsoft SQL Server is a widely used relational database management system (RDBMS) that organizations around the world rely on for managing and processing their data. It provides a scalable and reliable platform for managing large volumes of data, supporting a wide range of applications from small-
5 min read
How to Limit Rows in a SQL Server?
To limit rows in SQL Server, use the TOP clause in the SELECT statement. Using the TOP clause in SQL Server, users can limit the number of rows in the results set. Here, we will understand how to limit rows in SQL Server with the help of different examples. Steps to Limit Rows in SQL ServerLet's che
3 min read
Primary key in MS SQL Server
A table has a particular number of columns and each column has n number of rows. At times, there might be a probability of repeated rows in a column. For example, a column named identification number has repeated rows. To avoid the duplication of rows, the concept of a key has been introduced. A key
2 min read