0% found this document useful (0 votes)
9 views4 pages

User Retention Analysis SQL Queries

The document outlines a series of SQL queries aimed at analyzing user behavior to identify patterns related to retention. It includes steps to find users with completed transactions over three months, determine their retention status, compare transaction frequency and value, analyze transaction types for retained users, and assess retained users' first-month activity. The queries utilize various SQL functions and aggregations to extract meaningful insights from transaction data.

Uploaded by

novqcreatives
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

User Retention Analysis SQL Queries

The document outlines a series of SQL queries aimed at analyzing user behavior to identify patterns related to retention. It includes steps to find users with completed transactions over three months, determine their retention status, compare transaction frequency and value, analyze transaction types for retained users, and assess retained users' first-month activity. The queries utilize various SQL functions and aggregations to extract meaningful insights from transaction data.

Uploaded by

novqcreatives
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TASK-1- We need to analyse user behaviour to identify patterns

or combinations of actions that are strongly correlated with retention.

1)​ Finding all users who made at least one completed transaction in each of
the three months.
SELECT user_id
FROM transaction_tables
WHERE status = 'Completed'
GROUP BY user_id
HAVING
COUNT(DISTINCT EXTRACT(MONTH FROM date_of_transaction)) = 3;

OUTPUT

2) FINDING all users RETENTION STATUS


RetainedUsers AS (
SELECT
user_id
FROM transaction_tables
WHERE
status = 'Completed'
GROUP BY
user_id
HAVING
COUNT(DISTINCT EXTRACT(MONTH FROM date_of_transaction)) = 3
)
SELECT
udt.user_id,
CASE
WHEN ru.user_id IS NOT NULL THEN 'Retained'
ELSE 'Not Retained'
END AS retention_status
FROM user_details_table AS udt
LEFT JOIN RetainedUsers AS ru
ON udt.user_id = ru.user_id;
3) ComparING Transaction Frequency and Value
UserRetentionStatus AS (
SELECT
user_id,
CASE
WHEN COUNT(DISTINCT EXTRACT(MONTH FROM date_of_transaction))
=3
THEN 'Retained'
ELSE 'Not Retained'
END AS retention_status
FROM transaction_tables
WHERE
status = 'Completed'
GROUP BY
user_id
)
SELECT
urs.retention_status,
COUNT(t.txn_id) AS total_completed_transactions,
COUNT(DISTINCT t.user_id) AS total_users,
SUM([Link]) AS total_amount,
AVG([Link]) AS avg_transaction_amount
FROM transaction_tables AS t
JOIN UserRetentionStatus AS urs
ON t.user_id = urs.user_id
WHERE
[Link] = 'Completed'
GROUP BY
urs.retention_status;

4) Transaction Types for Retained Users


WITH
RetainedUsers AS (
SELECT
user_id
FROM transaction_tables
WHERE
status = 'Completed'
GROUP BY
user_id
HAVING
COUNT(DISTINCT EXTRACT(MONTH FROM date_of_transaction)) = 3
)
SELECT
t.type_of_transaction,
COUNT(t.txn_id) AS number_of_transactions,
SUM([Link]) AS total_amount,
COUNT(DISTINCT t.user_id) AS number_of_users
FROM transaction_tables AS t
JOIN RetainedUsers AS ru
ON t.user_id = ru.user_id
WHERE
[Link] = 'Completed'
GROUP BY
t.type_of_transaction
ORDER BY
number_of_transactions DESC;

5) Retained Users' First-Month Activity


WITH
RetainedUsers AS (
SELECT
user_id
FROM transaction_tables
WHERE
status = 'Completed'
GROUP BY
user_id
HAVING
COUNT(DISTINCT EXTRACT(MONTH FROM date_of_transaction)) = 3
),
FirstMonthTransactions AS (
SELECT
t.user_id,
COUNT(t.txn_id) AS first_month_txn_count,
MAX(t.date_of_transaction) AS last_txn_in_first_month,
MIN(t.date_of_transaction) AS first_txn_in_first_month
FROM transaction_tables AS t
JOIN RetainedUsers AS ru
ON t.user_id = ru.user_id
WHERE
[Link] = 'Completed'
AND EXTRACT(MONTH FROM t.date_of_transaction) = (
SELECT
EXTRACT(MONTH FROM MIN(date_of_transaction))
FROM transaction_tables
)
GROUP BY
t.user_id
)
SELECT
user_id,
first_month_txn_count,
DATEDIFF(last_txn_in_first_month, first_txn_in_first_month) AS
days_between_first_and_last_txn
FROM FirstMonthTransactions
ORDER BY
first_month_txn_count DESC;

6)

You might also like