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 NameType
Aint
Bint
Cint

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
Triangle Types ClassificationEquilateralAll sides equalaaaIsoscelesTwo sides equalaabScaleneAll sides differentabc2310Invalid2+3 โ‰ค 10Triangle Inequality CheckFor sides A, B, C to form a valid triangle:โ€ข A + B > Cโ€ข A + C > Bโ€ข B + C > AExample: (2, 3, 10) โ†’ 2 + 3 = 5 โ‰ค 10 โœ— Invalid TriangleSQL Classification LogicCASE WHEN A+Bโ‰คC OR A+Cโ‰คB OR B+Cโ‰คA THEN 'Not A Triangle'WHEN A=B AND B=C THEN 'Equilateral'WHEN A=B OR B=C OR A=C THEN 'Isosceles'ELSE 'Scalene' END
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.
Asked in
Amazon 25 Microsoft 18 Google 15 Meta 12
28.5K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen