Tutorials

Essential SQL window functions for business operations | Census

Allie Beazell
Allie Beazell June 22, 2021

Allie Beazell is director of developer marketing @ Census. She loves getting to connect with data practitioners about their day-to-day work, helping technical folks communicate their expertise through writing, and bringing people together to learn from each other. Los Angeles Metropolitan Area, California, United States

To run a successful business today, having good analytics is not only beneficial but almost instrumental. Part of having strong business analytics is having good SQL fluency.

SQL is arguably the most important skill to learn across any type of data-related profession, whether you’re a business operations associate, a product analyst, a data scientist, etc.

This article will look at a particular SQL concept called window functions (aka analytics functions), which is essential for unlocking advanced analytics and deeper insights.

Window functions: What are they and how do they differ from aggregate functions?

A window function is like an aggregate function in the sense that it returns aggregate values (eg. SUM(), COUNT(), MAX()).

What makes window functions different is that it does not group the result set. The number of rows in the output is the same as the number of rows in the input.

The basic anatomy of a window function is as follows:


SELECT SUM() OVER(PARTITION BY ___ ORDER BY ___) FROM Table

To make it easier to understand, there are three main parts to remember:

  1. The aggregate function: In the example above, I used SUM() but you can also use COUNT(), AVG(), etc.
  2. PARTITION BY: Simply think of this as a GROUP BY clause, but instead, it’s called PARTITION BY.
  3. ORDER BY: ORDER BY is the same as you would expect. This is important to consider when the order of your output matters.

In order to get a better understanding of windows functions, and how they can be used in business contexts, we’re going to look at five essential windows functions for business operations.

5 essential window functions

Window functions are a powerful tool in your data kit, and there's a lot of heavy lifting you can do with them. But for today, let's focus on the five essential window functions that'll make your life easier right now:

  1. ROW_NUMBER()
  2. SUM()
  3. AVG()
  4. LEAD()/LAG()
  5. DENSE_RANK()

1. Get first or last instance via ROW_NUMBER()

ROW_NUMBER() returns the number of each row, which starts at 1 for the first record and increases by 1 for each row following it. ROW_NUMBER() is extremely useful when you want to get the first or last record of a specific table.

When it's useful: If you have a table of customer purchases and you want to get the date of the first purchase for each customer, you can PARTITION BY customer (name/id) and ORDER BY purchase date. Then you can filter the table WHERE row number = 1.

The following query is an example of how you can use ROW_NUMBER() to get the first purchase date for each customer id.


with numbered_transactions as (
    SELECT
        customerId
      , purchaseDate
      , ROW_NUMBER() OVER (PARTITION BY customerId ORDER BY
        purchaseDate) as rowNumber
    FROM
        transactions
)
SELECT
    *
FROM
    numbered_transactions
WHERE 
    rowNumber = 1

2. Cumulative sum via SUM()

SUM() is an aggregate function that SUMs the values in a column. By using SUM() or COUNT() in a window function, you can calculate cumulative sums or cumulative counts.

When it's useful: If you want to create graphs that show the growth of a specific metric (i.e. number of orders, revenue, etc…) over time.

The following example shows how you can include a cumulative sum column of monthly revenue:


SELECT
    monthYear
  , monthlyRevenue
  , SUM(monthlyRevenue) OVER (ORDER BY monthYear) as cumRevenue
FROM
    sales

3. Moving averages via AVG()

AVG() function is an aggregate function that returns the average value of a column. AVG() is really powerful in windows functions as it allows you to compute moving averages over time.

When it's useful: If you want to calculate the average spend of your customers, moving averages for sales, or average use statistics for different features within your product. The following query is an example of calculating the 5 day moving average of daily sales.


SELECT
    Date
  , dailySales
  , AVG(dailySales) OVER (ORDER BY Date ROWS 5 PRECEDING) AS
    5_dayMovingAverage
FROM
    sales

4. Deltas via LEAD()/LAG()

LEAD() and LAG() are useful when you want to compare the values of previous rows or later rows. LEAD() and LAG() are mostly used when comparing one period of time with the previous period of time for a given metric.

When it's useful: LEAD() and LAG() are most useful if you're trying to do things like:

  • Compare each month’s revenue with the previous month’s revenue
  • Calculate the percentage growth in user sign-ups on a monthly basis
  • Compare the change in the number of users churned on a monthly basis

The following query shows how you can query the monthly percent change in revenue, given a date column and a monthly revenue column.


with monthly_sales as (
    SELECT
        monthYear
      , monthlyRevenue
      , LEAD(monthlyRevenue) OVER (ORDER BY monthYear) as
        previousMonth
    FROM
        sales
)
SELECT
    monthYear
  , (monthlyRevenue - previousMonth) / previousMonth * 100 AS
    revenuePercentChange
FROM monthly_sales

5. Ranking via DENSE_RANK()

DENSE_RANK() is similar to ROW_NUMBER() except that it returns the same rank (number) for equal values.

When it's useful: When you want to rank your data based on a particular variable or variables.

If you wanted to rank your top customers by total sales, DENSE_RANK() would be an appropriate function to use.


SELECT
    customerId
  , totalSales
  , DENSE_RANK() OVER (ORDER BY totalSales DESC) as rank
FROM
    customers

Wrapping up your SQL window skills

By reading this article, you should not only know what a window function is, but you should also have a good grasp of its versatility and functionality. There are so many more ways that you can use window functions, but these will take you pretty far.

Hungry to learn more? Check out our collection of data tutorials to sharpen your skills further. 🚀

Related articles

Customer Stories
Built With Census Embedded: Labelbox Becomes Data Warehouse-Native
Built With Census Embedded: Labelbox Becomes Data Warehouse-Native

Every business’s best source of truth is in their cloud data warehouse. If you’re a SaaS provider, your customer’s best data is in their cloud data warehouse, too.

Best Practices
Keeping Data Private with the Composable CDP
Keeping Data Private with the Composable CDP

One of the benefits of composing your Customer Data Platform on your data warehouse is enforcing and maintaining strong controls over how, where, and to whom your data is exposed.

Product News
Sync data 100x faster on Snowflake with Census Live Syncs
Sync data 100x faster on Snowflake with Census Live Syncs

For years, working with high-quality data in real time was an elusive goal for data teams. Two hurdles blocked real-time data activation on Snowflake from becoming a reality: Lack of low-latency data flows and transformation pipelines The compute cost of running queries at high frequency in order to provide real-time insights Today, we’re solving both of those challenges by partnering with Snowflake to support our real-time Live Syncs, which can be 100 times faster and 100 times cheaper to operate than traditional Reverse ETL. You can create a Live Sync using any Snowflake table (including Dynamic Tables) as a source, and sync data to over 200 business tools within seconds. We’re proud to offer the fastest Reverse ETL platform on the planet, and the only one capable of real-time activation with Snowflake. 👉 Luke Ambrosetti discusses Live Sync architecture in-depth on Snowflake’s Medium blog here. Real-Time Composable CDP with Snowflake Developed alongside Snowflake’s product team, we’re excited to enable the fastest-ever data activation on Snowflake. Today marks a massive paradigm shift in how quickly companies can leverage their first-party data to stay ahead of their competition. In the past, businesses had to implement their real-time use cases outside their Data Cloud by building a separate fast path, through hosted custom infrastructure and event buses, or piles of if-this-then-that no-code hacks — all with painful limitations such as lack of scalability, data silos, and low adaptability. Census Live Syncs were born to tear down the latency barrier that previously prevented companies from centralizing these integrations with all of their others. Census Live Syncs and Snowflake now combine to offer real-time CDP capabilities without having to abandon the Data Cloud. This Composable CDP approach transforms the Data Cloud infrastructure that companies already have into an engine that drives business growth and revenue, delivering huge cost savings and data-driven decisions without complex engineering. Together we’re enabling marketing and business teams to interact with customers at the moment of intent, deliver the most personalized recommendations, and update AI models with the freshest insights. Doing the Math: 100x Faster and 100x Cheaper There are two primary ways to use Census Live Syncs — through Snowflake Dynamic Tables, or directly through Snowflake Streams. Near real time: Dynamic Tables have a target lag of minimum 1 minute (as of March 2024). Real time: Live Syncs can operate off a Snowflake Stream directly to achieve true real-time activation in single-digit seconds. Using a real-world example, one of our customers was looking for real-time activation to personalize in-app content immediately. They replaced their previous hourly process with Census Live Syncs, achieving an end-to-end latency of <1 minute. They observed that Live Syncs are 144 times cheaper and 150 times faster than their previous Reverse ETL process. It’s rare to offer customers multiple orders of magnitude of improvement as part of a product release, but we did the math. Continuous Syncs (traditional Reverse ETL) Census Live Syncs Improvement Cost 24 hours = 24 Snowflake credits. 24 * $2 * 30 = $1440/month ⅙ of a credit per day. ⅙ * $2 * 30 = $10/month 144x Speed Transformation hourly job + 15 minutes for ETL = 75 minutes on average 30 seconds on average 150x Cost The previous method of lowest latency Reverse ETL, called Continuous Syncs, required a Snowflake compute platform to be live 24/7 in order to continuously detect changes. This was expensive and also wasteful for datasets that don’t change often. Assuming that one Snowflake credit is on average $2, traditional Reverse ETL costs 24 credits * $2 * 30 days = $1440 per month. Using Snowflake’s Streams to detect changes offers a huge saving in credits to detect changes, just 1/6th of a single credit in equivalent cost, lowering the cost to $10 per month. Speed Real-time activation also requires ETL and transformation workflows to be low latency. In this example, our customer needed real-time activation of an event that occurs 10 times per day. First, we reduced their ETL processing time to 1 second with our HTTP Request source. On the activation side, Live Syncs activate data with subsecond latency. 1 second HTTP Live Sync + 1 minute Dynamic Table refresh + 1 second Census Snowflake Live Sync = 1 minute end-to-end latency. This process can be even faster when using Live Syncs with a Snowflake Stream. For this customer, using Census Live Syncs on Snowflake was 144x cheaper and 150x faster than their previous Reverse ETL process How Live Syncs work It’s easy to set up a real-time workflow with Snowflake as a source in three steps: