EN
MS SQL Server - find duplicated values
0 points
In this article, we would like to show you how to find duplicated values in MS SQL Server.
Quick solution:
xxxxxxxxxx
1
SELECT [column_name], COUNT([column_name])
2
FROM [table_name]
3
GROUP BY [column_name]
4
HAVING COUNT([column_name]) > 1;
To show how to find duplicated values, 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 the number of names that are duplicated in users
table.
Query:
xxxxxxxxxx
1
SELECT [name], COUNT([name]) AS 'name_number'
2
FROM [users]
3
GROUP BY [name]
4
HAVING COUNT([name]) > 1;
Result:

create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE [users] (
2
[id] INT IDENTITY(1,1),
3
[name] VARCHAR(100) NOT NULL,
4
[email] VARCHAR(100) NOT NULL,
5
[country] VARCHAR(15) NOT NULL,
6
PRIMARY KEY ([id])
7
);
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO [users]
2
([name], [email], [country])
3
VALUES
4
('Tom', 'tom1@email.com', 'Poland'),
5
('Tom', 'tom2@email.com', 'Poland'),
6
('Tom', 'tom3@email.com', 'Poland'),
7
('Kim', 'kim1@email.com', 'Vietnam'),
8
('Kim', 'kim2@email.com', 'Vietnam'),
9
('Chris', 'chris1@email.com', 'Spain'),
10
('Chris', 'chris2@email.com', 'Spain'),
11
('Chris', 'chris3@email.com', 'USA');