Knowledge Guide
HomeDatabasesSQL Practice Problems

Ad-Free Sessions

Problem

Table: Playback

+-------------+------+
| Column Name | Type |
+-------------+------+
| session_id  | int  |
| customer_id | int  |
| start_time  | int  |
| end_time    | int  |
+-------------+------+
session_id is the column with unique values for this table.
customer_id is the ID of the customer watching this session.
The session runs during the inclusive interval between start_time and end_time.
It is guaranteed that start_time <= end_time and that two sessions for the same customer do not intersect.

Table: Ads

+-------------+------+
| Column Name | Type |
+-------------+------+
| ad_id       | int  |
| customer_id | int  |
| timestamp   | int  |
+-------------+------+
ad_id is the column with unique values for this table.
customer_id is the ID of the customer viewing this ad.
timestamp is the moment of time at which the ad was shown.

Problem Definition

Write a solution to report all the sessions that did not get shown any ads.

Return the result table in any order.

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To solve this problem, the approach involves using SQL queries to identify sessions that did not display any ads. The data is spread across two tables, Playback and Ads, with the former containing information about customer sessions and the latter containing details about ads viewed by customers during specific timestamps.

The solution employs the WHERE NOT IN clause, utilizing a subquery to identify the session_id values that correspond to sessions where ads were shown. The subquery involves an INNER JOIN between the Playback and Ads tables, where the customer_id in both tables match and the timestamp of the ad falls within the inclusive interval of the session (start_time to end_time). This subquery returns distinct session_id values for sessions with ads.

The main query then selects session_id values from the Playback table where the session_id is not present in the result of the subquery. This effectively identifies sessions that did not have any corresponding ads.

The final result is presented as a table of session IDs that did not show any ads. The order of the result table is not specified, as indicated in the problem statement.

SELECT session_id FROM Playback WHERE session_id NOT IN (SELECT DISTINCT session_id FROM Playback P INNER JOIN Ads A ON P.customer_id = A.customer_id WHERE A.timestamp >= P.start_time AND A.timestamp <= P.end_time);

Let's break down the query step by step:

Step 1: Identify sessions with ads

We need to find sessions that had at least one ad shown during the playback.

SELECT DISTINCT session_id FROM Playback P INNER JOIN Ads A ON P.customer_id = A.customer_id WHERE A.timestamp >= P.start_time AND A.timestamp <= P.end_time;

Output After Step 1:

+------------+ | session_id | +------------+ | 1 | | 4 | +------------+

Step 2: Exclude sessions with ads

We use NOT IN to exclude sessions with ads from the Playback table.

SELECT session_id FROM Playback WHERE session_id NOT IN (SELECT DISTINCT session_id FROM Playback P INNER JOIN Ads A ON P.customer_id = A.customer_id WHERE A.timestamp >= P.start_time AND A.timestamp <= P.end_time);

Final Output:

+------------+ | session_id | +------------+ | 2 | | 3 | | 5 | +------------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Ad-Free Sessions? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Ad-Free Sessions** (Databases) and want to truly understand it. Explain Ad-Free Sessions from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Ad-Free Sessions** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Ad-Free Sessions** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Ad-Free Sessions** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes