cswork / libraryOpen practice workspace

Coding questions · Meta

Find Values Owned Only by the Selected User

Use PostgreSQL to find values belonging to one selected user and no other user, while handling repeated values and shared ownership.

Find the values associated with a chosen user that are not associated with any other user.

Input Tables

user_values

ColumnTypeMeaning
row_idINTEGERPrimary key
user_idINTEGERNon-null user identifier
item_valueTEXTNon-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_iduser_iditem_value
110apple
210pear
310pear
420apple
530plum

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.

Values Owned Only by the Selected User

Table user_values

columntypeconstraints
row_idINTEGERPRIMARY KEY
user_idINTEGERNOT NULL
item_valueTEXTNOT NULL

Sample data:

item_valuerow_iduser_id
kiwi142
apple242
mango342
mango442
apple517
banana642
banana723
banana823
cherry917
cherry1023
grape1142
grapefruit1217
lemon1358
fig1442
lemon1558

Table selected_user

columntypeconstraints
user_idINTEGERNOT NULL

Sample data:

user_id
42

Expected output

item_value
fig
grape
kiwi
mango

Hints

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