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, I'd be happy to help you create a SQL interview question that tests a candidate's skills on Slowly Changing Dimension (SCD) Type 2, specifically in the context of a Netflix-like app. Here's a possible scenario and set of questions:
Scenario:
You are working as a data engineer for a new streaming platform, "StreamFlix," that offers movies and TV shows to its subscribers. The company wants to track the changes in the metadata of its content library over time, such as the addition or removal of titles, changes in ratings, and updates to the cast and crew. To achieve this, you have been tasked with designing a data model that supports SCD Type 2 for the content metadata.
Table Schemas:
Here are the table schemas you can provide to the candidate:
content table:
Column Name
Data Type
Description
content_id
INT
Unique identifier for each content item
title
VARCHAR(50)
Title of the movie or TV show
release_year
INT
Year of release
rating
DECIMAL(3,1)
Average rating (out of 10)
created_at
DATETIME
Timestamp when the content was first added
content_version table (SCD Type 2):
Column Name
Data Type
Description
content_version_id
INT
Unique identifier for each version of a content item
content_id
INT
Foreign key referencing the content table
title
VARCHAR(50)
Title of the movie or TV show
release_year
INT
Year of release
rating
DECIMAL(3,1)
Average rating (out of 10)
cast_crew
TEXT
List of cast and crew members (comma-separated)
start_date
DATETIME
Timestamp when this version became effective
end_date
DATETIME
Timestamp when this version was replaced (NULL if current)
Questions:
Design the data model for the content_version table to support SCD Type 2. Explain your design choices and any assumptions you made.
Expected answer: The candidate should propose adding a new table, content_version, with a primary key content_version_id, a foreign key content_id referencing the content table, and columns for the metadata attributes that can change over time (e.g., title, release_year, rating, and cast_crew). They should also include start_date and end_date columns to track the validity period of each version. The candidate should explain that this design allows for tracking changes to the metadata over time while maintaining a complete history of each content item.
Write a SQL query to insert a new version of a content item into the content_version table when its metadata changes. Assume that the new metadata is provided in a temporary table called new_content_metadata with the same schema as the content table.
Expected answer: The candidate should write a SQL query that inserts a new row into the content_version table with the new metadata and updates the end_date of the previous version. Here's an example query:
sql
INSERT INTO content_version (content_id, title, release_year, rating, cast_crew, start_date)
SELECT content_id, title, release_year, rating, cast_crew, NOW()
FROM new_content_metadata
WHERE content_id = :content_id;
UPDATE content_version
SET end_date = NOW()
WHERE content_id = :content_id AND end_date IS NULL;
Write a SQL query to retrieve the current metadata for a given content item, including its cast and crew members.
Expected answer: The candidate should write a SQL query that joins the content and content_version tables and filters for the current version of the content item. Here's an example query:
sql
SELECT c.content_id, cv.title, cv.release_year, cv.rating, cv.cast_crew
FROM content c
JOIN content_version cv ON c.content_id = cv.content_id
WHERE c.content_id = :content_id AND cv.end_date IS NULL;
Write a SQL query to retrieve the history of changes for a given content item, including the start and end dates of each version.
Expected answer: The candidate should write a SQL query that selects all rows from the content_version table for the given content item, ordered by the start_date. Here's an example query:
sql
SELECT content_version_id, content_id, title, release_year, rating, cast_crew, start_date, end_date
FROM content_version
WHERE content_id = :content_id
ORDER BY start_date;