You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+73Lines changed: 73 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1830,3 +1830,76 @@ WHERE year=2021 AND revenue > 0;
1830
1830
---
1831
1831
1832
1832
**[⬆ 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_nameAS'student_A',
1887
+
B.student_nameAS'student_B',
1888
+
C.student_nameAS'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_idAND
1893
+
A.student_id!=C.student_idAND
1894
+
B.student_id!=C.student_id
1895
+
AND
1896
+
A.student_name!=B.student_nameAND
1897
+
A.student_name!=C.student_nameAND
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