Skip to article content
FrontendXLesson 10 / 24

SQL joins and aggregation

Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.

intermediate · about 55 minutes · 2 exercises

  • Predict
  • Implement

What you will be able to do

#
  • Combine tables with INNER and LEFT JOIN and explain the difference.
  • Aggregate with GROUP BY and filter groups with HAVING.
  • Avoid the fan-out mistake that inflates counts.

Overview

#

Joins combine rows from two tables on a matching condition. Aggregates collapse many rows into one per group.

INNER versus LEFT

INNER JOIN keeps only matching rows. LEFT JOIN keeps every row from the left table and fills unmatched right columns with NULL.

snippet.sql
SELECT a.id, a.title, count(c.id) AS comment_count
FROM articles a
LEFT JOIN comments c ON c.article_id = a.id
GROUP BY a.id, a.title
HAVING count(c.id) >= 0
ORDER BY comment_count DESC;

A LEFT JOIN plus count(c.id) counts 0 for articles with no comments, while count(*) would count 1 for the NULL row.

WHERE versus HAVING

WHERE filters rows before grouping. HAVING filters groups after aggregation. To find authors with more than five articles:

snippet.sql
SELECT author_id, count(*) AS n
FROM articles
GROUP BY author_id
HAVING count(*) > 5;

The fan-out trap

Joining two one-to-many tables in one query multiplies rows, so count(*) overstates. Aggregate each side in a subquery or use count(DISTINCT ...).

Common pitfalls

  • Putting an aggregate condition in WHERE instead of HAVING.
  • Counting * after a LEFT JOIN when you mean matched rows.
  • Joining two child tables and then summing a value twice.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    An article has no comments. What does count(c.id) return for it in the LEFT JOIN query above?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.sql
    LEFT JOIN comments c ON c.article_id = a.id
    count(c.id)
    Choose the best answer
  2. Exercise 2Implement

    Write a query returning each author id and the number of published articles, only for authors with at least 3.

    starter.sql
    -- articles(id, author_id, status)
    

    Reveal the reference answer
    reference.sql
    SELECT author_id, count(*) AS published_count
    FROM articles
    WHERE status = 'published'
    GROUP BY author_id
    HAVING count(*) >= 3
    ORDER BY published_count DESC;

    Authored expected output: One row per qualifying author with a count of at least 3.

    Filter rows with WHERE, group by author, and filter groups with HAVING because the condition uses an aggregate.

Common misconceptions

#
  • LEFT JOIN and INNER JOIN differ only in syntax: LEFT JOIN preserves unmatched left rows, which changes counts and nullability.
  • WHERE and HAVING are interchangeable: WHERE runs before grouping and cannot reference aggregates.

Follow-up practice

#
  • backend-databases-normalization

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.