Рет қаралды 423
In This video we have used analytical function like lead and date function like datepart. with the help of these functions and the implementation of those we have achieved the desired output..
The LEAD function is a window (or analytic) function in SQL that allows you to access data from a subsequent row in the same result set without using self-joins or subqueries. It’s extremely useful when you want to compare a value in the current row with a value in the next row.
Syntax:
LEAD(column_name, offset, default_value) OVER (PARTITION BY column_name ORDER BY column_name)
Use Case Scenarios
1. Comparing sales data of the current day with the next day.
2 . Identifying trends like customer churn by comparing current and next row values.
Explanation:
Retrieves the current salary and the salary of the next employee in the sorted list.
If there is no next employee, 0 is returned as the default value.
The DATEPART function extracts a specific part of a date, such as the year, month, day, hour, minute, etc., from a date or datetime value. This is often used for grouping, filtering, or analyzing data based on specific date components.
datepart: The part of the date you want to extract (e.g., year, month, day, hour).
date_expression: The date or datetime value from which the datepart will be extracted.
Use Case Scenarios
Extracting the year from a date to group sales data by year.
Filtering records for a specific month, like extracting data for "January".
Analyzing hourly trends in website traffic.
.
.
.
.
If you found this video helpful, don't forget to like, comment, and subscribe for more SQL interview tips and tutorials.
#SQL #SQLInterviewQuestions #LearnSQL #DataAnalysis #SQLQuery #InterviewTips #Database #Programming
.
.
.
.
-------------Table SCript-----------------------------
create table purchases (empid int, purchase_date date)
insert into purchases values (1, '01-08-2024')
insert into purchases values (1 ,'02-08-2024')
insert into purchases values (1 ,'03-08-2024')
insert into purchases values (1, '03-08-2024')
insert into purchases values (1 ,'03-08-2024')
insert into purchases values (2, '01-08-2024')
insert into purchases values (2 ,'03-08-2024')
insert into purchases values (3, '02-08-2024')
insert into purchases values (3 ,'03-08-2024')
insert into purchases values (3, '04-08-2024')
insert into purchases values (4 ,'02-08-2024')