Create a SQL interview that test a candidate's skills on slowly change dimension type 2, with settings of a netflix like app. Provide table schemas. Candidate need to design the data model, and write sql queries to answer the question
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure, here is a SQL interview scenario that tests a candidate's skills on slowly changing dimension type 2 (SCD2) in the context of a Netflix-like streaming service:
Table Schemas
movies table:
sql
CREATE TABLE movies (
movie_id INT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
release_year INT,
genre VARCHAR(255),
rating DECIMAL(3,1)
);
users table:
sql
CREATE TABLE users (
user_id INT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
subscriptions table:
sql
CREATE TABLE subscriptions (
subscription_id INT PRIMARY KEY,
user_id INT NOT NULL,
start_date DATE NOT NULL,
end_date DATE,
plan_type VARCHAR(255) NOT NULL,
PRIMARY KEY (subscription_id, user_id),
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
);
streaming_history table:
sql
CREATE TABLE streaming_history (
history_id INT PRIMARY KEY,
user_id INT NOT NULL,
movie_id INT NOT NULL,
stream_start TIMESTAMP NOT NULL,
stream_end TIMESTAMP,
is_watched BOOLEAN NOT NULL,
PRIMARY KEY (history_id, user_id),
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (movie_id) REFERENCES movies(movie_id) ON DELETE CASCADE
);
subscription_prices table (SCD2 table):
sql
CREATE TABLE subscription_prices (
price_id INT PRIMARY KEY,
plan_type VARCHAR(255) NOT NULL,
price DECIMAL(5,2) NOT NULL,
start_date DATE NOT NULL,
end_date DATE,
PRIMARY KEY (price_id, plan_type),
FOREIGN KEY (plan_type) REFERENCES subscriptions(plan_type) ON DELETE CASCADE
);
Interview Questions
Design a data model for the above scenario that supports SCD2 for the subscription prices table. Explain your design decisions and constraints.
Write a SQL query to list all the movies that a user has watched, sorted by the date and time they started streaming.
vbnet
SELECT m.title, sh.stream_start
FROM streaming_history sh
JOIN movies m ON sh.movie_id = m.movie_id
WHERE sh.user_id = ? -- replace with the user id
ORDER BY sh.stream_start;
Write a SQL query to find the total amount that a user has spent on subscriptions, broken down by plan type.
vbnet
SELECT sp.plan_type, SUM(sp.price * DATEDIFF('day', sp.start_date, sp.end_date)) as total_spent
FROM subscription_prices sp
JOIN subscriptions s ON sp.plan_type = s.plan_type
WHERE s.user_id = ? -- replace with the user id
GROUP BY sp.plan_type;
Write a SQL query to find the top 5 most popular movies (by number of streams) in the last 30 days.
vbnet
SELECT m.title, COUNT(sh.history_id) as num_streams
FROM movies m
JOIN streaming_history sh ON m.movie_id = sh.movie_id
WHERE sh.stream_start >= CURDATE() - INTERVAL 30 DAY
GROUP BY m.title
ORDER BY num_streams DESC
LIMIT 5;
Write a SQL query to find the number of active subscribers for each plan type in the last 30 days.
vbnet
SELECT sp.plan_type, COUNT(DISTINCT s.user_id) as num_subscribers
FROM subscription_prices sp
JOIN subscriptions s ON sp.plan_type = s.plan_type
WHERE s.start_date <= CURDATE() - INTERVAL 30 DAY AND (s.end_date IS NULL OR s.end_date > CURDATE())
GROUP BY sp.plan_type;