In this video, we solve a real SQL interview question asked by Myntra that tests your understanding of date calculations, window functions, and analytical thinking.
Problem Statement
Assume today's date is 2021-01-01.
You are given a table containing user visit dates. Your task is to write an SQL query that, for each user_id, finds the largest gap (number of days) between consecutive visits.
If the visit is the last recorded visit for the user, then calculate the gap between that visit and today's date (2021-01-01).
Table Structure
CREATE TABLE user_visits (
user_id INT,
visit_date DATE
);
Sample Data
INSERT INTO user_visits (user_id, visit_date) VALUES
(1, '2020-11-28'),
(1, '2020-10-20'),
(1, '2020-12-03'),
(2, '2020-10-05'),
(2, '2020-12-09'),
(3, '2020-11-11');
What You Will Learn in This Video
* How to analyze SQL interview questions step by step
* Using window functions like LEAD()
* Handling date differences in SQL
* Dealing with edge cases like the last visit
* Writing clean and optimized SQL queries
This type of question is commonly asked in Data Analyst, Data Scientist, and SQL Developer interviews at top tech companies.
If you find this helpful, like the video and subscribe to the channel for more SQL interview questions and data analytics tutorials.
#SQL #SQLInterviewQuestions #MyntraInterview #DataAnalytics #WindowFunctions #LearnSQL #SQLPractice