doingwithdata Profile picture
👉Here to help you enter the field of data. 🧑‍🏫 Learn Excel, Python, SQL, Power BI

Oct 21, 2022, 9 tweets

SQL Windows Functions Explained like you are 5!

SQL windows are essential for a data analyst. You must learn them. Learn here in 2 min.

A thread..⬇️

So what are Windows Functions: — Summarize a particular table over a particular column(thus creating a window) and calculate the result using different functions(mentioned above)

Functions are divided into two types: the ranking and value functions. The ranking functions are used to simply assign numbers to the existing rows according to some requirements, and value functions are used to copy values from other rows to other rows within the defined windows

USE CASE: Suppose you have financial data of companies and you have recently started investing You have the basic idea that you will only invest in companies that are in the Top 3 of their business category.

That’s where the importance of windows functions comes in. You will use the windows rank function(See the first image) and fetch on top 3 companies by the market cap of their business. (See the code below).

Ranking Window Functions :
Ranking functions are, RANK(), DENSE_RANK(), ROW_NUMBER().
The below image tells you the difference between these.

Value Window Functions :
Value functions are, Lead() , Lag().

LAG() — The LAG() function allows access to a value stored in a different row above the current row. The row above may be adjacent or some number of rows above, as sorted by a specified column or set of columns.

LEAD() — Lead() is similar to Lag(). Whereas Lag() accesses a value stored in a row above, Lead() accesses a value stored in a row below.

That’s It. I hope it helps you in your journey of becoming a #dataanalyst. I have used and used most of these websites not just for practice but for revision of multiple aspects of the data analytics journey.

Do follow @doingwithdata for more updates on #dataanalytics.

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling