Classifying Triangles by Lengths - Problem
Given a table containing the three side lengths of various triangles, write a SQL query to classify each triangle based on its geometric properties.
Table: Triangles
| Column Name | Type |
|---|---|
| A | int |
| B | int |
| C | int |
Where (A, B, C) represents the primary key, and each row contains the lengths of a triangle's three sides.
Classification Rules:
- Equilateral: All three sides are equal (A = B = C)
- Isosceles: Exactly two sides are equal
- Scalene: All three sides are different
- Not A Triangle: The sides don't satisfy the triangle inequality theorem
Triangle Inequality: For three sides to form a valid triangle, the sum of any two sides must be greater than the third side: A + B > C, A + C > B, and B + C > A.
Return the result table with each triangle classified appropriately. The order doesn't matter.
Input & Output
example_1.sql โ Basic Triangle Types
$
Input:
Triangles table:
+---+---+---+
| A | B | C |
+---+---+---+
| 20| 20| 23|
| 20| 20| 20|
| 20| 21| 22|
| 13| 14| 30|
+---+---+---+
โบ
Output:
+---+---+---+---------------+
| A | B | C | triangle_type |
+---+---+---+---------------+
| 20| 20| 23| Isosceles |
| 20| 20| 20| Equilateral |
| 20| 21| 22| Scalene |
| 13| 14| 30| Not A Triangle|
+---+---+---+---------------+
๐ก Note:
Row 1: Two sides equal (A=B=20), so Isosceles. Row 2: All sides equal (20,20,20), so Equilateral. Row 3: All sides different, so Scalene. Row 4: 13+14=27 < 30, violates triangle inequality.
example_2.sql โ Edge Cases
$
Input:
Triangles table:
+---+---+---+
| A | B | C |
+---+---+---+
| 10| 10| 20|
| 15| 15| 15|
| 7 | 8 | 9 |
+---+---+---+
โบ
Output:
+---+---+---+---------------+
| A | B | C | triangle_type |
+---+---+---+---------------+
| 10| 10| 20| Not A Triangle|
| 15| 15| 15| Equilateral |
| 7 | 8 | 9 | Scalene |
+---+---+---+---------------+
๐ก Note:
Row 1: 10+10=20, which equals C, so not a valid triangle (needs to be greater than). Row 2: All sides equal. Row 3: All sides different and valid triangle.
example_3.sql โ Isosceles Variations
$
Input:
Triangles table:
+---+---+---+
| A | B | C |
+---+---+---+
| 5 | 12| 5 |
| 8 | 6 | 6 |
| 4 | 4 | 7 |
+---+---+---+
โบ
Output:
+---+---+---+---------------+
| A | B | C | triangle_type |
+---+---+---+---------------+
| 5 | 12| 5 | Isosceles |
| 8 | 6 | 6 | Isosceles |
| 4 | 4 | 7 | Isosceles |
+---+---+---+---------------+
๐ก Note:
All three rows show different patterns of isosceles triangles: A=C, B=C, and A=B respectively. Each has exactly two equal sides.
Constraints
- 1 โค A, B, C โค 100
- The table contains at least 1 row
- All side lengths are positive integers
- Triangle inequality: A + B > C, A + C > B, B + C > A must all be true for valid triangles
Visualization
Tap to expand
Understanding the Visualization
1
Validate Triangle
Check if three sides can form a valid triangle using triangle inequality
2
Count Equal Sides
Determine how many sides are equal to each other
3
Apply Classification
Use the equality pattern to classify the triangle type
4
Return Result
Output the appropriate triangle classification
Key Takeaway
๐ฏ Key Insight: Always validate the triangle inequality first, then use equality patterns to classify valid triangles efficiently using SQL CASE statements.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code