FIRST_VALUE vs LAST_VALUE in SQL | Window Function Explained | SQL Interview Question
Crack DE with Mradul
0:00 / 0:00
FIRST_VALUE vs LAST_VALUE in SQL | Window Function Explained | SQL Interview Question
176 просмотров · 1 год назад
Crack DE with Mradul
524 подписчика
176 просмотров · 1 год назад
In this video, you'll learn how to use the FIRST_VALUE and LAST_VALUE window functions in SQL with a real-world dataset using proper date-based ordering.
🔍 What you’ll learn:
Difference between FIRST_VALUE and LAST_VALUE
Why LAST_VALUE needs a custom window frame
Common mistake with string-based month ordering
How to use date columns to avoid sorting issues
Real example with sales data (region, date, revenue)
💡 Perfect for beginners and intermediate SQL learners preparing for interviews or improving their analytical SQL skills.
-- 1. Create table
CREATE TABLE sales (
region VARCHAR(10),
month DATE,
revenue INT
);
-- 2. Insert values
INSERT INTO sales (region, month, revenue) VALUES
('East', '2024-01-01', 100),
('East', '2024-02-01', 150),
('East', '2024-03-01', 200),
('West', '2024-01-01', 120),
('West', '2024-02-01', 130);
🧠 Don’t forget to LIKE, SHARE, and SUBSCRIBE for more such SQL tutorials!
#CrackDEwithMradul #SQL #WindowFunctions #DataAnalytics #FIRSTVALUE #LASTVALUE #SQLTutorial #DataEngineering