SQL Tutorial - Window Functions

preview_player
Показать описание
Another fantastic SQL Tutorial brought to you by BeardedDev.

In this video we begin to explore Window Functions and their purpose within SQL Server.

Window Functions are used for performing data analysis calculations and address an important need compared to the GROUP BY clause that we are able to return the underlying data in the same query.

This video shows an example of the differences between the GROUP BY clause and Window Functions.

Window Functions were first introduced in SQL Server 2005 but further enhancements and support was added in SQL Server 2012.

We look at the OVER clause and PARTITION BY.

Window Functions can only be included within SELECT or ORDER BY clauses.

Functions Available:
Aggregate - COUNT, SUM, MIN, MAX, AVG
Offset - FIRST_VALUE, LAST_VALUE, LEAD, LAG
Statistical - PERCENT_RANK, CUME_DIST, PERCENTILE_CONT, PERCENTILE_DIST

Windows Functions also have FRAMES
ROWS
RANGE

Window Functions are a powerful tool within SQL Server and I am excited to bring more videos and tutorials working with Window Functions in the future.

Code:

WITH CTE
AS
(
SELECT
Sales_Id
, SUM(Line_Total) AS Total
FROM Sales_Details
GROUP BY Sales_Id
)

SELECT * FROM CTE AS A
INNER JOIN Sales_Details AS B
ON A.Sales_Id = B.Sales_Id

SELECT
Sales_Id
, Sales_Date
, Item
, Price
, Quantity
, Line_Total
, COUNT(Line_Total) OVER(PARTITION BY Sales_Id) AS Line_Count
, SUM(Line_Total) OVER(PARTITION BY Sales_Id) AS Sales_Total
, SUM(Line_Total) OVER(PARTITION BY Sales_Date) AS Daily_Total
, SUM(Line_Total) OVER() AS Total
FROM Sales_Details
ORDER BY Sales_Total
Рекомендации по теме
Комментарии
Автор

I loved this video, looking forward to see more videos from you

sabaamanollahi
Автор

I've seen so many videos trying to explain windows functions and none of them Explain it as you do. I think the difference is you showed the difference between using group by, CTEs and windows function. Thanks a lot, Dev.

fa
Автор

I came to YouTube after going through a paid online SQL course wanting to understand window functions better. You explained/showed it to a level where conceptually it sunk in. Thanks.

jdjohnson
Автор

I have watched this video, and others (in fact all of them), of yours and really enjoy your approach and pace. For what it's worth, I have watched many other SQL Tutorial channels, however, I find myself drawn back to your videos time and again.

OldPick-Unix-dude-pbjg
Автор

In the last bullet of your "Window Functions" slide, you want to use the preposition "into" rather than the phrase "in to". The result set is being split <i>into</i> partitions, denoting that the abstract positioning of the data is changing.

haedruspa
Автор

Wow! I've literally been searching for an easy-to-understand explanation of what Window Functions are, and this one is the best explanation so far. Thank you so much!

zohraserhenova
Автор

A short time ago I was contacted by an employer for an SQL Developer job (Entry Level), and they wanted me to take a skills assessment. The manager told me that I should know CTE's and Window Functions. I was pretty comfortable with CTE's but I basically learned what Window Functions were (and how they are used) from your videos, and also found your CTE videos a useful refresher. I watched the series over a few times until I was comfortable playing around on my machine with them, and, turns out, like 90% of the assessment was a practical coding assessment for Window Functions/CTE's (moderately more complex than the examples in your videos). I ended up being hired for the position.

I just wanted to say that I appreciated your videos and they were apparently clear enough to both introduce Window Functions and help me understand it enough to apply it appropriately (in tandem with playing around a bit with a practice database i.e. AdventureWorks). So thanks!

cheezemansam
Автор

One of the best explaination of Window functions to get started on !!

sumitbhatnagar
Автор

The best description of windows functions that I have seen. Thank you!

BillSmith-iwwm
Автор

Thank you so much, i was never able to understand these until now !

quentinsmits
Автор

You are by far the best SQL tutor. I dont get it why you dont have many subscribers and views. Great videos.

hercai
Автор

Awesome introduction to Window functions. Clear as Icelandic water on a sunny day. good job

FatDada
Автор

Explain window functions with examples (sum, count) very clearly. Thank you.

zhiqiaojiang
Автор

Short, convice and to the point. Very good video!

kesu
Автор

Thanks a lot the best explanation for OVER() and PARTITION BY I ever seen :)

goldenmomentsHD
Автор

Another great tutorial from BeardedDev!!

markwade
Автор

Awesome video, thanks! The explanation is better than some paid online courses provide. It would be great if you also shared the table you use for querying to be able to practice and compare the results.

mikhailsladkomedov
Автор

Great video. one of those topics as a software dev I never really cared to look into but glad you made it so simple to learn!

aaronhammer
Автор

great explanation of window functions, thanks a lot

ALEXZILBER
Автор

Very good demo and explanation for windows functions.

codyscorner