Beginner

388. GROUP BY Multiple Columns

Learn how to create groups using multiple columns.

📘 Lesson 388 of 728 ⏱ 11 min 🎯 Intermediate

GROUP BY can use multiple columns to create groups based on unique combinations of values across those columns.

Business analysis often requires summaries at multiple levels, such as department and location or region and product.

Calculate total salary for each department and location.

SQL
SELECT
    Department,
    Location,
    SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY
    Department,
    Location;

This technique is useful for detailed management reports, regional analysis, departmental comparisons, and multidimensional reporting.

  • Multiple columns can define a group.
  • Each unique combination creates a group.
  • Aggregates are calculated for each combination.
  • It supports detailed analysis.
  • Column order can affect result ordering but not grouping combinations.
What happens when multiple columns are used in GROUP BY?

SQL creates groups based on unique combinations of the specified column values.

Why use multiple-column GROUP BY?

It allows more detailed summaries, such as totals by department and location.

Have you finished this lesson?

Introduction to SQL – All Lessons