Count user signups per country for the first quarter of 2024
Company: Dropbox
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Count how many users signed up in each country with a `signup_date` from 2024-01-01 to 2024-03-31 inclusive. Return each country's name and its signup count, listing only countries with at least one signup in that range.
### Input Table(s)
`countries`
| Column | Type | Meaning |
|---|---|---|
| country_id | INT | Unique id of the country |
| country_name | VARCHAR(100) | Name of the country (NOT NULL) |
Primary key: `country_id`.
`users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Unique id of the user |
| username | VARCHAR(100) | The user's name (NOT NULL) |
| country_id | INT | Country of the user, references `countries.country_id` |
| signup_date | DATE | Date the user signed up (NOT NULL) |
Primary key: `user_id`. Foreign key: `users.country_id` references `countries.country_id`.
### Output Contract
- Columns, in this order: `country_name`, `signup_count`.
- `signup_count` is an integer: the number of users in that country whose `signup_date` is between 2024-01-01 and 2024-03-31, both ends included.
- One row per country with `signup_count` of at least 1.
- Order by `signup_count` descending, then `country_name` ascending.
- No rounding is needed.
### Example
`countries`
| country_id | country_name |
|---|---|
| 1 | Brazil |
| 2 | Canada |
| 3 | France |
| 4 | Japan |
`users`
| user_id | username | country_id | signup_date |
|---|---|---|---|
| 101 | ana | 1 | 2024-01-15 |
| 102 | bruno | 1 | 2024-03-31 |
| 103 | carla | 1 | 2024-04-01 |
| 104 | dave | 2 | 2023-12-31 |
| 105 | emma | 2 | 2024-02-29 |
| 106 | felix | 3 | 2024-01-01 |
| 107 | gina | 3 | 2024-03-10 |
| 108 | hiro | 4 | 2024-05-20 |
Expected result:
| country_name | signup_count |
|---|---|
| Brazil | 2 |
| France | 2 |
| Canada | 1 |
Brazil counts ana and bruno (2024-03-31 is the last day and is included) but not carla, who signed up on 2024-04-01. France counts felix (2024-01-01, the first day) and gina. Canada counts only emma. Japan has no signup in range, so it does not appear. Brazil and France tie at 2 and are ordered by name.
### Constraints and Clarifications
- `users` has at most 10,000 rows and `countries` has at most 200 rows.
- Each `user_id` is unique, so each row is one signup.
- Every `country_id` in `users` exists in `countries`. A user row with a NULL `country_id` belongs to no country and is not counted.
- Countries with no signups in the range are left out; do not return them with a count of 0.
- If two countries share a name, they are still separate countries and get separate rows.
```hint Date boundaries
Both 2024-01-01 and 2024-03-31 count. Choose a range test that keeps both ends and would still be right if `signup_date` were a timestamp.
```
Overview: Write a PostgreSQL query that joins users to countries and counts how many users signed up in each country during the first quarter of 2024. It tests inner joins, inclusive date-range filtering, grouping on a key, and a tie-breaker that makes the sort order deterministic.
Count how many users signed up in each country with a signup_date from 2024-01-01 to 2024-03-31 inclusive. Return each country's name and its signup count, listing only countries with at least one signup in that range.
Tables:
- countries(country_id INTEGER PRIMARY KEY, country_name VARCHAR(100) NOT NULL): one row per country. country_name is not guaranteed to be unique.
- users(user_id INTEGER PRIMARY KEY, username VARCHAR(100) NOT NULL, country_id INTEGER REFERENCES countries(country_id), signup_date DATE NOT NULL): one row per user. country_id may be NULL.
Output columns, in this order: country_name, signup_count.
- signup_count is an integer: the number of users in that country whose signup_date is between 2024-01-01 and 2024-03-31, both ends included.
- Return one row per country with a signup_count of at least 1.
- Order by signup_count descending, then country_name ascending.
- No rounding is needed.
Clarifications:
- users has at most 10,000 rows and countries has at most 200 rows.
- Each user_id is unique, so each row is one signup.
- Every non-NULL country_id in users exists in countries. A user row with a NULL country_id belongs to no country and is not counted.
- Countries with no signups in the range are left out; do not return them with a count of 0.
- If two countries share a name, they are still separate countries and get separate rows.
Tables
countries(country_id INTEGER, country_name VARCHAR(100))
users(user_id INTEGER, username VARCHAR(100), country_id INTEGER, signup_date DATE)
Hints
- Both 2024-01-01 and 2024-03-31 count. Choose a range test that keeps both ends and would still be right if signup_date were a timestamp.
- A country appears only when at least one of its users falls inside the range, and a user with no country adds to no row.