> For the complete documentation index, see [llms.txt](https://jaywin.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jaywin.gitbook.io/leetcode/solutions/1623-all-valid-triplets-that-can-represent-a-country.md).

# 1623. All Valid Triplets That Can Represent a Country

<https://leetcode.com/problems/all-valid-triplets-that-can-represent-a-country>

## Description

Table: `SchoolA`

```
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| student\_id    | int     |
| student\_name  | varchar |
+---------------+---------+
student\_id is the primary key for this table.
Each row of this table contains the name and the id of a student in school A.
All student\_name are distinct.
```

Table: `SchoolB`

```
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| student\_id    | int     |
| student\_name  | varchar |
+---------------+---------+
student\_id is the primary key for this table.
Each row of this table contains the name and the id of a student in school B.
All student\_name are distinct.
```

Table: `SchoolC`

```
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| student\_id    | int     |
| student\_name  | varchar |
+---------------+---------+
student\_id is the primary key for this table.
Each row of this table contains the name and the id of a student in school C.
All student\_name are distinct.
```

There is a country with three schools, where each student is enrolled in **exactly one** school. The country is joining a competition and wants to select one student from each school to represent the country such that:

* `member_A` is selected from `SchoolA`,
* `member_B` is selected from `SchoolB`,
* `member_C` is selected from `SchoolC`, and
* The selected students' names and IDs are pairwise distinct (i.e. no two students share the same name, and no two students share the same ID).

Write an SQL query to find all the possible triplets representing the country under the given constraints.

Return the result table in **any order**.

The query result format is in the following example.

```
SchoolA table:
+------------+--------------+
| student\_id | student\_name |
+------------+--------------+
| 1          | Alice        |
| 2          | Bob          |
+------------+--------------+
SchoolB table:
+------------+--------------+
| student\_id | student\_name |
+------------+--------------+
| 3          | Tom          |
+------------+--------------+
SchoolC table:
+------------+--------------+
| student\_id | student\_name |
+------------+--------------+
| 3          | Tom          |
| 2          | Jerry        |
| 10         | Alice        |
+------------+--------------+
Result table:
+----------+----------+----------+
| member\_A | member\_B | member\_C |
+----------+----------+----------+
| Alice    | Tom      | Jerry    |
| Bob      | Tom      | Alice    |
+----------+----------+----------+
Let us see all the possible triplets.
- (Alice, Tom, Tom) --> Rejected because member\_B and member\_C have the same name and the same ID.
- (Alice, Tom, Jerry) --> Valid triplet.
- (Alice, Tom, Alice) --> Rejected because member\_A and member\_C have the same name.
- (Bob, Tom, Tom) --> Rejected because member\_B and member\_C have the same name and the same ID.
- (Bob, Tom, Jerry) --> Rejected because member\_A and member\_C have the same ID.
- (Bob, Tom, Alice) --> Valid triplet.
```

## ac

```java
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jaywin.gitbook.io/leetcode/solutions/1623-all-valid-triplets-that-can-represent-a-country.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
