Find users who have both the developer and remote tags

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

MongoDB logo
MongoDB
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
user_idINTId of the user. Primary key.
nameVARCHAR(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):

ColumnTypeMeaning
idINTId of the tag row. Primary key, auto-generated.
user_idINTUser the tag belongs to. Foreign key to Users.
tagVARCHAR(50)The tag, in lowercase letters. Not null.

Output Contract

One row per user who has both tags, with these columns in this order:

ColumnMeaning
user_idId of the user
nameName of the user

Order the result by user_id ascending. user_id is unique, so the order has no ties.

Example

Users:

user_idname
1Alice
2Bob
3Charlie
4Diana

UserTags:

iduser_idtag
11developer
21remote
31python
42developer
52onsite
63remote
73designer
84developer
94remote

Expected result:

user_idname
1Alice
4Diana

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.