Back to Practice
#0393

Pairs of Users in Same Country

MediumSQL24 min20 XP

Problem

Return user_id_1 and user_id_2 pairs where both users are in the same country. Return each pair once.

Why This Matters

SQL interviews usually test whether you can turn a business question into the right rows, grouping level, join type, and edge-case behavior.

Schema

users(user_id INTEGER, country TEXT)

Examples

Example 1
Inputusers 1, 2, and 4 are in IN
Outputthree unique pairs

The less-than condition avoids duplicate reversed pairs and self-pairs.

Constraints

  • Return only the requested columns.
  • Use deterministic ORDER BY when multiple rows are returned.
  • Assume SQLite-compatible SQL unless the prompt states otherwise.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core SQL case
Inputusers 1, 2, and 4 are in IN
Expected[[1,2],[1,4],[2,4]]

The less-than condition avoids duplicate reversed pairs and self-pairs.

Hidden Test Categories
Empty or missing matchesDuplicate rows or tiesNULL values where the pattern commonly breaks