All Valid Triplets That Can Represent a Country - Problem

You are given three tables representing students from three different schools in a country. Each student has a unique ID within their school and a distinct name within their school.

The country wants to select one student from each school to represent them in a competition with the following constraints:

  • member_A must be selected from SchoolA
  • member_B must be selected from SchoolB
  • member_C must be selected from SchoolC
  • The selected students must have pairwise distinct names and IDs (no two students can share the same name or the same ID)

Write a SQL query to find all possible triplets that can represent the country under these constraints.

Table Schema

SchoolA
Column Name Type Description
student_id PK int Unique student identifier within SchoolA
student_name varchar Unique student name within SchoolA
Primary Key: student_id
SchoolB
Column Name Type Description
student_id PK int Unique student identifier within SchoolB
student_name varchar Unique student name within SchoolB
Primary Key: student_id
SchoolC
Column Name Type Description
student_id PK int Unique student identifier within SchoolC
student_name varchar Unique student name within SchoolC
Primary Key: student_id

Input & Output

Example 1 — Multiple Valid Triplets
Input Tables:
SchoolA
student_id student_name
1 Alice
2 Bob
SchoolB
student_id student_name
3 Tom
SchoolC
student_id student_name
3 Jerry
6 Alice
Output:
member_A member_B member_C
1 3 6
2 3 6
💡 Note:

From the cross join, we get several combinations, but only some are valid:

  • Alice(1) + Tom(3) + Jerry(3): Invalid - duplicate ID 3
  • Alice(1) + Tom(3) + Alice(6): Invalid - duplicate name Alice
  • Alice(1) + Tom(3) + Alice(6): Valid - all IDs and names distinct
  • Bob(2) + Tom(3) + Alice(6): Valid - all IDs and names distinct
Example 2 — No Valid Triplets
Input Tables:
SchoolA
student_id student_name
1 Alice
SchoolB
student_id student_name
1 Bob
SchoolC
student_id student_name
2 Alice
Output:
member_A member_B member_C
💡 Note:

The only possible combination is Alice(1) + Bob(1) + Alice(2), but this is invalid because:

  • Student IDs 1 and 1 are duplicated (Alice from SchoolA and Bob from SchoolB both have ID 1)
  • Student names 'Alice' appears in both SchoolA and SchoolC

Since no valid triplets exist, the result is empty.

Constraints

  • 1 ≤ student_id ≤ 1000
  • student_name consists of uppercase and lowercase English letters
  • All student_name within each school are distinct
  • All student_id within each school are unique

Visualization

Tap to expand
All Valid Triplets That Can Represent a Country INPUT School A ID Name 1 Alice 2 Bob School B ID Name 2 Carol 3 Dave School C ID Name 3 Eve 4 Frank 3 Schools, 2 students each Find valid triplets ALGORITHM STEPS 1 Triple Nested Loop Iterate: A x B x C 2 Check IDs a.id != b.id != c.id 3 Check Names a.name != b.name != c.name 4 Add Valid Triplet Store (a, b, c) in result Validation Example (Alice,1) + (Carol,2) + (Eve,3) IDs: 1,2,3 - All different [OK] Names: All unique [OK] Time: O(n1 * n2 * n3) Space: O(result size) FINAL RESULT Valid Triplets Found Triplet 1 (Alice,1) (Carol,2) (Eve,3) Triplet 2 (Alice,1) (Carol,2) (Frank,4) Triplet 3 (Alice,1) (Dave,3) (Frank,4) Triplet 4 (Bob,2) (Dave,3) (Frank,4) Total: 4 Valid Teams Status: [OK] Key Insight: A valid triplet requires ALL THREE students to have different IDs AND different names. This is essentially a 3-way JOIN with inequality conditions: each pair must satisfy both constraints. The brute-force approach checks all combinations but can be optimized with indexing for large datasets. TutorialsPoint - All Valid Triplets That Can Represent a Country | Optimal Solution
Asked in
Amazon 12 Microsoft 8
32.1K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen