0% completed
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
Output
Try It Yourself
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 | +------------+
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible