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:
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.