Find users who have both the developer and remote tags
Company: MongoDB
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Find all users who have both the `'developer'` and `'remote'` tags. A user can have zero or more tags, one row per tag in `UserTags`. Return `user_id` and `name`, ordered by `user_id` ascending.
### Input Table(s)
Table `Users` (primary key `user_id`):
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Id of the user. Primary key. |
| name | VARCHAR(100) | Name of the user. Not null. |
Table `UserTags` (primary key `id`, foreign key `user_id` references `Users.user_id`, each `(user_id, tag)` pair unique):
| Column | Type | Meaning |
|---|---|---|
| id | INT | Id of the tag row. Primary key, auto-generated. |
| user_id | INT | User the tag belongs to. Foreign key to `Users`. |
| tag | VARCHAR(50) | The tag, in lowercase letters. Not null. |
### Output Contract
One row per user who has both tags, with these columns in this order:
| Column | Meaning |
|---|---|
| user_id | Id of the user |
| name | Name of the user |
Order the result by `user_id` ascending. `user_id` is unique, so the order has no ties.
### Example
`Users`:
| user_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
| 4 | Diana |
`UserTags`:
| id | user_id | tag |
|---|---|---|
| 1 | 1 | developer |
| 2 | 1 | remote |
| 3 | 1 | python |
| 4 | 2 | developer |
| 5 | 2 | onsite |
| 6 | 3 | remote |
| 7 | 3 | designer |
| 8 | 4 | developer |
| 9 | 4 | remote |
Expected result:
| user_id | name |
|---|---|
| 1 | Alice |
| 4 | Diana |
Alice (user_id 1) has both `'developer'` and `'remote'` tags. Bob has `'developer'` but not `'remote'`. Charlie has `'remote'` but not `'developer'`. Diana has both. So Alice and Diana are returned.
### Constraints and Clarifications
- `Users` has at most 10,000 rows.
- `UserTags` has at most 50,000 rows.
- Each `(user_id, tag)` combination in `UserTags` is unique.
- Tag values consist of lowercase English letters only, so an exact string match is enough.
- A user with other tags besides the two still qualifies, as Alice does with `'python'`.
- A user with no tags, or with only one of the two, is not returned.
- Each qualifying user appears exactly once.
```hint Rows versus users
A single `UserTags` row holds one tag. Think about what has to be checked across several rows of the same user.
```
Overview: Write a SQL query that returns every user who has both the developer and remote tags, given a users table and a one-row-per-tag table. It tests relational division, filtering before aggregation, and counting distinct values per group with HAVING.
Find all users who have both the 'developer' and the 'remote' tags. A user can have zero or more tags, stored as one row per tag in usertags. Return user_id and name, ordered by user_id ascending.
Tables:
- `users(user_id INTEGER PRIMARY KEY, name VARCHAR(100) NOT NULL)`: one row per user. user_id is the id of the user and name is the name of the user.
- `usertags(id INTEGER PRIMARY KEY, user_id INTEGER, tag VARCHAR(50) NOT NULL)`: one row per tag a user has. id is the id of the tag row, user_id references users.user_id, and tag is the tag in lowercase letters. Each (user_id, tag) pair is unique.
Output: one row per user who has both tags, with the columns user_id and name in that order. Order the result by user_id ascending. user_id is unique, so the order has no ties.
Rules:
- users has at most 10,000 rows and usertags has at most 50,000 rows.
- Tag values consist of lowercase English letters only, so an exact string match is enough.
- A user with other tags besides the two still qualifies.
- A user with no tags, or with only one of the two, is not returned.
- Each qualifying user appears exactly once.
Tables
users(user_id INTEGER, name VARCHAR(100))
usertags(id INTEGER, user_id INTEGER, tag VARCHAR(50))
Hints
- A single usertags row holds one tag. Think about what has to be checked across several rows of the same user.
- Each qualifying user must appear once in the output, even when that user has many tags.