Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

aggregates-group-by

Aggregates and GROUP BY

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Aggregates and GROUP BY: the SQL patterns, schema habits, and query reasoning you need before advancing in SQL.

Aggregations power dashboards and reports—GROUP BY rules and HAVING filters appear in every BI interview.

You will apply Aggregates and GROUP BY in contexts like: Reporting dashboards, cohort analysis, and feature tables for ML pipelines.

Copy SQL from each lesson into SQLite (sqlite3), DB Fiddle, or local Postgres—read result grids and row counts. The in-browser SQL lab (sql_sandbox) will run queries when the runner ships; until then, local clients are the practice path.

When you can explain the previous lesson's ideas without copying example queries verbatim.

Aggregate functions collapse many rows into summary values. GROUP BY computes aggregates per group—foundation of dashboards and Data Science SQL summaries.

Aggregate functions

SELECT COUNT(*) AS order_count,
       SUM(total) AS revenue,
       AVG(total) AS avg_order,
       MIN(ordered_at) AS first_order,
       MAX(ordered_at) AS last_order
FROM orders;

Practice: Seed customers and orders sample data, then run each query. Verify row counts manually.

GROUP BY

SELECT customer_id,
       COUNT(*) AS order_count,
       SUM(total) AS spent
FROM orders
GROUP BY customer_id;

Every non-aggregated SELECT column must appear in GROUP BY (or be functionally dependent in engines that allow it).

Join then aggregate

SELECT c.name,
       COUNT(o.id) AS order_count,
       COALESCE(SUM(o.total), 0) AS spent
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name;

LEFT JOIN + GROUP BY includes customers with zero orders (COUNT(o.id) is 0).

Important interview questions and answers

  1. Q: COUNT(*) vs COUNT(col)?
    A: COUNT(*) counts rows; COUNT(col) ignores NULLs in col.
  2. Q: GROUP BY error symptom?
    A: Selecting bare column not in GROUP BY causes error in strict SQL modes.

Self-check

  1. Which aggregate computes average?
  2. Why GROUP BY customer_id before summing totals?

Pitfall: Every selected non-aggregate column must appear in GROUP BY in strict SQL.

Interview prep

COUNT(*) vs COUNT(col)?

COUNT(*) all rows; COUNT(col) ignores NULLs in col.

GROUP BY rule?

Non-aggregated SELECT columns must be in GROUP BY.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • COUNT(*) vs COUNT(col)?
  • GROUP BY rule?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump