SQL Practice Logo

SQLPractice Online

DISTINCT & ALL: Mistakes

Module: Aggregate Functions & Grouping

SELECT DISTINCT department, employee_name FROM employees;

SELECT department, employee_name FROM employees;

If employee_name is unique, DISTINCT adds overhead without benefit. Only use DISTINCT when duplicates exist.

Only use DISTINCT when duplicates are actually present

Medium

Unnecessary DISTINCT when all rows are unique

SELECT department, DISTINCT job_title FROM employees;

SELECT DISTINCT department, job_title FROM employees;

DISTINCT is not per-column, it applies to entire row. Place after SELECT keyword.

DISTINCT goes after SELECT, applies to all selected columns

High

DISTINCT must come after SELECT, applies to all columns