JOIN with Aggregation & GROUP BY: Functions
Module: Joins & Relationships
SELECT c.name, COUNT(o.id) AS order_count FROM customers c LEFT JOIN orders o ON c.id = o.customer_id GROUP BY c.id, c.name;
GROUP BY comes after JOIN
Include all non-aggregated SELECT columns in GROUP BY
Use COUNT(column) not COUNT(*) with LEFT JOIN
HAVING filters aggregated results
WHERE filters before aggregation
Core references in this topic include WHERE, =, <, >, <=, >=. 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.
<, >, <=, >=
Range comparison operators for less-than, greater-than, and inclusive boundary checks.
salary >= 80000
IS NULL / IS NOT NULL
Tests whether a value is missing. SQL NULL semantics require dedicated NULL predicates.
manager_id IS NULL
Never use = NULL or != NULL.
ANY / ALL
Compares one value against every or at least one value from a subquery result.
salary > ALL (SELECT salary FROM interns)
INTERVAL
Represents a duration that can be added to or subtracted from dates and timestamps.
order_date + INTERVAL '7 days'
PRIMARY KEY
Uniquely identifies each row and implicitly requires NOT NULL.
customer_id INT PRIMARY KEY
COUNT
Counts rows or non-NULL values depending on the argument.
COUNT(*)
SUM
Adds numeric values together across the current group or window frame.
SUM(revenue)
AVG
Calculates the arithmetic mean of numeric values.
AVG(salary)