<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Onyinyechi Ofondu</title>
    <description>The latest articles on DEV Community by Onyinyechi Ofondu (@onyii).</description>
    <link>https://dev.to/onyii</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F726908%2F5af842b8-a9f8-43ed-a03b-d9ff90439516.JPG</url>
      <title>DEV Community: Onyinyechi Ofondu</title>
      <link>https://dev.to/onyii</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/onyii"/>
    <language>en</language>
    <item>
      <title>Aggregate Functions in SQL</title>
      <dc:creator>Onyinyechi Ofondu</dc:creator>
      <pubDate>Sat, 27 Nov 2021 17:46:27 +0000</pubDate>
      <link>https://dev.to/onyii/aggregate-functions-in-sql-164d</link>
      <guid>https://dev.to/onyii/aggregate-functions-in-sql-164d</guid>
      <description>&lt;p&gt;Aggregate functions are mathematical computations that return a single value from a range of values which expresses the significance of the aggregated data. They are used to derive descriptive statistics and provide key numbers in different sectors like the health, economic, and business sectors.&lt;br&gt;
The diagram below shows the typical operation on an aggregate function on a specific column and what the result looks like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xb8ezjif1z2r5zvll2x.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xb8ezjif1z2r5zvll2x.PNG" alt="Image showing  the application of aggregate functions visually"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the diagram above we can see the dataset with two columns (column 1 &amp;amp; column 2). Using the SUM function on column 2 we can see that it adds up all the values in that column and returns a single value in the "result" column.&lt;br&gt;
For this article, we shall be using PostgreSQL and Pgadmin4 as the GUI which is one of the best Graphical user interface platforms for PostgreSQL and is very beginner-friendly. You can download it &lt;a href="https://www.pgadmin.org/download/" rel="noopener noreferrer"&gt;here&lt;/a&gt; for your PostgreSQL needs.&lt;/p&gt;

&lt;p&gt;Getting back into it, the different aggregate functions are:&lt;br&gt;
&lt;strong&gt;SUM&lt;/strong&gt;: adds up all the values of a specified column.&lt;br&gt;
&lt;strong&gt;MIN&lt;/strong&gt;: the minimum value of a specified column.&lt;br&gt;
&lt;strong&gt;MAX&lt;/strong&gt;: the maximum value of a specified column.&lt;br&gt;
&lt;strong&gt;AVG&lt;/strong&gt;: the average number of values of a specified column.&lt;br&gt;
&lt;strong&gt;COUNT&lt;/strong&gt;: the number of values (rows) of a specified column/table.&lt;/p&gt;

&lt;p&gt;Aggregate functions can only be used in the SELECT  and HAVING clause where:&lt;br&gt;
The &lt;strong&gt;SELECT&lt;/strong&gt; clause lists or specifies the column that will be returned for the SQL query and &lt;br&gt;
The &lt;strong&gt;HAVING&lt;/strong&gt; clause specifies a search condition for a group or an aggregate.&lt;/p&gt;

&lt;p&gt;I created a dataset of movie downloads for this article which contains certain movie names, genres, and the number of downloads. This can be created using the SQL statement below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvs0t5slqrv6egswekhbt.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvs0t5slqrv6egswekhbt.PNG" alt="article_movies: a dataset created for this article"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the dataset above, we will look at the applications of the different aggregate functions.&lt;br&gt;
🔥Let's go!!&lt;/p&gt;
&lt;h3&gt;
  
  
  COUNT()
&lt;/h3&gt;

&lt;p&gt;The COUNT function is the most straightforward function and the best to start with:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The "COUNT(*)" in &lt;em&gt;line 1&lt;/em&gt; above is used to count all the rows in the dataset. This gives the result in the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faetm1zn0ueq1utbu7srr.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faetm1zn0ueq1utbu7srr.PNG" alt="count_aggregate_function_1_output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, when the COUNT function is used on a column, it counts only the values in that column that are, &lt;strong&gt;not NULL&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;looking at the &lt;em&gt;count_aggregate_function_2&lt;/em&gt; snippet of code above and the &lt;em&gt;count_aggregate_function_1&lt;/em&gt; snippet before that, we can see that the only difference is the "genre" column which is in the COUNT function and not "*" which denotes all the columns in the table.&lt;br&gt;
The result as seen in the image below is not the same as the COUNT  for the entire table because that column contains two NULL values:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcb657lq07v60yvwipr86.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcb657lq07v60yvwipr86.PNG" alt="count_aggregate_function_2_output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Apart from the COUNT function, &lt;strong&gt;all the other aggregate functions are only used on one column at a time&lt;/strong&gt;. Following this, let's look at the other functions!!&lt;/p&gt;
&lt;h3&gt;
  
  
  SUM()
&lt;/h3&gt;

&lt;p&gt;The SUM function was used to illustrate aggregate functions visually in the image at the start of this article, so it's pretty clear that it adds up the values of a column. Unlike the COUNT function, the SUM function can only be used on columns with a numeric data type:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ntkqrm4z9fguw6mvphh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ntkqrm4z9fguw6mvphh.PNG" alt="sum_aggegate_function_output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From &lt;em&gt;line 1&lt;/em&gt; in the code snippet above, we can see that the SUM function is applied to the downloads column (a numeric data type column).&lt;/p&gt;

&lt;h3&gt;
  
  
  AVG()
&lt;/h3&gt;

&lt;p&gt;The AVG function gets the mean of all values of a specified column. The mean of a set of numbers is the sum of all the numbers in that set divided by the number of values (count) in the set.&lt;br&gt;
Same as the SUM function, the AVG function can only be used on  numeric columns:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy3kdird4vwms8vp03u0y.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy3kdird4vwms8vp03u0y.PNG" alt="avg_aggegate_function_output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  MIN() &amp;amp; MAX()
&lt;/h3&gt;

&lt;p&gt;The MIN and MAX functions are opposites of the same coin in that the MIN function gets the lowest value of a specified column and the MAX function gets the highest value of a specified column. Unlike the other two functions above, the MIN and MAX functions can be used on columns with numerical, date-time, and even character/string data types as seen below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5pkfigk7qdxiukuxbi37.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5pkfigk7qdxiukuxbi37.PNG" alt="min_and_max_aggregate_functions_output"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Let's take a look at some helpful clauses - AS, GROUP BY and ORDER BY clause.&lt;br&gt;
Take a look at this code below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdj1fpnp0lna5q4mqwm3o.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdj1fpnp0lna5q4mqwm3o.PNG" alt="count_aggregate_function_3_output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result for the snippet of code above is confusing without the code.&lt;/p&gt;

&lt;p&gt;Now look at this one:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv1x4jqa2s2fz6bcoh7mp.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv1x4jqa2s2fz6bcoh7mp.PNG" alt="as_command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This one is better, isn't it? 😉&lt;br&gt;
The &lt;strong&gt;AS&lt;/strong&gt; command is used to rename a column or table with an alias (which only exists for the duration of the query). &lt;br&gt;
The result for the snippet is easier to understand with the AS command added in. This can be used for all sorts of queries to make your output easier to understand.&lt;/p&gt;

&lt;p&gt;There are cases when aggregate functions &lt;em&gt;does not return a single value per column&lt;/em&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuzk78ljeh8fi7dqmpthi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuzk78ljeh8fi7dqmpthi.PNG" alt="group_by_clause"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;line 3&lt;/em&gt; above, the &lt;strong&gt;GROUP BY&lt;/strong&gt; clause is introduced. It groups the SUM of the downloads according to the different genres. &lt;br&gt;
The GROUP BY clause groups rows with the same values into summary rows. It is used on categorical columns.&lt;br&gt;
Now let's take a look at the URDER BY clause:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fluip1ixexnvbkzwhj5d6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fluip1ixexnvbkzwhj5d6.PNG" alt="order_by_clause"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;ORDER BY&lt;/strong&gt; clause is introduced in &lt;em&gt;line 4&lt;/em&gt; above. It is used to order the output of a column(s) in a table in either ascending (ASC) or descending (DESC) order.&lt;/p&gt;

&lt;h3&gt;
  
  
  HAVING()
&lt;/h3&gt;

&lt;p&gt;The HAVING clause is used as a conditional statement for aggregate functions or/and arithmetic. It is used with the GROUP BY clause to filter groups or aggregates based on a specific condition(s).&lt;br&gt;
It is very similar to the WHERE clause to filter/restrict the results of a query. However, unlike the WHERE clause, it can only be used with the SELECT statement and must be used with the GROUP BY clause.&lt;/p&gt;

&lt;p&gt;In this case, we will see how aggregate functions are used to filter a table using the HAVING clause:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmekjrpai154x41d30cyi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmekjrpai154x41d30cyi.PNG" alt="having_clause"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the snippet above, the GROUP BY clause returns the rows grouped according to the "genre" column and the &lt;strong&gt;HAVING&lt;/strong&gt; clause specifies the condition to filter the groups.&lt;/p&gt;

&lt;p&gt;Now let's dive into using aggregate functions as window functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Aggregate Functions in Window functions
&lt;/h2&gt;

&lt;p&gt;Window functions are functions that perform operations across a set of rows that are related to the row the function is currently operating on. There are different window functions and they are used to simplify complex operations.&lt;br&gt;
To understand the different window functions and how they are used in SQL, check out &lt;a href="https://dev.to/onyii/window-functions-in-sql-part-1-lin"&gt;Window Functions in SQL&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we shall look at window functions and aggregate functions. &lt;br&gt;
All the aggregate functions can be used as window functions and they each give awesome and unique results depending on what you are looking for.&lt;br&gt;
Let's look at the &lt;strong&gt;SUM()&lt;/strong&gt; as a window function that gives running totals:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7yumpzzrwb5tbn4ky8eq.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7yumpzzrwb5tbn4ky8eq.PNG" alt="sum_aggregate_window_function_output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The aggregate window function was used to get the running totals for the number of downloads per genre.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;em&gt;Line 1&lt;/em&gt;, all the columns were selected because aggregate window functions do not return a single value as a result. They behave completely like window functions whilst retaining their computational qualities.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Line 2&lt;/em&gt;, is where the aggregate window function &lt;strong&gt;SUM() OVER()&lt;/strong&gt; is introduced as a brand new column named &lt;em&gt;"genre_running_total"&lt;/em&gt;. 
This new column is a running total on all the downloads that are split into partitions by their genres and ordered by both the name of the movies and their genres.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using Aggregate functions either on their own (SUM(), COUNT(), e.t.c) or as a filter (with the HAVING clause) or as a window function (SUM() OVER()) gives different results. &lt;br&gt;
They are very useful and make SQL coding and data presentation as well as analysis a lot easier.&lt;/p&gt;

&lt;p&gt;I hope this has answered some of your questions and given you some new ideas!!&lt;br&gt;
I'll be Back 😎&lt;br&gt;
Bye for now.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>sql</category>
      <category>datascience</category>
      <category>database</category>
    </item>
    <item>
      <title>Window Functions in SQL: Part 1</title>
      <dc:creator>Onyinyechi Ofondu</dc:creator>
      <pubDate>Sun, 14 Nov 2021 16:56:40 +0000</pubDate>
      <link>https://dev.to/onyii/window-functions-in-sql-part-1-lin</link>
      <guid>https://dev.to/onyii/window-functions-in-sql-part-1-lin</guid>
      <description>&lt;p&gt;In SQL, Window functions are functions that performs operations across a set of rows that are related to the row the function is currently operating on. &lt;br&gt;
Window functions were first introduced in SQL in 2003 with functionality expanded in 2012 and are needed in SQL because they simplify certain complex operations and analysis and can be used to calculate running totals, moving averages, and growth over time amongst others. &lt;br&gt;
The dataset below with a set of rows and columns has a window function operating on a particular column and the result extracted spans an entire new column as shown below.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8e7bkbtbwn6jejzdmn5.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8e7bkbtbwn6jejzdmn5.PNG" alt="Diagram describing the LAG function. Showing that window functions spans a whole new column."&gt;&lt;/a&gt;&lt;br&gt;
From the diagram above, we can see a sample dataset with 2 columns (column_1 &amp;amp; column_2). Using the LAG function (which shall be explained later), we can see the result that is produced which spans an entire new column(lag). To get this, all that was needed using window functions was two lines of code as seen below&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
However, to perform this same operation without window functions in SQL we would need multiple self joins and subqueries.&lt;br&gt;
For this article, we shall be using PostgreSQL and the Pgadmin4 as the GUI which is one of the best Graphical user interface Platforms for PostgreSQL and is very beginner friendly. You can download it &lt;a href="https://www.pgadmin.org/download/" rel="noopener noreferrer"&gt;here&lt;/a&gt; for you PostgreSQL needs.

&lt;p&gt;To start with, we shall look at the basic window functions, which include:&lt;br&gt;
&lt;strong&gt;The Ranking functions&lt;/strong&gt;:- Row number, Rank &amp;amp; Dense rank&lt;br&gt;
&lt;strong&gt;The Fetching functions&lt;/strong&gt;:- Lag, Lead, First_Value &amp;amp; Last_Value. &lt;/p&gt;

&lt;p&gt;I created a dataset of Movie downloads which contains certain movie names, genre and number of downloads. This can be created using this SQL statement below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmdi92uumo9omlvribkpx.PNG" alt="fic_movies dataset"&gt;&lt;br&gt;
Using the dataset above, we will look at the basic applications of window functions
&lt;h3&gt;
  
  
  Ranking Functions. 🥇🥈🥉
&lt;/h3&gt;

&lt;p&gt;Ranking functions are functions that assign numbers to rows in sequential order. To rank a column in a dataset, things like highest and lowest can be easily seen with a glance and it can be used as a reference (index) for other operations in SQL.&lt;br&gt;
The different ranking functions have the same result with very few differences:- &lt;br&gt;
&lt;strong&gt;ROW_NUMBER&lt;/strong&gt; ranks the different rows starting from number 1. It is used mainly as an index for a dataset and can be used for easier reference to each row.&lt;br&gt;
&lt;strong&gt;RANK&lt;/strong&gt; also does the same as ROW_NUMBER above but assigns the same number(s) to identical values and skips the next value(s) for the number of times the number was repeated.&lt;br&gt;
&lt;strong&gt;DENSE_RANK&lt;/strong&gt; also assigns the same number(s) to identical values but doesn’t skip the next value(s) at all.&lt;br&gt;
The movies dataset can be ranked as is with the 1st movie recorded as the 1st rank and using the ORDER BY function, it can be ranked from the lowest to the highest number of downloads (as shown below) or vice versa:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;strong&gt;OVER&lt;/strong&gt; clause in lines 1,2 &amp;amp; 3 is a staple in all window functions and determines exactly how the rows of the query are split up for processing by the window function. &lt;br&gt;
The &lt;strong&gt;ORDER BY&lt;/strong&gt; clause in lines 2 &amp;amp; 3 is used inside the window functions' OVER clause specifying that the ORDER should be determined before the function is executed.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxzp80n9twmnp9s3kham.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhxzp80n9twmnp9s3kham.PNG" alt="Output showing the ranked downloads"&gt;&lt;/a&gt;&lt;br&gt;
Looking at the output above, we can see the differences between the ROW_NUMBER(), the RANK() and the DENSE_RANK(). &lt;/p&gt;

&lt;p&gt;Let's take a look at the &lt;strong&gt;PARTITION BY&lt;/strong&gt; clause below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;strong&gt;PARTITION BY&lt;/strong&gt;, a new clause introduced in this snippet above is used to divide the dataset into different partitions (tables/sections). When this happens, any window function executed in the dataset sees each partition as a table. We can see that inside the OVER clause, we have PARTITION BY genre and the ORDER BY clause. In SQL, the code on the inside is executed 1st which means that the table will be partitioned  and ordered accordingly before the window function is applied. &lt;br&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ma5sid4ztffcfjak8ps.PNG" alt="output showing the ranked downloads with partitions."&gt;

&lt;h3&gt;
  
  
  🥏🐕 Fetching Functions
&lt;/h3&gt;

&lt;p&gt;The Fetching functions work a bit differently from the ranking functions:&lt;br&gt;
&lt;strong&gt;LAG&lt;/strong&gt; returns the value at n rows before the current row. &lt;br&gt;
&lt;strong&gt;LEAD&lt;/strong&gt; returns the value at n rows after the current row. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
In line 1, we can see &lt;em&gt;&lt;strong&gt;LAG(downloads, 1)&lt;/strong&gt;&lt;/em&gt;: this tells the LAG function to lag the downloads column by 1 by returning the values of the downloads column but skipping the 1st row (pushes the values of the column down one row 😊). &lt;br&gt;
The LEAD function does the same but instead, it starts at the bottom (pushes up) and since its &lt;em&gt;&lt;strong&gt;LEAD(downloads, 2)&lt;/strong&gt;&lt;/em&gt; it skips 2 rows as seen below.

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7cux3dy6sb14ozia0xf.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7cux3dy6sb14ozia0xf.PNG" alt="LEAD &amp;amp; LAG functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last two window functions in this article are pretty straight forward.&lt;br&gt;
The &lt;strong&gt;FIRST_VALUE&lt;/strong&gt; returns the value of the first row in a table or partition and &lt;strong&gt;LAST_VALUE&lt;/strong&gt; returns the value of the last row in a table or partition. &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Nothing new in this snippet above. The FIRST_VALUE clause is pretty easy to code but take a look at this one below:&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
The LAST_VALUE clause is followed by &lt;strong&gt;RANGE BETWEEN&lt;/strong&gt;. Normally, window functions read from the beginning of the table/partition to the current row the window function is operating on. So it doesn't extend to the end of the table but rather stops at the specified row, but the LAST_VALUE clause starts at the bottom of the table so the &lt;strong&gt;RANGE BETWEEN&lt;/strong&gt; is used to extend the window function to the end of the table.&lt;br&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftf5bu5qt9i82x2db2xxo.PNG" alt="output showing the first_value and the last_value"&gt;&lt;br&gt;
Window functions make SQL life much easier and can be used in different ways. Now go grab yourself a dataset and get to work 🔥🔥

</description>
      <category>sql</category>
      <category>postgres</category>
      <category>datascience</category>
      <category>database</category>
    </item>
  </channel>
</rss>
