DISTINCT & Removing Duplicates: Functions
Module: SQL Fundamentals
SELECT DISTINCT department FROM employees;
SELECT DISTINCT department, job_title FROM employees;
SELECT DISTINCT department
FROM employees
ORDER BY department ASC;
SELECT DISTINCT ON (customer_id)
customer_id, order_date, total
FROM orders
ORDER BY customer_id, order_date DESC;
DISTINCT applies to ALL columns in SELECT list
DISTINCT comes immediately after SELECT keyword
NULL values are treated as equal (multiple NULLs = one unique value)
ORDER BY applied after DISTINCT
Cannot use DISTINCT with aggregate functions directly
Core references in this topic include WHERE, =, LIKE. Learn what each one does, when to use it, and the execution or engine rules that matter.
WHERE
Filters rows before projection and sorting. It decides which rows continue through the query pipeline.
SELECT ... FROM table WHERE condition;
Most performance issues start with a weak WHERE clause or a missing supporting index.
=
Returns rows where the left and right values are exactly equal.
column = value
Use with exact matches. Do not use = NULL.
LIKE
Pattern-matching operator for wildcard string searches.
name LIKE 'Joh%'
EXISTS
Tests whether a correlated or non-correlated subquery returns at least one row.
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id)
ANY / ALL
Compares one value against every or at least one value from a subquery result.
salary > ALL (SELECT salary FROM interns)
PRIMARY KEY
Uniquely identifies each row and implicitly requires NOT NULL.
customer_id INT PRIMARY KEY
UNIQUE
Prevents duplicate values in a column or column combination.
email VARCHAR(255) UNIQUE
COUNT
Counts rows or non-NULL values depending on the argument.