MySQL Joins, Subqueries, and CTEs Cheatsheet/Join and aggregate child rows

Count child rows per parent.

Section: joins

Join and aggregate child rows

sql
sql
SELECT u.id, u.email, COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
GROUP BY u.id, u.email;
Explanation

A classic reporting query for dashboards and admin pages.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing joins
Inner join two tables
Return rows where both tables match.
OpenIn sheetsqlsame section
Left join to keep unmatched parent rows
Return all rows from the left table and matching rows from the right table.
OpenIn sheetsqlsame section
Self join a table
Join a table to itself using aliases.
OpenIn sheetsqlsame section
Join to a derived table
Compute an aggregate once and join it back.
OpenIn sheetsql1 tag match
Filter with a subquery
Return users who have at least one paid order.
Use EXISTS for correlated filtering
Return users who have orders.