SQL Fundamentals

DISTINCT & Removing Duplicates: Real-World

Duplicate rows in query results are common when joining tables or selecting non-unique columns. DISTINCT removes duplicate rows, returning only unique combinati

Duplicate rows in query results are common when joining tables or selecting non-unique columns. DISTINCT removes duplicate rows, returning only unique combinations of selected columns. Understanding when to use DISTINCT versus GROUP BY is crucial for writing efficient queries.

Unique campaign audience

A campaign export needs one customer per eligible segment even when a customer has several qualifying purchases.

The downstream campaign receives one stable recipient record per customer.

Return the unique audience

SELECT DISTINCT customer_id FROM orders WHERE status = 'completed' AND order_date >= CURRENT_DATE - INTERVAL '30 days';

All