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
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1557,3 +1557,37 @@ ON employee.team_id = team.team_id;
1557
1557
---
1558
1558
1559
1559
**[⬆ Back to Top](#sql-coding-challenges-for-beginners)**
1560
+
1561
+
## 46. Monsters using CASE
1562
+
1563
+
You have access to two tables `top_half` and `bottom_half`. Write a SQL query to return the results as outlined below. The IDs on the tables match to make a full monster. For the `species`, if the monster has more heads than arms or more tails than legs, it is a `BEAST`, else it is a `WEIRDO`. Order your results by `species`.
1564
+
1565
+
```
1566
+
top_half bottom_half output
1567
+
-------- ----------- ------
1568
+
id id id
1569
+
heads legs heads
1570
+
arms tails legs
1571
+
arms
1572
+
tails
1573
+
species
1574
+
```
1575
+
1576
+
<details><summary>Solution</summary>
1577
+
1578
+
```sql
1579
+
SELECTT.id, heads, legs, arms, tails,
1580
+
CASE
1581
+
WHEN heads > arms OR tails > legs THEN 'BEAST'
1582
+
ELSE 'WEIRDO'
1583
+
END AS species
1584
+
FROM top_half T JOIN bottom_half B
1585
+
ONT.id=B.id
1586
+
ORDER BY species;
1587
+
```
1588
+
1589
+
</details>
1590
+
1591
+
---
1592
+
1593
+
**[⬆ Back to Top](#sql-coding-challenges-for-beginners)**
0 commit comments