Bird
Raised Fist0
PostgreSQLquery~3 mins

Why GROUP BY single and multiple columns in PostgreSQL? - Purpose & Use Cases

Choose your learning style10 modes available
The Big Idea

What if you could get instant answers from your data without counting a single row yourself?

The Scenario

Imagine you have a big list of sales records on paper, and you want to find out how many sales each salesperson made or how many sales happened in each city.

You try to count them by scanning the list manually, writing down each name or city and adding up the numbers yourself.

The Problem

This manual counting is slow and tiring. You might miss some entries or count some twice. If the list changes, you have to start all over again.

It's easy to make mistakes, and it takes a lot of time to get the right totals.

The Solution

The GROUP BY command in SQL lets you quickly group your data by one or more columns, like salesperson or city, and then calculate totals or averages for each group automatically.

This saves time, reduces errors, and updates instantly when your data changes.

Before vs After
✗ Before
Count sales for each person by scanning paper and writing totals.
✓ After
SELECT salesperson, COUNT(*) FROM sales GROUP BY salesperson;
What It Enables

You can easily summarize and analyze large data sets by any combination of columns, unlocking insights that would be impossible to find manually.

Real Life Example

A store manager wants to see total sales per product category and per store location to decide where to stock more items.

Using GROUP BY on category and location columns gives a clear summary instantly.

Key Takeaways

Manual counting is slow and error-prone.

GROUP BY groups data by one or more columns to summarize it.

This makes data analysis fast, accurate, and flexible.

Practice

(1/5)
1. What does the GROUP BY clause do in a PostgreSQL query?
easy
A. It updates values in the specified columns.
B. It sorts the rows in ascending order.
C. It deletes duplicate rows from the table.
D. It groups rows that have the same values in specified columns.

Solution

  1. Step 1: Understand the purpose of GROUP BY

    The GROUP BY clause collects rows that share the same value in one or more columns into groups.
  2. Step 2: Differentiate from other clauses

    Unlike ORDER BY which sorts rows, GROUP BY organizes rows into groups for aggregation.
  3. Final Answer:

    It groups rows that have the same values in specified columns. -> Option D
  4. Quick Check:

    GROUP BY = groups rows by column values [OK]
Hint: GROUP BY collects rows sharing column values into groups [OK]
Common Mistakes:
  • Confusing GROUP BY with ORDER BY
  • Thinking GROUP BY deletes duplicates
  • Assuming GROUP BY updates data
2. Which of the following is the correct syntax to group data by two columns, category and region?
easy
A. SELECT category, region, COUNT(*) FROM sales GROUP BY category, region;
B. SELECT category, region, COUNT(*) FROM sales GROUP BY category region;
C. SELECT category, region, COUNT(*) FROM sales GROUP BY (category, region);
D. SELECT category, region, COUNT(*) FROM sales GROUP category, region;

Solution

  1. Step 1: Check correct GROUP BY syntax

    In PostgreSQL, multiple columns are separated by commas in GROUP BY without parentheses.
  2. Step 2: Validate each option

    SELECT category, region, COUNT(*) FROM sales GROUP BY category, region; uses commas correctly; SELECT category, region, COUNT(*) FROM sales GROUP BY category region; misses comma; SELECT category, region, COUNT(*) FROM sales GROUP BY (category, region); uses parentheses incorrectly; SELECT category, region, COUNT(*) FROM sales GROUP category, region; misses BY keyword.
  3. Final Answer:

    SELECT category, region, COUNT(*) FROM sales GROUP BY category, region; -> Option A
  4. Quick Check:

    GROUP BY columns separated by commas [OK]
Hint: Separate multiple columns with commas in GROUP BY [OK]
Common Mistakes:
  • Omitting commas between columns
  • Using parentheses around columns
  • Forgetting the BY keyword
3. Given the table orders with columns customer_id, product_id, and quantity, what will this query return?
SELECT customer_id, SUM(quantity) FROM orders GROUP BY customer_id;
medium
A. Total quantity ordered by each customer.
B. Total quantity ordered by each product.
C. Total quantity ordered by each customer and product.
D. Syntax error due to missing GROUP BY columns.

Solution

  1. Step 1: Analyze the SELECT and GROUP BY columns

    The query groups rows by customer_id and sums quantity for each group.
  2. Step 2: Understand the aggregation result

    SUM(quantity) calculates total quantity per customer_id, ignoring product_id.
  3. Final Answer:

    Total quantity ordered by each customer. -> Option A
  4. Quick Check:

    GROUP BY customer_id sums quantity per customer [OK]
Hint: GROUP BY column matches SELECT non-aggregates [OK]
Common Mistakes:
  • Assuming product_id affects grouping
  • Thinking query returns per product totals
  • Confusing aggregation with filtering
4. Identify the error in this query:
SELECT product_id, region, COUNT(*) FROM sales GROUP BY product_id;
medium
A. COUNT(*) cannot be used with GROUP BY.
B. Missing region in GROUP BY causes error.
C. product_id cannot be grouped alone.
D. Syntax error due to missing FROM clause.

Solution

  1. Step 1: Check SELECT columns vs GROUP BY columns

    SELECT includes product_id and region, but GROUP BY only has product_id.
  2. Step 2: Understand PostgreSQL grouping rules

    All non-aggregated columns in SELECT must appear in GROUP BY; region is missing.
  3. Final Answer:

    Missing region in GROUP BY causes error. -> Option B
  4. Quick Check:

    All SELECT non-aggregates must be in GROUP BY [OK]
Hint: Include all non-aggregated SELECT columns in GROUP BY [OK]
Common Mistakes:
  • Forgetting to add all columns to GROUP BY
  • Thinking COUNT(*) disallows grouping
  • Ignoring SELECT and GROUP BY mismatch
5. You want to find the total sales quantity for each product in each region from the sales table with columns product_id, region, and quantity. Which query correctly groups and sums the data?
hard
A. SELECT product_id, SUM(quantity) FROM sales GROUP BY product_id;
B. SELECT product_id, region, SUM(quantity) FROM sales GROUP BY product_id;
C. SELECT product_id, region, SUM(quantity) FROM sales GROUP BY product_id, region;
D. SELECT product_id, region, SUM(quantity) FROM sales;

Solution

  1. Step 1: Identify grouping needs

    We want totals per product and per region, so both columns must be grouped.
  2. Step 2: Check each option's GROUP BY clause

    SELECT product_id, region, SUM(quantity) FROM sales GROUP BY product_id, region; groups by product_id and region, matching SELECT columns; others miss region or GROUP BY entirely.
  3. Final Answer:

    SELECT product_id, region, SUM(quantity) FROM sales GROUP BY product_id, region; -> Option C
  4. Quick Check:

    Group by all non-aggregated columns for correct totals [OK]
Hint: Group by all selected non-aggregated columns [OK]
Common Mistakes:
  • Grouping by only one column when selecting multiple
  • Omitting GROUP BY causing aggregation errors
  • Selecting columns without aggregation or grouping
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard