SQL Window Function | How to write SQL Query using RANK, DENSE RANK, LEAD/LAG | SQL Queries Tutorial

SQL Window Function | How to write SQL Query using RANK, DENSE RANK, LEAD/LAG | SQL Queries Tutorial

techTFQ

3 года назад

952,153 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Alf joy
Alf joy - 16.09.2023 19:34

so simple and understandable, Thank you so much

Ответить
Anjana Walia
Anjana Walia - 09.09.2023 06:28

Very useful content! Thanks

Ответить
Sagar Ranpise
Sagar Ranpise - 07.09.2023 10:05

Thank you for explaining so well👍

Ответить
manju rk
manju rk - 29.08.2023 19:38

Awesome explanation.
Thanks for thr effort and time.

Ответить
Pawan Yadav
Pawan Yadav - 28.08.2023 04:07

Hi Sir Good Morning,
Please solve the queries below:-
Dept id 10,20 update where 10 update with 30 and 20 update with 40 in single queries.

Ответить
prithika Batra
prithika Batra - 26.08.2023 18:53

here why are not we using salary_range instead of full query in case statement

Ответить
Olajide Bello
Olajide Bello - 26.08.2023 08:08

This was really helpful, was working on a challenge that involved checking if the next employee had a higher salary racked my brain for hours and came here only to see it in seconds

Ответить
Priya Jangid
Priya Jangid - 24.08.2023 09:04

can u also add date functions dedicated video, like dateadd, datediff , etc ?

Ответить
VAIBHAV PATHARKAR
VAIBHAV PATHARKAR - 22.08.2023 06:31

Ye video dekh kar maza aaaygaya , sahi mein beautiful explanation..pl pl pl take full course of sql. Pl take sql advanced questions also. Please please Please please Please please Please please Please please Please please Please please Please please Please please Please please Please please Please please Please please Please please Please please Please please

Ответить
Teju
Teju - 21.08.2023 12:19

Can we have multiple columns inside over clause partition by?

Ответить
Subbu
Subbu - 20.08.2023 14:44

Awesome video. But I'm getting error while I was downloading We couldn't find the page you were looking for. This is either because

Ответить
sailu sailu
sailu sailu - 20.08.2023 14:07

nice

Ответить
Maab Khan
Maab Khan - 17.08.2023 18:00

Good explanation.
Just to add -
when you use the ROW_NUMBER() function, it operates on the rows that have already passed through the WHERE clause filtering. This is important to note because the row numbering is affected by the filtered data, and you won't get row numbers for rows that were filtered out by the WHERE clause.

example -
SELECT
EmployeeID,
FirstName,
LastName,
Salary,
ROW_NUMBER() OVER (ORDER BY Salary DESC) AS row_num
FROM
Employees
WHERE
Salary > 50000;

i was expecting output row_num to be discontinuous as i thought row_num is assigned before where , but first where is applied then row_num is assigned.

In SQL, the order of execution of clauses is as follows in order:

FROM: The initial source of data.
WHERE: Filters out rows that do not meet the specified conditions.
GROUP BY: Groups the remaining rows into summary rows by one or more columns.
HAVING: Filters out groups that do not meet the specified conditions.
SELECT: Computes expressions, produces computed columns, and computes scalar expressions.
DISTINCT: Removes duplicate rows from the result set.
ORDER BY: Sorts the rows in the result set by the specified column(s).
TOP/LIMIT: Returns the specified number of rows.
OFFSET/FETCH: Skips a specified number of rows before returning the next set of rows.
WINDOW FUNCTIONS: Applies window functions like ROW_NUMBER(), RANK(), etc.

Ответить
Mohsin Mahmood
Mohsin Mahmood - 17.08.2023 06:38

Hi; Thank you for making it so simple to understand. Great job.

Ответить
Manjuu Thomas
Manjuu Thomas - 17.08.2023 01:51

can i get the videos to download of the bootcamp of sql whic was conducted from june 2020 to aug 2023

Ответить
Tom Anto
Tom Anto - 15.08.2023 09:30

Link for downloading query is broken please fix

Ответить
Dawood Ahmed
Dawood Ahmed - 14.08.2023 22:26

Very very helpful! Excellent work super easy to understand! God bless you with all the success. Thank you Toufiq bhai 😀

Ответить
Palash Mahata
Palash Mahata - 14.08.2023 13:54

This was very very helpful to me. I'm currently learning MYSQL and this was the best explanation of windows functions I came across. I would love to see more videos on MYSQL!

Ответить
murugan selva
murugan selva - 14.08.2023 13:07

Super bro , Clearly understand

Ответить
James D'Costa
James D'Costa - 14.08.2023 09:46

The link to download the scripts is broken. Please fix.

Ответить
prabhjot singh
prabhjot singh - 13.08.2023 21:48

Instead of partition by the command I have to use order by in order to make the commands work.

Ответить
SPORT BRAHA
SPORT BRAHA - 12.08.2023 10:50

In the example of fetch the top 3 employee salaries it’s not appropriate to use rank function because how about if you have more then 3 employees have the same high salary , by using rank function you will fetch more than 3 instead you can use the row number function

Ответить
Yashsvee Jain
Yashsvee Jain - 10.08.2023 09:48

very simple and clear cut, thnk you

Ответить
Emily Shi
Emily Shi - 07.08.2023 06:02

This is the most straightforward version of the window function I ever learned. Super helpful! Please teach more advanced SQL language in this channel.

Ответить
Ahmad Alaaeddine
Ahmad Alaaeddine - 05.08.2023 12:36

you're a life saviour

Ответить
Tanmay Thaker
Tanmay Thaker - 03.08.2023 09:52

We can also use CTE for lag function to optimise this query. Here's how:
-- Using with clause
with prev_emp_sal as (
select e.*,
lag(salary) over (partition by dept_name order by emp_id) as prev_emp_salary
from employee as e
)
select * ,
case when salary > prev_emp_salary then 'Higher than previous salary'
WHEN salary < prev_emp_salary THEN 'Lower Than previous salary'
WHEN salary = prev_emp_salary THEN 'Same as previous employee'
end as sal_range
from prev_emp_sal;

Ответить
raotechtelugu
raotechtelugu - 02.08.2023 11:49

What SQL you are using?

Ответить
Shubham Rane
Shubham Rane - 02.08.2023 06:41

I have been struggling with understanding window function for a really really really long time. But just one video of yours has made the concepts crystal clear to me. Thanks a lot!

Ответить
IMMADI SUJITH
IMMADI SUJITH - 01.08.2023 15:49

very nice explanation sir .... but instead of using lag (salary) over( ) in case clause we can use alise_name i.e, pre_emp_salary .....that made little confusion other than that it is very clear sir tq

Ответить
Rasmita Hembram
Rasmita Hembram - 31.07.2023 23:56

Am I right to say that the problem statement - fetch top 3 employees in each department earning the max salary - can be solved using row_number() as well?

Ответить
aditya khajuria
aditya khajuria - 31.07.2023 21:45

Can we solve the rank function question using row_number? if yes, please comment the logic :)

Ответить
Leaflet
Leaflet - 29.07.2023 10:49

Awesome!

Ответить
belal mohamed
belal mohamed - 29.07.2023 03:37

Your Explanation Really Amazing , Simple And Incredible <3

Ответить
Naga k
Naga k - 25.07.2023 22:51

Super Brother nice explanation.

Ответить
vyshnavi yoj
vyshnavi yoj - 25.07.2023 13:54

Thank you!

Ответить
CR 7
CR 7 - 24.07.2023 23:59

when will you upload the video for other window function

Ответить
Ruchi Sharma
Ruchi Sharma - 24.07.2023 13:34

such nicely taught lecture , i loved it!!

Ответить
Veera C
Veera C - 17.07.2023 20:14

Awesome explanation bro!!! If possible please post Snowflake related stuff also . Thanks for your great teaching skills.

Ответить
Mullai Pugazhendi
Mullai Pugazhendi - 16.07.2023 04:14

Amazing Explanation.Thank you very much. Could you please post a video on level function in sql too?

Ответить
Nazakat Ali
Nazakat Ali - 11.07.2023 16:35

This was really very helpful to understand the window function concept, you described it precisely, now moving to next part of it, Many thanks

Ответить
Sarvesh T
Sarvesh T - 08.07.2023 22:05

Is window function and analytical functions both same ?

Ответить
Yamuna u.yamuna
Yamuna u.yamuna - 08.07.2023 16:57

Crystal clear explination thanks bro

Ответить
udayan singh
udayan singh - 06.07.2023 10:54

Awesome video. Apt knowledge. to the point.

Ответить
Sushma Rose
Sushma Rose - 01.07.2023 13:09

Thank you sir for clean explanation of these function. One doubt sir, in interview if they ask to find the top 3 employees as per the question you have solved then what is the answer expected from us. Is it rank() or dense_rank()?

Ответить
Nitish Kumar
Nitish Kumar - 26.06.2023 10:50

@techTFQ you are amazing man!

Ответить
claybyfrancisshop
claybyfrancisshop - 24.06.2023 18:38

beautiful lecture

Ответить
sskylark
sskylark - 22.06.2023 16:48

Very well explained!

Ответить
Subash
Subash - 22.06.2023 11:44

Great video, mate. You saved a lot of time. My head was spinning having listened to the AI teaching voice in my udemy sql course(365 careers). You are awesome

Ответить
Biubiubiu Last
Biubiubiu Last - 22.06.2023 08:33

thank you my Indian brother. The tutorial was very clear!

Ответить