LogoInterview Master

SQL Learning Hub

SQL GROUP BY Clause

Understanding the SQL GROUP BY Clause

The GROUP BY clause is a fundamental part of SQL used to group rows that have the same values in one or more specified columns into a summary row. Think of it as creating categories or buckets based on shared characteristics.

GROUP BY is almost always used in conjunction with aggregate functions like COUNT, SUM, AVG, MIN, or MAX. These functions perform calculations on each group, allowing you to summarize large datasets into meaningful insights (e.g., total sales per region, average views per user, count of orders per customer).

Basic Syntax

Key GROUP BY Concepts & Interview Questions

  • What is the processing order of WHERE, GROUP BY, and HAVING?
  • Explain the difference between filtering with WHERE and filtering with HAVING in a query with GROUP BY.
  • How does GROUP BY treat NULL values in the grouping column(s)? (Answer: Usually forms a separate group for NULLs).
  • What happens when you group by multiple columns? What defines a unique group?

GROUP BY Fundamentals

Simple Grouping and Aggregation

Group rows based on a single column and apply an aggregate function.

This query collapses all rows for each distinct user_id into a single summary row, showing the user ID and the count of videos associated with that ID.

Including Descriptive Columns

To show descriptive information alongside aggregated values, join tables and include the necessary columns in both SELECT and GROUP BY.

Because u.username is selected and not aggregated, it must be listed in the GROUP BY clause along with u.user_id (even if user_id functionally determines username, standard SQL requires listing all non-aggregated selected columns).

Grouping by Expressions

You can group by the result of an expression or function applied to a column.

Here, we group users based on the year extracted from their join_date.

Grouping by Multiple Columns

Grouping by more than one column creates groups based on the unique combination of values across all specified grouping columns.

Creating Finer-Grained Groups

Each unique combination of the grouping columns forms a distinct group.

This query creates summary rows like (Video 1, 'like', 50), (Video 1, 'comment', 10), (Video 2, 'like', 120), etc., showing the breakdown of interaction types for each video.

Multi-Column Grouping with Joins

Combine joins with multi-column grouping for detailed categorical summaries.

This query groups by both the user and the month they uploaded videos, allowing analysis of user performance trends over time.

How GROUP BY Handles NULL Values

A common point of confusion for SQL beginners is how GROUP BY handles NULL values in the grouping columns.

NULL Values Form Their Own Group

In SQL, NULL represents an unknown or missing value. When grouping, all NULL values are considered equal to each other and are placed into a single group.

This query will produce a result set where one of the rows has NULL for interaction_type and the count of all interactions that don't have a type assigned.

Handling NULL Values in GROUP BY

The COALESCE function replaces NULL values with an explicit string, making the results more user-friendly and clarifying that these are interactions without assigned types.

Filtering Groups with HAVING

The HAVING clause is specifically designed to filter results *after* the GROUP BY clause has been applied and aggregate functions have been calculated. It filters based on the aggregated values.

Filtering Based on Aggregate Results

HAVING allows us to keep only those user groups where the video count (calculated by COUNT(*)) is greater than 3.

Crucial Distinction: WHERE vs. HAVING

  • WHERE Clause: Filters individual rows BEFORE they are processed by GROUP BY and aggregate functions like COUNT.
    Example: WHERE country = 'USA' (filters rows before grouping by user).
  • HAVING Clause: Filters entire groups AFTER grouping and aggregation occur. Conditions in HAVING typically involve aggregate functions.
    Example: HAVING SUM(views) > 1000 (filters user groups based on their total views).

Think of the SQL query execution order (simplified): FROM → WHERE → GROUP BY → Aggregate Functions → HAVING → SELECT → ORDER BY.

Learn more about HAVING

Combining WHERE and HAVING

Use both clauses for multi-stage filtering.

This efficiently filters down the data before grouping and then applies a final filter on the aggregated counts.

Summary

The GROUP BY clause is essential for data summarization and analysis in SQL:

  • It groups rows with identical values in the specified grouping column(s).
  • It's used with aggregate functions (COUNT, SUM, AVG, MIN, MAX) to perform calculations on each group.
  • You can group by multiple columns to create more specific subgroups based on unique combinations.
  • You can group by expressions derived from columns (e.g., grouping by month or year).
  • Use the WHERE clause to filter rows *before* grouping.
  • Use the HAVING clause to filter groups *after* aggregation, typically based on aggregate results.

Mastering GROUP BY along with aggregate functions and the HAVING clause is fundamental for anyone performing data analysis or reporting with SQL.

Test Your Knowledge

Take a quick quiz to reinforce what you've learned about SQL GROUP BY Clause

Ready for hands-on SQL practice?

We have 200+ questions on real companies and real products.

Find a question to practice
Dawn AI