EN
MS SQL Server - SELECT DISTINCT statement
0 points
In this article, we would like to show you how to use SELECT DISTINCT
statement in MS SQL Server.
Quick solution:
xxxxxxxxxx
1
SELECT DISTINCT [column1], [column2], [columnN]
2
FROM [table_name];
To show how the SELECT DISTINCT
statement works, we will use the following table:

Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will display unique countries from the users
table.
Query:
xxxxxxxxxx
1
SELECT DISTINCT [country]
2
FROM [users];
Output:

create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE [users] (
2
[id] INT IDENTITY(1,1),
3
[name] VARCHAR(100) NOT NULL,
4
[country] VARCHAR(15) NOT NULL,
5
PRIMARY KEY ([id])
6
);
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO [users]
2
([name], [country])
3
VALUES
4
('Tom', 'Poland'),
5
('Chris', 'Belgium'),
6
('Jack', 'Belgium'),
7
('Kate', 'Algeria'),
8
('Denis', 'Germany'),
9
('Matt', 'Germany');