SQL Interview: Calculate Days Between Posts | Datalemur Facebook Question | DATEDIFF, YEAR, MIN, MAX

Опубликовано: 19 Июль 2026
на канале: Data with Ritika
13
1

📌 COMMENT "PDF" below to get a FREE PDF of all MySQL & PostgreSQL built-in Date & Timestamp functions!

Master Facebook SQL interview questions by calculating the time difference between user posts. We'll solve it step-by-step using MySQL, and I'll explain every date and timestamp function, Group by used in the query.

━━━━━━━━━━━━━━━━━━━━━━━━━━
🔍 WHAT YOU'LL LEARN IN THIS VIDEO
━━━━━━━━━━━━━━━━━━━━━━━━━━

✅ DATEDIFF() — Calculate the difference in days between two dates
✅ DATE() — Extract just the date part from a datetime/timestamp value
✅ YEAR() — Filter records by year without manual date range calculation
✅ MAX() & MIN() — Find the latest and earliest post dates per user
✅ GROUP BY + HAVING — Group by user and filter for users with more than one post
✅ Why using DATE() inside DATEDIFF avoids timestamp comparison bugs

━━━━━━━━━━━━━━━━━━━━━━━━━━
📝 THE SQL SOLUTION
━━━━━━━━━━━━━━━━━━━━━━━━━━

Watch the Video

━━━━━━━━━━━━━━━━━━━━━━━━━━
🗓️ DATE & TIMESTAMP FUNCTIONS USED
━━━━━━━━━━━━━━━━━━━━━━━━━━

This query uses 4 key date functions that every SQL developer must know:

→ DATEDIFF(date1, date2): Returns the number of days between two dates. Always wraps date expressions, not raw timestamps, for clean results.
→ DATE(expression): Strips the time portion from a DATETIME or TIMESTAMP column, so comparisons stay date-level.
→ YEAR(date): Extracts the year from a date value — perfect for filtering annual data without writing BETWEEN clauses.
→ MAX() / MIN() on dates: Aggregates dates just like numbers — MAX returns the latest, MIN returns the earliest.

💡 Bonus tip: In PostgreSQL, use AGE() or date subtraction with EXTRACT(EPOCH FROM ...) instead of DATEDIFF — I cover this in the FREE PDF below!

━━━━━━━━━━━━━━━━━━━━━━━━━━
📄 FREE PDF — MySQL & PostgreSQL Date Functions
━━━━━━━━━━━━━━━━━━━━━━━━━━

Want a complete cheat sheet of ALL built-in date and timestamp functions in MySQL and PostgreSQL?

👇 COMMENT "PDF" on this video and I'll send you the free PDF!

Covers:
• MySQL: NOW(), CURDATE(), DATE_ADD(), DATE_SUB(), TIMESTAMPDIFF(), DATE_FORMAT(), EXTRACT(), LAST_DAY() and more
• PostgreSQL: CURRENT_DATE, AGE(), DATE_TRUNC(), TO_CHAR(), INTERVAL, EXTRACT(), MAKE_DATE() and more

Chapters:
00:00 Intro
00:14 The Problem walk through
01:58 Solution
02:35 Where clause
02:50 Comment down "Copy" or "PDF" to get your free copy of date functions
03:00 Group by
03:50 DATEDIFF
04:14 Max, Min functions
05:10 Imp catch
06:35 Final Solution

━━━━━━━━━━━━━━━━━━━━━━━━━━
🔔 SUBSCRIBE & TURN ON NOTIFICATIONS
━━━━━━━━━━━━━━━━━━━━━━━━━━

If this video helped you, hit the LIKE button and SUBSCRIBE — I post SQL interview solutions, data engineering tips, and Databricks tutorials every week.

#SQL #MySQL #SQLInterview #DATEDIFF #DateFunctions #DataEngineering #SQLTutorial #PostgreSQL