Find Values Owned Only by the Selected User
Company: Meta
Role: Data Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Technical Screen
Find the values associated with a chosen user that are not associated with any other user.
### Input Tables
`user_values`
| Column | Type | Meaning |
| --- | --- | --- |
| `row_id` | INTEGER | Primary key |
| `user_id` | INTEGER | Non-null user identifier |
| `item_value` | TEXT | Non-null value associated with that user |
`selected_user` contains exactly one row with one non-null INTEGER column, `user_id`.
These tables are an explicit relational representation of a mapping from users to their values. A user may have repeated rows for the same value.
### Output Contract
Write one read-only PostgreSQL query returning a single column, `item_value`. Return each value owned by the selected user exactly once only if no different user owns that value. Sort values ascending. Compare values exactly as stored.
### Example
`selected_user` contains `user_id = 10`.
| row_id | user_id | item_value |
| --- | --- | --- |
| 1 | 10 | apple |
| 2 | 10 | pear |
| 3 | 10 | pear |
| 4 | 20 | apple |
| 5 | 30 | plum |
Expected result:
| item_value |
| --- |
| pear |
### Constraints and Clarifications
- Repeated ownership by the selected user does not disqualify a value.
- Ownership by even one different user disqualifies it.
- A selected user with no values produces no rows.
- Values are nonempty lowercase English strings; there are no null values.
```hint Separate repetition from shared ownership
Several rows for one user are different from a matching row for another user. Check the identity of the owner when excluding shared values.
```
Overview: Use PostgreSQL to find values belonging to one selected user and no other user, while handling repeated values and shared ownership.
Read the full Meta Data Engineer interview experience this question came from
Find the values associated with a chosen user that are not associated with any other user.
Table `user_values` has columns `row_id` (INTEGER, primary key), `user_id` (INTEGER, non-null user identifier) and `item_value` (TEXT, non-null value associated with that user). It is an explicit relational representation of a mapping from users to their values; a user may have repeated rows for the same value.
Table `selected_user` contains exactly one row with one non-null INTEGER column, `user_id`, identifying the chosen user.
Write one read-only PostgreSQL query returning a single column, `item_value`. Return each value owned by the selected user exactly once, and only if no different user owns that value. Sort values ascending. Compare values exactly as stored.
Clarifications:
- Repeated ownership by the selected user does not disqualify a value.
- Ownership by even one different user disqualifies it.
- A selected user with no values produces no rows.
- Values are nonempty lowercase English strings; there are no null values.
Tables
user_values(row_id INTEGER, user_id INTEGER, item_value TEXT)
selected_user(user_id INTEGER)
Hints
- Separate repetition from shared ownership: several rows for one user are different from a matching row for another user. Check the identity of the owner when excluding shared values.