Have you ever found yourself in a situation where you need to retrieve duplicate values from a table in SQL Server? This could be useful for several reasons, such as identifying records with the same data or pinpointing inconsistent entries. However, SQL Server's DISTINCT clause only returns unique values, leaving you wondering how to get duplicate values. In this guide, we'll explore two efficient methods to achieve this.

Before diving into the solutions, let's assume we have a simple table named 'Orders' with two columns: 'OrderID' and 'CustomerName'. We want to find all the customers with duplicate orders. Here's how you can do it.

Using GROUP BY and HAVING Clauses
One of the simplest ways to find duplicate values is by grouping the data and filtering the grouped results. But first, let's see why a straightforward 'GROUP BY' won't work:

Why GROUP BY Alone Doesn't Suffice
Even though 'GROUP BY' can group records with the same values, it doesn't provide the count of occurrences. So, you wouldn't know which groups have duplicates without another query. Here's an example of what 'GROUP BY' would look like:

```sql SELECT CustomerName FROM Orders GROUP BY CustomerName; ```
This query will return all unique customers, but it won't help us find duplicates.
Using HAVING to Filter Groups
To find duplicate values, we'll use the 'HAVING' clause alongside 'GROUP BY'. 'HAVING' can filter groups based on a specified condition, unlike 'WHERE', which filters records before grouping. Here's how to do it:

```sql SELECT CustomerName, COUNT(*) FROM Orders GROUP BY CustomerName HAVING COUNT(*) > 1; ```
In this query, 'HAVING COUNT(*) > 1' filters the groups, keeping only those with more than one record, i.e., duplicates.
Using a Self-Join and Aggregate Functions
Another method to find duplicates involves performing a self-join with the help of aggregate functions. This approach might be beneficial when you need to examine duplicates in relation to other columns' values.

Setting Up the Self-Join
A self-join is simply joining a table to itself. In our case, we'll join the 'Orders' table to itself on the 'CustomerName' column. Here's an example of what the self-join would look like:




















```sql SELECT o1.CustomerName, o2.OrderID FROM Orders o1 INNER JOIN Orders o2 ON o1.CustomerName = o2.CustomerName; ```
This query will return a Cartesian product of customers and their orders, but it's not yet in a useful format.
Using Aggregate Functions to Filter Duplicates
Now, we'll use aggregate functions to find duplicates. We'll count the orders per customer and filter the results to show only those with more than one order:
```sql SELECT CustomerName, COUNT(*) as NumberOfOrders FROM ( SELECT CustomerName FROM Orders ) AS OrdersWithDuplicates GROUP BY CustomerName HAVING COUNT(*) > 1; ```
This query wraps our initial self-join query in a subquery and uses 'GROUP BY' and 'HAVING' to filter the results and display only customers with duplicate orders.
The methods described above are efficient ways to find duplicate values in SQL Server. Depending on your specific needs, you might find one approach more useful than the other. Now that you've mastered these techniques, you can confidently tackle duplicate values in your databases.