Learn With Grito

SQL GROUP BY
Explained for Beginners

Raw data is useful. But business decisions usually need summaries. Not: “Show me 50,000 individual orders.” Instead: total revenue by city, customers by country.

Tutorial Series8 Mins ReadSQL Level 14

Raw data is useful. But business decisions usually need summaries.

Not: “Show me 50,000 individual orders.” Instead: total revenue by city, customers by country, orders by product category, signups by month, revenue by sales channel. That is exactly what SQL GROUP BY does.

The GROUP BY clause groups rows that share the same value, so you can calculate summaries. If you are learning SQL for data analysis, GROUP BY is one of the most important concepts to master. Because this is where SQL starts looking like real analytics work.

In this guide, you’ll learn:

  • what SQL GROUP BY is
  • GROUP BY syntax
  • GROUP BY with aggregate functions
  • grouping by multiple columns
  • GROUP BY with WHERE
  • GROUP BY with ORDER BY
  • real analyst examples
  • common mistakes
  • interview questions

This is one of the highest-value SQL concepts for data analysts.

What Is the SQL GROUP BY Clause?

The SQL GROUP BY clause groups rows that have the same values in specified columns. Then SQL can perform calculations for each group.

Example
SELECT city, COUNT(*)
FROM customers
GROUP BY city;

Meaning: Group customers by city, then count how many customers are in each city.

Simple idea: GROUP BY turns raw rows into grouped summaries.

Why SQL GROUP BY Matters for Data Analysts

This is real analytics SQL. Because analysts constantly summarize business data.

Real questions:

  • Sales Analysis: How much revenue came from each city?
  • Customer Analysis: How many users signed up in each country?
  • Product Analysis: How many orders per category?
  • Subscription Analysis: How many active vs inactive users?
  • Marketing Analysis: Revenue by campaign source?

Without GROUP BY

Raw data overload.

With GROUP BY

Business insights.

Basic SQL GROUP BY Syntax

Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;

Breakdown:

  • SELECT → output columns
  • FROM → source table
  • GROUP BY → grouping rule
  • aggregate → calculation per group

GROUP BY with Aggregate Functions

GROUP BY with COUNT()

Most common beginner example.

COUNT Example
SELECT city, COUNT(*)
FROM customers
GROUP BY city;

GROUP BY with SUM()

Perfect for revenue analysis.

SUM Example
SELECT city, SUM(revenue)
FROM orders
GROUP BY city;

GROUP BY with AVG(), MIN(), MAX()

Other Aggregates
-- Average order value
SELECT city, AVG(order_value)
FROM orders
GROUP BY city;

-- Highest order by city
SELECT city, MAX(revenue)
FROM orders
GROUP BY city;

Why GROUP BY Needs Aggregate Functions: Grouping alone usually is not enough. You can group by city to get unique cities, but most analyst use cases need calculations (count, sums, averages). That is where aggregate functions matter.

Advanced GROUP BY Features

GROUP BY with Multiple Columns

You can group by more than one column.

Multiple Columns
SELECT city, status, COUNT(*)
FROM customers
GROUP BY city, status;

Meaning: Group by city AND customer status.

GROUP BY with WHERE

WHERE filters rows before grouping.

With WHERE
SELECT city, SUM(revenue)
FROM orders
WHERE status = 'Completed'
GROUP BY city;

Meaning: Step 1: Keep only completed orders. Step 2: Group remaining rows by city. Step 3: Calculate revenue totals. This is extremely common analyst SQL.

GROUP BY with ORDER BY

With ORDER BY
SELECT city, SUM(revenue) AS total_revenue
FROM orders
GROUP BY city
ORDER BY total_revenue DESC;

Meaning: Rank cities by revenue. Very practical for dashboards.

Real Analyst Examples

Common Analyst Queries:

  • Revenue by City: SELECT city, SUM(revenue) AS total_revenue FROM orders GROUP BY city;
  • Users by Subscription Plan: SELECT plan_type, COUNT(*) AS user_count FROM users GROUP BY plan_type;
  • Orders by Product Category: SELECT category, COUNT(*) AS total_orders FROM orders GROUP BY category;
  • Revenue by Campaign Source: SELECT campaign_source, SUM(revenue) FROM marketing_orders GROUP BY campaign_source;

The Grito Factor GROUP BY is where many beginners accidentally become analysts. The moment you stop pulling rows and start summarizing patterns—revenue by city, churn by cohort, users by segment—you are doing real business analysis, not just writing SQL syntax.

Common Beginner Mistakes

1. Forgetting GROUP BY

SELECT city, COUNT(*) FROM customers; fails because SQL sees an individual column and an aggregate without a grouping rule.

2. Selecting Non-Grouped Columns

SELECT city, customer_name, COUNT(*) FROM customers GROUP BY city; fails. customer_name is neither grouped nor aggregated.

3. Confusing WHERE and HAVING

WHERE COUNT(*) > 10 is wrong. Correct: HAVING COUNT(*) > 10. Because aggregation happens after grouping.

4. Wrong Grouping Level

Grouping by city vs city, customer_name produces a completely different output.

5. Forgetting ORDER BY for Rankings

GROUP BY summarizes. If you want top cities, you must add ORDER BY total_revenue DESC.

Practice Questions

  1. Count customers by city.
  2. Calculate revenue by product.
  3. Average salary by department.
  4. Count users by city and plan.

What Comes Next?

Now that you can group data, the next step is filtering grouped results. Next: SQL HAVING Clause Explained. Because business analysis often needs filtering after aggregation.

Final Thoughts

SQL GROUP BY is one of the most important SQL concepts for data analysts.

It helps you:

  • summarize business data
  • calculate metrics
  • compare segments
  • analyze trends
  • turn raw data into insights

This is where SQL starts feeling like analytics. Master it early.

Grit Over Excuses.

— The Grito Team