Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 58e1824

Browse files
committed
Add a question
1 parent 37dba45 commit 58e1824

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

‎README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,3 +1830,76 @@ WHERE year=2021 AND revenue > 0;
18301830
---
18311831

18321832
**[⬆ Back to Top](#sql-coding-challenges-for-beginners)**
1833+
1834+
## 52. All Valid Triplets
1835+
1836+
Your district is joining a national competition for students, and wants to select one student from each one of its three schools such that:
1837+
- `student_A` is selected from `School_A`,
1838+
- `student_B` is selected from `School_B`,
1839+
- `student_C` is selected from `School_C`,
1840+
- and the selected students' names and IDs are unique such that no two students share the same name or ID.
1841+
1842+
Write a SQL query to find all the possible triplets. Return the result table in any order. Here is an example:
1843+
1844+
```
1845+
School_A table:
1846+
+------------+--------------+
1847+
| student_id | student_name |
1848+
+------------+--------------+
1849+
| 1 | Alice |
1850+
| 2 | Bob |
1851+
+------------+--------------+
1852+
1853+
School_B table:
1854+
+------------+--------------+
1855+
| student_id | student_name |
1856+
+------------+--------------+
1857+
| 3 | Tom |
1858+
+------------+--------------+
1859+
1860+
School_C table:
1861+
+------------+--------------+
1862+
| student_id | student_name |
1863+
+------------+--------------+
1864+
| 3 | Tom |
1865+
| 2 | Jerry |
1866+
| 10 | Alice |
1867+
+------------+--------------+
1868+
```
1869+
1870+
```
1871+
Result table:
1872+
+-----------+-----------+-----------+
1873+
| student_A | student_B | student_C |
1874+
+-----------+-----------+-----------+
1875+
| Alice | Tom | Jerry |
1876+
| Bob | Tom | Alice |
1877+
+-----------+-----------+-----------+
1878+
```
1879+
1880+
As you can see, out of all possible triplets, `(Alice, Tom, Tom), (Alice, Tom, Alice), (Bob, Tom, Tom), (Bob, Tom, Jerry), (Alice, Tom, Jerry), (Bob, Tom, Alice)`, only the last two are valid since they contain students with unique names as well as IDs.
1881+
1882+
<details><summary>Solution</summary>
1883+
1884+
```sql
1885+
SELECT
1886+
A.student_name AS 'student_A',
1887+
B.student_name AS 'student_B',
1888+
C.student_name AS 'student_C'
1889+
FROM
1890+
School_A A CROSS JOIN School_B B CROSS JOIN School_C C
1891+
WHERE
1892+
A.student_id != B.student_id AND
1893+
A.student_id != C.student_id AND
1894+
B.student_id != C.student_id
1895+
AND
1896+
A.student_name != B.student_name AND
1897+
A.student_name != C.student_name AND
1898+
B.student_name != C.student_name;
1899+
```
1900+
1901+
</details>
1902+
1903+
---
1904+
1905+
**[⬆ Back to Top](#sql-coding-challenges-for-beginners)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /