Beginner

466. COUNT with JOIN

Learn how COUNT can measure related records after joining tables.

📘 Lesson 466 of 728 ⏱ 11 min 🎯 Intermediate

COUNT can be used with JOIN to count related records such as orders per customer, employees per department, or transactions per account.

Counting related records is one of the most common analytical SQL tasks.

Count the number of orders placed by each customer.

SQL
SELECT
    c.CustomerName,
    COUNT(o.OrderID) AS TotalOrders
FROM Customers c
LEFT JOIN Orders o
    ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerName;

Used for customer activity, transaction volume, employee headcount, product sales, and operational reporting.

  • COUNT can count related records.
  • COUNT(column) ignores NULL values.
  • LEFT JOIN can include entities with zero related records.
  • GROUP BY defines what is being counted for each group.
  • COUNT(*) behaves differently with outer joins.
Why use COUNT(column) with LEFT JOIN?

COUNT(column) ignores NULL values and therefore can return zero for entities without matching records.

What is a common use of COUNT with JOIN?

Counting related records such as orders per customer or employees per department.

Have you finished this lesson?

Introduction to SQL – All Lessons