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!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Table of Contents

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.

Distinct In SQL 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

Distinct In SQL Syntax



Common Mistakes

Frequently Asked Questions (FAQs)

How do I remove duplicate rows in SQL with the DISTINCT keyword?

To eliminate duplicate rows, place DISTINCT immediately after SELECT and list the columns whose unique combinations you want returned. For example:
SELECT DISTINCT product_type FROM orders;
This statement scans the product_type column, removes repeated values, and returns only one row per unique product type—ideal for de-duplicating customer names, product SKUs, or any column subject to repetition.

What is the best way to count unique customers or products using DISTINCT?

Pair DISTINCT with an aggregate like COUNT() to calculate cardinality without double-counting. Example:
SELECT COUNT(DISTINCT customer_id) AS unique_customers FROM orders;
This guarantees each customer is tallied once, even if they have multiple orders. The same pattern applies to products, sessions, or any entity where uniqueness matters.

How does Galaxy’s AI copilot improve writing and maintaining DISTINCT queries?

Galaxy’s context-aware AI copilot autocompletes columns, flags unnecessary duplicates, and can instantly rewrite a query to add or remove DISTINCT based on the underlying data model. When tables evolve, the copilot suggests optimized versions—helping you avoid mistakes like omitting a column that breaks uniqueness. Combined with Galaxy’s sharing and endorsement features, teams can store vetted DISTINCT queries in Collections instead of pasting them into Slack.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.