SQL Practice Logo

SQLPractice Online

DISTINCT & ALL: Examples

Module: Aggregate Functions & Grouping

Basic DISTINCT

basic

List all unique departments in company

SELECT DISTINCT department FROM employees ORDER BY department;

department

Engineering

Finance

Marketing

Sales

DISTINCT removes duplicate department names. Returns one row per unique department.

All

COUNT DISTINCT

basic

Count unique customers who placed orders

SELECT

COUNT(*) AS total_orders,

COUNT(DISTINCT customer_id) AS unique_customers

FROM orders;

total_orders | unique_customers

1250 | 450

COUNT(*) counts all orders. COUNT(DISTINCT customer_id) counts unique customers. Shows 450 customers placed 1250 orders.

All

Multiple Column DISTINCT

intermediate

Find all unique department-job title combinations

SELECT DISTINCT

department,

job_title

FROM employees

ORDER BY department, job_title;

department | job_title

Engineering | Engineer

Engineering | Senior Engineer

Sales | Account Manager

Sales | Sales Rep

DISTINCT on multiple columns returns unique combinations. Each (department, job_title) pair appears once.

All

DISTINCT with GROUP BY

intermediate

Count unique job titles per department