SQL Tutorial 30: Write a Query to Find First Sale Where Next Sale is 30+ Days Later

SQL Tutorial 30: Write a Query to Find First Sale Where Next Sale is 30+ Days Later

61 Lượt nghe
SQL Tutorial 30: Write a Query to Find First Sale Where Next Sale is 30+ Days Later
Watch the entire SQL playlist here: https://www.youtube.com/playlist?list=PL_neLGJ5LPUzPIg96tf7rXb57D6-3n5Pq In this advanced SQL Server tutorial, learn how to write a query that finds the first sale for each book where the next sale occurs over 30 days later. Using only one table, this challenge tests your skills with window functions, date logic, and row filtering, perfect for interviews and real-world scenarios. Table Schema: CREATE TABLE BookSales ( SaleID INT PRIMARY KEY, BookID INT, SaleDate DATE, Price DECIMAL(10, 2) ); INSERT INTO BookSales (SaleID, BookID, SaleDate, Price) VALUES (1, 101, '2023-01-05', 19.99), (2, 101, '2023-01-10', 21.99), (3, 101, '2023-02-01', 25.99), (4, 102, '2023-01-15', 17.50), (5, 102, '2023-02-05', 18.00), (6, 102, '2023-03-01', 22.00), (7, 103, '2023-01-20', 10.00), (8, 103, '2023-02-20', 15.00), (9, 104, '2023-01-01', 5.00), (10, 104, '2023-04-01', 8.00); -- Expected Output: -- BookID FirstSaleDate NextSaleDate -- 103 2023-01-20 2023-02-20 -- 104 2023-01-01 2023-04-01 Relevant Hashtags: #SQL #Database #SQLQuery #SQLTutorial #LearnSQL #DataScience #SQLPractice #TechInterview #Programming #SoftwareDevelopment#SoftwareDevelopment