WikiGalaxy

Personalize

SQL Select Distinct

Understanding SQL Select Distinct:

The SELECT DISTINCT statement is used in SQL to retrieve unique values from a specified column in a database table. This is particularly useful when you want to eliminate duplicate entries and focus on unique data.

Syntax:

The basic syntax of the SELECT DISTINCT statement is as follows:


SELECT DISTINCT column1, column2, ...
FROM table_name;
      

Use Case:

Suppose you have a table named Customers with a column City. To find out all the different cities where your customers are located, you would use:


SELECT DISTINCT City
FROM Customers;
      

Console Output:

New York, Los Angeles, Chicago

Practical Example: Removing Duplicates

Scenario:

You have a table Orders with columns OrderID, CustomerID, and ProductID. To list all unique products ordered, use:


SELECT DISTINCT ProductID
FROM Orders;
      

Console Output:

101, 102, 103

Combining with WHERE Clause

Advanced Usage:

You can combine SELECT DISTINCT with the WHERE clause to filter records before retrieving distinct values. For instance, to get unique cities where customers have placed orders above $1000:


SELECT DISTINCT City
FROM Customers
WHERE OrderAmount > 1000;
      

Console Output:

San Francisco, Miami

Using Multiple Columns

Multiple Column Distinct:

To retrieve unique combinations of multiple columns, you can specify more than one column in the SELECT DISTINCT clause. For example, to find unique pairs of customer and product:


SELECT DISTINCT CustomerID, ProductID
FROM Orders;
      

Console Output:

C001-P001, C002-P002

Distinct with ORDER BY

Ordering Distinct Results:

You can use the ORDER BY clause with SELECT DISTINCT to sort the unique records. For instance, to get distinct customer names in alphabetical order:


SELECT DISTINCT CustomerName
FROM Customers
ORDER BY CustomerName;
      

Console Output:

Alice, Bob, Charlie

Performance Considerations

Efficiency Tips:

Using SELECT DISTINCT can be resource-intensive on large datasets because it requires sorting and comparing records. It's advisable to use it judiciously and ensure proper indexing on columns involved.

Limitations and Alternatives

Understanding Limits:

While SELECT DISTINCT is useful, it may not always be the most efficient method for removing duplicates. Alternatives like using GROUP BY or window functions can sometimes offer better performance.

Practical Example: Group By Alternative

Using GROUP BY:

Instead of SELECT DISTINCT, you can use GROUP BY to achieve similar results, especially when you need to perform aggregation. For example, to find unique cities with the count of customers:


SELECT City, COUNT(CustomerID)
FROM Customers
GROUP BY City;
      

Console Output:

New York - 5, Los Angeles - 3

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025