SQL Practice Logo

SQLPractice Online

DISTINCT & Removing Duplicates: Examples

Module: SQL Fundamentals

Basic DISTINCT - Remove Duplicate Departments

basic

HR needs unique list of departments for dropdown menu

-- Without DISTINCT: Returns duplicate departments

SELECT department FROM employees;

-- Result: 5 rows with duplicates

-- With DISTINCT: Returns only unique departments

SELECT DISTINCT department FROM employees;

-- Result: 2 rows (Engineering, Sales)

department

Engineering

Sales

DISTINCT removes duplicate rows, returning only unique department names. Perfect for dropdown lists, reports, or data analysis.

All

DISTINCT on Multiple Columns

intermediate

Find unique customer-product combinations for recommendation engine

SELECT DISTINCT customer_id, product_id

FROM orders;

customer_id | product_id

101 | 501

101 | 502

102 | 501

DISTINCT on multiple columns returns unique combinations. Useful for finding "which customers bought which products" without counting quantities.

All