How to Perform User Cohort Analysis SQL?: 4 Easy Steps


Cohort Analysis SQL: Featured Image

Cohort Analysis is a valuable process that empowers you to extract actionable insights related to Customer Churn, Product Engagement, Product Value, and more. Irrespective of the technology or domain, Cohort Analysis offers substantial data evaluation capabilities for mobile applications, Cloud-based tools, E-Commerce, Online Gaming Platforms, Digital Marketing, and Security Services. This method is a unique and robust way to process, analyze and classify data.

This article will introduce you to Cohort Analysis and Retention. It will also explain the step-by-step process of performing Cohort Analysis SQL. Furthermore, the article will elaborate on the various applications of this methodology. Read along to learn more about Retention and Cohort Analysis!

Table of Contents

What is Cohort Analysis?

Cohort Analysis SQL: Cohort Analysis Image
Image Source

To understand the importance of Cohort Analysis, you need to first grasp what is a Cohort? A Cohort is formed by grouping together users using a common characteristic. This implies a Cohort Analysis is an analysis of multiple cohorts (i.e. customer groups) with the objective of getting a deeper understanding of user behaviors, shopping patterns, market trends, etc.

Performing Cohort Analysis SQL is easy and you can use it for improving certain business areas as it can smoothen user onboarding, enhance product development, and facilitate data-driven marketing tactics. The true strength of Cohort Analysis lies in its 3-dimensional visualizations which allow you to compare a metric across various data segments over time. This helps you to understand your customers’ behavior and supports you in planning efficient future strategies.

To learn more about Cohort Analysis, visit here.

Simplify Data Streaming Using Hevo’s No Code Data Pipeline

Hevo Data, a Fully-managed Data Pipeline platform, can help you automate, simplify & enrich your data replication process in a few clicks. With Hevo’s wide variety of connectors and blazing-fast Data Pipelines, you can extract & load data from 150+ Data Sources straight into your Data Warehouse or any Databases. To further streamline and prepare your data for analysis, you can process and enrich raw granular data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!


Hevo is the fastest, easiest, and most reliable data replication platform that will save your engineering bandwidth and time multifold. Try our 14-day full access free trial today to experience an entirely automated hassle-free Data Replication!

What is Retention?

Cohort Analysis SQL: Retention Logo
Image Source

User Retention is a key metric useful for measuring the ratio of users that come back to utilize your service or product. If customer A uses your product (or service) on Monday and again on Tuesday, then Customer A is a retained user. However, if Customer B does not utilize your product on Tuesday after using it on Monday, he is marked as a lapsed user. this way, Retention for the day of Monday is the amount of retained users divided by total users. Therefore, if customers A and B were the only users on Monday, then Monday’s retention is 50%.

Measure Retention is beneficial for your business teams as it can help them understand how your products perform and even help them to analyze user behaviors according to various locations, gender, age, etc. Moreover, calculating Retention Rate is easy and all you need to do is count the number of returning users in a time frame (every week or month), and group them according to the week they signed up.

Steps to Perform Cohort Analysis SQL

Cohort Analysis SQL is based on Retention Calculation which requires you to count the users who revert back regularly on a weekly (or monthly) basis and group them according to the week they signed up.

For instance, a student of a school requires to log in to the school’s portal every day to use study materials present online. This student will be your user for which retention calculation is needed. Now, every time user, logs into the website, details such as login timing, data, and user ID, are updated on the server. You can get this data in tabular forms and group them into cohorts for Calculating Retention Rate.

The following steps allow you to calculate Retention Rate and perform Cohort Analysis SQL on a given user data:

Step 1: Create a New Table to Store Cohort Data

Using SQL generate a new table named login consisting of 3 columns namely login_date, user_id, and id. Next, perform auto_increament on the column id using the IDENTITY keyword so that it will automatically increase by 1 for every newly inserted record. Furthermore, set id as the primary key using the following code:

Now, since the login table is ready, you need to insert values in each row using the following SQL Query:

Finally, use the following command to view the login table:

Cohort Analysis SQL: Login Table
Image Source

Step 2: Group Data into Different Cohorts

Calculating Retention Rate requires you to group each user visit by login week. You can leverage the DATEPART() function of Microsoft SQL Server to get a specified part (minute, week, quarter, month, etc.) of any mentioned date. So, to get week data, use the following SQL query:

Now, we’ll calculate the first week of login for every user using the MIN function and GROUP BY to return the first login week of every user:

Cohort Analysis SQL: GROUP BY Output
Image Source

Step 3: Calculating Retention Rate

Next, for calculating Retention Rate, write the INNER JOIN function and group the results of “login_week” and “first” as follows:

Extract the difference in login_week & first and calculate how many weeks are involved in this data using the following code:

Select m.user_id,m.login_week,n.first as first,
m.login_week-first as week_number from
(SELECT user_id, DATEPART(week,login_date)
AS login_week FROM login GROUP BY user_id,
DATEPART(week,login_date)) m, (SELECT user_id,
min (DATEPART(week,login_date)) AS first FROM login
GROUP BY user_id) n where m.user_id=n.user_id;
Cohort Analysis SQL: Retention Calculation
Image Source
What Makes Hevo’s Data Streaming and Loading Unique?

Providing a high-quality ETL solution can be a difficult task if you have a large volume of data. Hevo’s automated, No-code platform empowers you with everything you need to have for a smooth data replication experience.

Check out what makes Hevo amazing:

  • Fully Managed: Hevo requires no management and maintenance as it is a fully automated platform.
  • Data Transformation: Hevo provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Faster Insight Generation: Hevo offers near real-time data replication so you have access to real-time insight generation and faster decision making. 
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 150+ sources (with 40+ free sources) that can help you scale your data infrastructure as required.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-day free trial!

Step 4: Perform Cohort Analysis SQL

Finally, merge the results of step 3 to a Cohort Table that contains a row for every first week and a column for every week number (that retains a number of users after n weeks to login into the portal).

The following Query supports you in calculating the retention rate in SQL:

You can use any Data visualization tool to generate an elegant representation of your Cohort Table Analysis.

Cohort Analysis SQL: Final Output
Image Source

Applications of Cohort Analysis SQL

There are numerous benefits of applying Cohort Analysis to a customer dataset, and its application expands to many verticals. This section discusses the following key applications of Cohort Analysis SQL:

  • Calculating Customer Retention: Companies can predict the future change in customer behavior by analyzing them over time. Therefore, using Cohort Analysis SQL, companies can predict Customer Retention for the foreseeable future.
  • Identifying Better Products: Cohort Analysis is a key tool for e-commerce companies as it allows them to identify products that can produce the highest increase in their sales.
  • High Performing Website Flagging: Companies leverage Cohort Analysis SQL to evaluate which of their websites(or webpages) are getting the highest number of visitors. This data help companies to flag those web pages and further enhance the retention statistics.
  • Analyze Customer Churn: Retail businesses use Cohort Analysis SQL to test their various hypotheses. This way, companies are able to know for sure whether a customer, action, or attribute can generate another action, such as situations in which sign-ups for a promotion decrease or increase the customer churn (User percentage that discontinued product use in a specific time period).


The article introduced you to Cohort Analysis and Retention. It also provided you with a step-by-step guide and explained their simple application. Using the 4 simple steps explained in this blog, you can seamlessly implement Cohort Analysis SQL for your business and derive actionable insights.

Visit our Website to Explore Hevo

Now, to run queries or perform Data Analytics on your raw data, you first need to export this data to a Data Warehouse. This will require you to custom code complex scripts to develop the ETL processes. Hevo Data can automate your data transfer process, hence allowing you to focus on other aspects of your business like Analytics, Customer Management, etc. This platform allows you to transfer data from 150+ sources to Cloud-based Data Warehouses like Amazon Redshift, Snowflake, Google BigQuery, etc. It will provide you with a hassle-free experience and make your work life much easier.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand.

Share your understanding of Cohort Analysis SQL in the comments below!

Abhinav Chola
Research Analyst, Hevo Data

Abhinav is a data science enthusiast who loves data analysis and writing technical content. He has authored numerous articles covering a wide array of subjects in data integration and infrastructure.

No-code Data Pipeline For Your Data Warehouse