Distinct In SQL

Galaxy Glossary

How do you select only unique values from a column in SQL?

The `DISTINCT` keyword in SQL is used to retrieve only unique rows or values from a result set. It's crucial for filtering out duplicate entries, ensuring data integrity, and simplifying analysis.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The `DISTINCT` keyword in SQL is a powerful tool for data manipulation. It allows you to eliminate duplicate rows or values from a query result, leaving only the unique entries. This is essential for tasks like finding unique customer names, identifying distinct product types, or counting unique items in a dataset. Imagine you have a table of orders, and you want to know the different types of products ordered. Using `DISTINCT` on the product column will give you a list of only the unique product types, without any repetition. This is a fundamental operation in data analysis and manipulation, ensuring that your results are accurate and concise. It's particularly useful when you need to count unique values or perform aggregations on unique data points. For example, if you're calculating the number of unique customers, `DISTINCT` ensures you don't double-count customers who have placed multiple orders. The `DISTINCT` keyword is typically used in conjunction with the `SELECT` statement, specifying which columns should have their unique values returned.

Why Distinct In SQL is important

The `DISTINCT` keyword is crucial for accurate data analysis and reporting. It ensures that results are not skewed by duplicate entries, leading to more reliable insights. It's a fundamental tool for data cleaning and preparation, making sure your data is ready for further analysis.

Example Usage


-- Sample table (Customers)
CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    City VARCHAR(50)
);

INSERT INTO Customers (CustomerID, FirstName, LastName, City) VALUES
(1, 'John', 'Doe', 'New York'),
(2, 'Jane', 'Doe', 'Los Angeles'),
(3, 'John', 'Smith', 'Chicago'),
(4, 'Jane', 'Doe', 'Houston');

-- Query to get unique cities
SELECT DISTINCT City FROM Customers;
-- Expected output:
-- New York
-- Los Angeles
-- Chicago
-- Houston

-- Query to get unique first names
SELECT DISTINCT FirstName FROM Customers;
-- Expected output:
-- John
-- Jane

-- Query to get unique combinations of first and last names
SELECT DISTINCT FirstName, LastName FROM Customers;
-- Expected output:
-- John, Doe
-- Jane, Doe
-- John, Smith

Common Mistakes

Want to learn about other SQL terms?