SQL
Lab
Meta Meta Interview Question 09

New
vs Returning
Users

Classify users as new or returning based on activity.

Table Schema

Inspect
Table

interactive
ColumnType
user_idinteger
activity_datedate

Sample Data

Input
Output

Sample Input: activity
user_idactivity_date
1012024-01-01
1012024-01-02
1022024-01-01
1032024-01-02
1042024-01-03
Expected Output
user_iduser_type
101returning
102new
103new
104new

SQL Editor

Run
Query

postgresql
Waiting for query

user_iduser_type
101returning
102new
103new
104new

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT user_id,
  CASE WHEN COUNT(DISTINCT activity_date) > 1 THEN 'returning' ELSE 'new' END AS user_type
FROM activity
GROUP BY user_id;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving