📊 LeetCode SQL: User Activity in the Past 30 Days | Master DATEDIFF, GROUP BY, and HAVING
Notes
🚀 SQL Interview Prep – Problem #24 of 50!
In this video, we solve the "User Activity for the Past 30 Days (I)" problem from LeetCode, the 24th question in our 50 SQL problem series. This problem is essential for mastering date filtering, user activity tracking, and grouping data in SQL.
🔍 What This Problem Will Teach You:
✅ Using DATEDIFF to filter data based on date ranges
✅ Counting distinct users with COUNT(DISTINCT user_id)
✅ Grouping data by date (GROUP BY activity_date)
✅ Applying date constraints with HAVING
📌 LeetCode Problem Link: https://leetcode.com/problems/user-activity-for-the-past-30-days-i
💡 Like this video?
👍 Hit the like button to support the channel!
📩 Comment below with your approach or any doubts!
🔔 Subscribe now for more SQL tutorials, coding interview solutions, and database tips!
Comparison of Two Solutions:
1️⃣ First Solution:
Uses WHERE with DATEDIFF to filter activity records within the past 30 days.
Efficient because filtering happens before grouping, reducing unnecessary computations.
2️⃣ Second Solution:
Uses HAVING with DATEDIFF after grouping, applying the filter later in the process.
Less efficient as it groups all data first before filtering, making it slower for large datasets.
**Alert⚠️ : However it also depends on the data [so for knowledge remember it]
#SQL #LeetCode #Database #SQLQueries #DataAnalysis #SQLInterview #UserActivity #WebAnalytics
Discussion
Sign in to ask a question or share what you learned.