SQL from zero · Chapter 15 of 37

One feed from many tables

By the end of this chapter you can combine the results of two queries into one: stack them, keep only the rows both return, or take one away from the other. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Joins
  • 5 tasks
  • SQL
  • In the all labs plan

The first 2 chapters of SQL from zero are free. This one comes with the all labs plan.

Why you need this

Meera is designing a new profile page: one list of everything a member has done, posts and comments mixed together, newest first. Rohan wants lists for his dashboard too: members who did one thing and also another, and members who never did something at all.

Posts and comments live in different tables, and a join puts tables side by side, not on top of each other. Joining Vikram's 4 posts to his 7 comments would pair every post with every comment. Without a way to stack results, you would run two queries and glue the lists together in code, and sort them there too.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    meera-feed.sql: meera's posts and comments in one list

    List everything meera has written in one list: all her posts (deleted ones too) and all her comments. kind is the text post for a post and comment for a comment.

    Return three columns: kind, content and created_at. Newest first; when two rows have the same created_at, sort them by kind A to Z.

  2. Task 2

    anya-contacts.sql: everyone anya has messaged with

    List everyone anya has exchanged a message with: the members she sent a message to, and the members who sent one to her. Each member appears once, even if messages went both ways.

    Return one column: username. Any order.

  3. Task 3

    posted-and-commented.sql: members who posted and commented

    List the members who have written at least one post (deleted ones count) and at least one comment. Members who left count too. Each member appears once.

    Return one column: username. Any order.

  4. Task 4

    never-liked.sql: members who never liked a post

    List the members who have never liked a post. Members who left are included like anyone else.

    Return one column: username. Any order.

  5. Task 5 · interview question

    Interview: june-actions.sql, each member's actions in June 2026

    For each member, count everything they did in June 2026 (India time, the lab's clock): the posts they wrote (deleted ones count), the comments they wrote and the likes they gave, added up. Every action counts: three likes are three actions, not one. Only members with at least one action appear.

    Return two columns: username and actions. Any order.

What you will learn

  • UNION ALL: stack the rows of one query under another's.
  • UNION: stack them and keep each row once.
  • INTERSECT and EXCEPT: the rows both queries return, and the rows of the first that the second does not.
  • A stacked result as a table: count and group it like any other.

More in Joins

Build "One feed from many tables" yourself

MingleSphereQL, a social app, needs answers from its data every day. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of SQL from zero are free. This one comes with the all labs plan.