Important SQL functions

This article will introduce important functions in SQL rank, denserank, over, partition.

There are some important SQL functions we can study, like

  • RANK()
  • DENSERANK()
  • OVER()
  • PARTITION()

RANK() and DENSE_RANK() are both effort-saving and effective functions to learn in SQL. The RANK() the function simply works like giving rank 1 to the student who earns the maximum marks in the exam, giving rank 2 to the student who scores the second maximum marks in the exam, and so on.

Let’s understand the details with the following examples: Suppose the current position of football-playing countries in the FIFA ranking system is as given in the table.

TeamTotal PointsPrevious PointsDelta (+/-)
Brazil18271823.429.27
Belgium18271828.45-1.45
France17891786.153.7
Argentina17651766.99-1.86
England17611755.526.19

The second column in this table displays the total points earned by each country in the FIFA ranking system. Now we want to apply functions RANK() and DENSE_RANK() on the basis of total points.

We can see that Brazil and Belgium have the same number of total points.

Simple Implementation

SQL
SQL
SQL
SELECT *,
RANK () OVER( ORDER BY TOTAL POINTS asc) as RANK
FROM ‘FIFA_table’
ORDER BY RANK;
Rank
TeamTotal PointsPrevious PointsDelta(+/-)Rank
Brazil 1827 1823.429.25 1
Belgium 1827 1828.45-1.45 1
France17891786.153.7 3
Argentina17651786.99-1.864
England17611755.526.195

Countries that have equal total points are given the same rank, and for countries that have fewer adjacent points, the rank will be given after considering the positions of previous rank holders.

Applying partition

We can also apply RANK() category- or division-wise. This can be easily understood from the following example, where we will be giving a ranking to the dataset according to each division or region in which football-playing countries are categorized, viz; UEFA, CONMEBOL.

Suppose we have this table.

TeamTotal PointsPrevious PointsDelta(+/-)Division
Brazil18271823.429.27CONMEBOL
Belgium18271828.45-1.45UEFA
France17891786.153.7UEFA
Argentina17651766.99-1.86CONMEBOL
England17611755.526.19UEFA

1. RANK()

SQL
SQL
SQL
SELECT *,
RANK () OVER( PARTITION by Division ORDER by Total Points asc) as RANK
FROM ‘FIFA_table’
ORDER by RANK;

After applying the code for RANK() with PARTITION() of any category.

TeamTotal PointsPrevious PointsDelta(+/-)DivisionRank
Brazil18271823.429.27CONMEBOL1
Argentina17651766.99-1.86CONMEBOL 2
Belgium18271828.45-1.45UEFA1
France17891786.153.7UEFA2
England17611755.526.19UEFA3

2. DENSE_RANK()

DENSE_RANK() will rank data, but it does not take into account the position of other rank holders. This can be seen in the following example.

SQL
SQL
SQL
SELECT *,
DENSE_RANK () OVER( ORDER BY TOTAL POINTS asc) as RANK
FROM ‘FIFA_table’
ORDER BY RANK;
TeamTotal PointsPrevious PointsDelta(+/-)DivisionRank
Brazil 1827 1823.429.27CONMEBOL 1
Belgium 1827 1826.45-1.45UEFA 1
France17891786.153.7UEFA 2
Argentina17651766.99-1.86CONMEBOL3
England17611755.526.19UEFA4

Countries that have equal total points are given the same rank, the same rank, for rest of countries ranks will be given according to total points. The adjacent ranks will be given to countries without considering the row positions of previous rank holders.

In simple words, Brazil and Belgium have equal total points, which is the highest number of total points in the table, so they are given rank 1. France has the second-highest total of points in the table, so it is given rank 2, despite being in row position 3

This is contrary to RANK() and that’s why it explains the difference between RANK() and DENSE_RANK().

CREATE TABLE mytable( Id INTEGER NOT NULL PRIMARY KEY ,SepalLengthCm NUMERIC(3,1) NOT NULL ,SepalWidthCm NUMERIC(3,1) NOT NULL ,PetalLengthCm NUMERIC(3,1) NOT NULL ,PetalWidthCm NUMERIC(3,1) NOT NULL ,Species VARCHAR(15) NOT NULL ); INSERT INTO mytable(Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species) VALUES (1,5.1,3.5,1.4,0.2,”Iris-setosa”); INSERT INTO mytable(Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species) VALUES (2,4.9,3.0,1.4,0.2,”Iris-setosa”); INSERT INTO mytable(Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species) VALUES (3,4.7,3.2,1.3,0.2,”Iris-setosa”); INSERT INTO mytable(Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species) VALUES (4,4.6,3.1,1.5,0.2,”Iris-setosa”); INSERT INTO mytable(Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species) VALUES (5,5.0,3.6,1.4,0.2,”Iris-setosa”); INSERT INTO mytable(Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species) VALUES (6,5.4,3.9,1.7,0.4,”Iris-setosa”); INSERT INTO mytable(Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species) VALUES (7,4.6,3.4,1.4,0.3,”Iris-setosa”); INSERT INTO mytable(Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species) VALUES (8,5.0,3.4,1.5,0.2,”Iris-setosa”); INSERT INTO mytable(Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species) VALUES (9,4.4,2.9,1.4,0.2,”Iris-setosa”); select * from mytable;

How useful was this post?

Click on a star to rate it!

  • ANCOVA: Analysis of Covariance with python

    ANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.

  • Learn Python The Fun Way

    What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!

  • Meet the most efficient and intelligent AI assistant : NotebookLM

    Start using NotebookLM today and embark on a smarter, more efficient learning journey!

  • Break the ice

    This can be a super guide for you to start and excel in your data science career.

  • Manova Quiz

    Solve this quiz for testing Manova Basics

  • Quiz on Group By

    Test your knowledge on pandas groupby with this quiz

  • Visualization Quiz

    Observe the dataset and try to solve the Visualization quiz on it

  • Versions of ANCOVA (Analysis Of Covariance) with python

    To perform ANCOVA (Analysis of Covariance) with a dataset that includes multiple types of variables, you’ll need to ensure your dependent variable is continuous, and you can include categorical variables as factors. Below is an example using the statsmodels library in Python: Mock Dataset Let’s create a dataset with a mix of variable types: Performing…

  • Python Variables

    How useful was this post? Click on a star to rate it! Submit Rating

  • A/B Testing Quiz

    Complete the code by dragging and dropping the correct functions

  • Python Functions

    Python functions are a vital concept in programming which enables you to group and define a collection of instructions. This makes your code more organized, modular, and easier to understand and maintain. Defining a Function: In Python, you can define a function via the def keyword, followed by the function name, any parameters wrapped in parentheses,…

  • Python Indexing: A Guide for Data Science Beginners

    Mastering indexing will significantly boost your data manipulation and analysis skills, a crucial step in your data science journey.

  • Diffusion Models: Making AI Creativity

    Stable Diffusion Models: Where Art and AI Collide Artificial Intelligence meets creativity in the fascinating realm of Stable Diffusion Models. These innovative models take text descriptions and bring them to life in the form of detailed and realistic images. Let’s embark on a journey to understand the magic behind Stable Diffusion in a way that’s…

Instagram
WhatsApp
error: Content is protected !!