Top 5 SQL Analytic Functions Every Data Analyst Needs to Know

Top 5 SQL Analytic Functions Every Data Analyst Needs to Know

SQL analytical functions to take your analysis skills to the next level. Analytical functions are one of the most popular tools among BI/Data analysts for performing complex data analysis.

Analytical functions are one of the most popular tools among BI/Data analysts for performing complex data analysis. These functions perform computations over multiple rows and return the multiple rows as well. Today we’ll cover 5 functions I find most useful, with a lot of practical examples.

For the uninitiated, the relative size of a query with an analytical function might seem a bit intimidating. Don’t worry, we have you covered. Most of these functions follow a basic syntax:

analytic_function_name([argument_list])
OVER (
[PARTITION BY partition_expression,…]
[ORDER BY sort_expression, … [ASC|DESC]])

There are three parts to this syntax, namely functionpartition by and order by. Let’s briefly cover what each one does:

  • analytic_function_name: name of the function — like RANK()SUM()FIRST(), etc
  • partition_expression: column/expression on the basis of which the partition or window frames have to be created
  • sort_expression: column/expression on the basis of which the rows in the partition will be sorted

Okay, we’ve covered the basics thus far. For the practical part we’re gonna use the Orders table stored inside the PostgreSQL database:

Image for post

Let’s begin with the practical part now, shall we?


AVG() and SUM()

We’ve all been using aggregate functions such as SUMAVGMINMAX, and COUNT in our GROUP BY clauses. But when these functions are used over an ORDER BY clause they can give us running sum, mean, total, etc.

The following example will make it a lot more clear — we want to calculate the running average revenue and total revenue for each agent in the third quarter:

SELECT ord_date, agent_code, AVG(ord_amount) OVER (

PARTITION BY agent_code

ORDER BY ord_date

) running_agent_avg_revenue,

SUM (ord_amount) OVER (

PARTITION BY agent_code

ORDER BY ord_date

) running_agent_total_revenue

FROM orders

WHERE ord_date BETWEEN ‘2008–07–01’ AND ‘2008–09–30’;

machine-learning programming towards-data-science data-science sql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

15 Machine Learning and Data Science Project Ideas with Datasets

Learning is a new fun in the field of Machine Learning and Data Science. In this article, we’ll be discussing 15 machine learning and data science projects.

Learn Programming, Software Engineering, Machine Learning, And More

Best Free Resources to Learn Programming, Software Engineering, Machine Learning, And More All you need to learn. Do you know that you can take the courses from MIT, Stanford.

Most popular Data Science and Machine Learning courses — July 2020

Most popular Data Science and Machine Learning courses — August 2020. This list was last updated in August 2020 — and will be updated regularly so as to keep it relevant

Pipelines in Machine Learning | Data Science | Machine Learning | Python

Machine Learning Pipelines performs a complete workflow with an ordered sequence of the process involved in a Machine Learning task. The Pipelines can also