The best examples of SQL DISTINCT keyword explained for real-world queries

If you’ve ever opened a messy database and thought, “I just want the unique values, not this wall of duplicates,” the SQL DISTINCT keyword is your friend. In this guide, we’ll walk through practical, real examples of SQL DISTINCT keyword usage, not just theory. You’ll see examples of sql distinct keyword examples explained in everyday reporting scenarios: counting customers, cleaning up product lists, simplifying analytics queries, and more. Instead of vague definitions, we’ll focus on how DISTINCT behaves in different contexts: with single columns, multiple columns, aggregate functions, and even subqueries. Along the way, we’ll talk about performance, indexing, and modern analytics patterns you’re likely to hit in 2024–2025, especially if you’re working with PostgreSQL, MySQL, SQL Server, or cloud warehouses like BigQuery and Snowflake. By the end, you’ll understand not only how DISTINCT works, but when it’s the right tool—and when it’s a bad habit that hides data quality problems.
Written by
Jamie
Published
Updated

Real examples of SQL DISTINCT keyword examples explained in everyday queries

Let’s skip the textbook definition and walk straight into real examples. The SQL DISTINCT keyword removes duplicate rows from a result set. That sounds simple, but the behavior changes in interesting ways depending on how you use it.

Imagine a basic orders table:

CREATE TABLE orders (
    order_id      INT PRIMARY KEY,
    customer_id   INT,
    order_date    DATE,
    status        VARCHAR(20),
    total_amount  DECIMAL(10,2),
    country       VARCHAR(50)
);

Most real examples of SQL DISTINCT keyword usage start with tables like this: transactional data with lots of repetition in fields such as customer_id, status, or country.


Example of DISTINCT on a single column: cleaning up value lists

One of the best examples of sql distinct keyword examples explained in practice is the classic “filter to unique values” query. Say you want a list of countries where you have orders:

SELECT DISTINCT country
FROM orders
ORDER BY country;

This returns each country exactly once, no matter how many orders came from that country. In day-to-day analytics work, examples include:

  • Building filter dropdowns in dashboards (e.g., list of countries, product categories, or user roles).
  • Quickly checking data coverage: “Which markets did we actually sell into last quarter?”
  • Sanity-checking imported data for strange or misspelled values.

In 2024, BI tools like Power BI, Tableau, and Looker often generate this pattern behind the scenes when populating filter controls. Understanding these examples of sql distinct keyword examples explained in your own queries helps you debug why a filter shows a value you didn’t expect.


Examples of SQL DISTINCT with multiple columns explained

The moment you add more than one column, DISTINCT behaves differently. It no longer cares about individual column uniqueness; it cares about the combination.

Say you want a list of customer–country combinations that actually appear in your orders:

SELECT DISTINCT customer_id, country
FROM orders
ORDER BY customer_id, country;

Here, a customer who ordered from two different countries appears twice. One of the best examples of this pattern is geo analysis for remote workers or travelers using your service.

Another example of SQL DISTINCT keyword usage with multiple columns:

SELECT DISTINCT status, country
FROM orders
ORDER BY status, country;

This gives you only the status–country pairs that exist. This is useful when:

  • You want to see which regions ever had "CANCELLED" orders.
  • You’re mapping business rules like “Status X is valid only in these countries.”

These examples of sql distinct keyword examples explained in multi-column queries are where many beginners get surprised: DISTINCT is row-based, not column-based.


Real examples of SQL DISTINCT with aggregate functions

DISTINCT also shows up inside aggregate functions, and that’s where things get subtle. Compare these two queries:

Count of all orders:

SELECT COUNT(*) AS total_orders
FROM orders;

Count of distinct customers who placed orders:

SELECT COUNT(DISTINCT customer_id) AS distinct_customers
FROM orders;

That COUNT(DISTINCT ...) is one of the best examples of SQL DISTINCT keyword examples explained in analytics work. It answers questions like:

  • How many customers ordered in 2024, not how many orders?
  • How many distinct products did we sell this month?
  • How many distinct visitors hit the site this week (in web analytics schemas)?

Another real example:

SELECT
    country,
    COUNT(*) AS total_orders,
    COUNT(DISTINCT customer_id) AS distinct_customers
FROM orders
GROUP BY country
ORDER BY country;

This pattern shows up constantly in business reporting. You’re not just counting rows; you’re counting distinct entities. Modern data engineering blogs from universities such as Harvard often show similar patterns when describing aggregated research datasets.


Examples of SQL DISTINCT with WHERE and ORDER BY

DISTINCT does not exist in a vacuum. It plays alongside filters and sorting. Here’s a useful example of SQL DISTINCT keyword usage with a filter:

SELECT DISTINCT customer_id
FROM orders
WHERE order_date >= '2024-01-01'
  AND status = 'COMPLETED'
ORDER BY customer_id;

This returns customers who completed at least one order in 2024. Examples include:

  • Building a marketing audience of active buyers.
  • Identifying customers to include in a loyalty program.

Now add sorting on a different column when using multiple columns with DISTINCT:

SELECT DISTINCT customer_id, country
FROM orders
WHERE order_date >= '2024-01-01'
ORDER BY country, customer_id;

The DISTINCT is applied first to the row set after filtering, then the result is sorted. Understanding this order of operations is one of the best examples of sql distinct keyword examples explained in execution terms: FROM → WHERE → DISTINCT → ORDER BY.


Real examples of SQL DISTINCT vs GROUP BY

In 2024, you’ll see a lot of debates on forums and Q&A sites about when to use DISTINCT versus GROUP BY. Often, they produce the same result—but the intent differs.

Simple DISTINCT:

SELECT DISTINCT country
FROM orders;

Equivalent GROUP BY:

SELECT country
FROM orders
GROUP BY country;

For a single column, these are effectively the same. But when you want aggregates, GROUP BY becomes clearer:

SELECT country, COUNT(*) AS total_orders
FROM orders
GROUP BY country;

You could combine DISTINCT and GROUP BY in strange ways, but it’s rarely a good idea. A better pattern is either:

  • Use DISTINCT to get unique rows, or
  • Use GROUP BY to both group and aggregate.

One useful example of SQL DISTINCT keyword usage inside a subquery with GROUP BY:

SELECT
    country,
    COUNT(*) AS active_customers
FROM (
    SELECT DISTINCT customer_id, country
    FROM orders
    WHERE order_date >= '2024-01-01'
) AS distinct_customers
GROUP BY country
ORDER BY active_customers DESC;

This two-step pattern—first DISTINCT, then GROUP BY—is common in analytics pipelines and is often discussed in training materials from universities and research institutions, similar in spirit to tutorials you might find around data aggregation on sites like NIH, where summarizing distinct participants or samples is standard.


Examples of SQL DISTINCT performance considerations in 2024–2025

In modern databases and cloud warehouses, DISTINCT is not free. Under the hood, it usually triggers a sort or a hash aggregation to identify duplicates. On a billion-row table, that can hurt.

Real examples include:

  • A dashboard query that does SELECT DISTINCT user_id FROM events on a massive events table, causing slow load times.
  • A data scientist running COUNT(DISTINCT user_id) on unsampled clickstream data in BigQuery and getting a surprisingly high bill.

To keep DISTINCT from becoming a performance problem:

  • Index the columns you frequently use with DISTINCT, especially in OLTP databases like PostgreSQL or SQL Server.
  • In warehouses, partition and cluster tables so that DISTINCT operations scan fewer bytes.
  • Consider approximate distinct counts (e.g., APPROX_COUNT_DISTINCT in BigQuery or HyperLogLog-based functions) when you only need an estimate.

These topics are widely discussed in modern data engineering circles, and you’ll see similar guidance on performance and query optimization in technical resources from institutions like MIT and other academic data labs.


Advanced examples of SQL DISTINCT keyword examples explained with joins

Things get more interesting when joins enter the picture. Joins often multiply rows, and DISTINCT is used to “collapse” them back down.

Consider a separate customers table:

CREATE TABLE customers (
    customer_id   INT PRIMARY KEY,
    customer_name VARCHAR(100),
    email         VARCHAR(255)
);

If you join orders to customers and want one row per customer who has at least one order:

SELECT DISTINCT c.customer_id, c.customer_name
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id;

Here, DISTINCT removes duplicate customer rows created by multiple orders. Real examples include:

  • Generating a contact list of customers who have ever ordered.
  • Building a deduplicated export of customer data for a CRM.

Another example of SQL DISTINCT keyword usage with left joins:

SELECT DISTINCT c.customer_id, c.customer_name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NOT NULL;
``;

This pattern ensures you only get customers who actually have orders, even though you started from the full `customers` table.

---

## Examples of SQL DISTINCT with NULL values explained

One subtle area where people trip up is how DISTINCT handles NULL. In SQL, NULL means “unknown,” but DISTINCT treats all NULLs in a column as the same for deduplication.

Example:

```sql
SELECT DISTINCT status
FROM orders;

If some rows have status = NULL, you’ll see a single NULL in the result set, not one per row. Real examples of sql distinct keyword examples explained with NULL include:

  • Checking which statuses are actually used, including missing data.
  • Auditing for unexpected NULLs in key fields like country or status.

With multiple columns, a row with (NULL, 'US') is different from (NULL, 'UK'), so both will appear. DISTINCT is row-aware, not value-aware in isolation.


When DISTINCT is a smell: real-world anti-examples

DISTINCT can be overused. In 2024–2025, data teams increasingly treat “just slap DISTINCT on it” as a warning sign. Some anti-pattern examples include:

  • Using SELECT DISTINCT * on a messy join instead of fixing the join conditions.
  • Hiding data quality issues: duplicates in source tables that should have been prevented with constraints.
  • Wrapping every query in DISTINCT out of habit, then wondering why everything runs slowly.

A better approach:

  • Fix the underlying model or join logic.
  • Add primary keys or unique constraints where appropriate.
  • Use DISTINCT only when the business logic truly calls for deduplication.

These examples of sql distinct keyword examples explained from the “don’t do this” side are just as important as the success stories.


FAQ: short answers with practical examples

Q: Can you give a simple example of using DISTINCT to get unique values?
Yes. To get a list of product categories from a products table:

SELECT DISTINCT category
FROM products
ORDER BY category;

This is the classic example of SQL DISTINCT keyword usage: a clean, deduplicated list.

Q: How is DISTINCT different from GROUP BY if I only need unique rows?
For returning just unique rows, they often produce the same output. SELECT DISTINCT country FROM orders; and SELECT country FROM orders GROUP BY country; are effectively identical. GROUP BY becomes more expressive when you add aggregates, like COUNT(*) or SUM(total_amount).

Q: Can I use DISTINCT on multiple columns at once?
Yes, and that’s one of the best examples of sql distinct keyword examples explained in multi-column form. SELECT DISTINCT customer_id, country FROM orders; returns unique combinations of customer and country, not unique customers or unique countries alone.

Q: Are there performance risks with DISTINCT on large tables?
Yes. DISTINCT usually requires sorting or hashing. On large datasets, that can be expensive. Use indexes, clustering, or approximate distinct functions where available. Also consider pre-aggregating data in summary tables.

Q: Is COUNT(DISTINCT ...) always accurate in cloud data warehouses?
Standard COUNT(DISTINCT column) is accurate but can be slow and costly at scale. Many warehouses also provide approximate functions (like APPROX_COUNT_DISTINCT) for faster, cheaper estimates.


These real examples of SQL DISTINCT keyword examples explained—from simple value lists to advanced joins and aggregates—should give you a solid mental model for when DISTINCT sharpens your query and when it simply masks deeper problems in your data.

Explore More SQL Code Snippets

Discover more examples and insights in this category.

View All SQL Code Snippets