Classifying Triangles by Lengths - Problem

Given a table Triangles that contains the lengths of three sides of triangles, write a SQL query to classify each triangle based on its side lengths.

Triangle Classification Rules:

  • Equilateral: All three sides are equal in length
  • Isosceles: Exactly two sides are equal in length
  • Scalene: All three sides have different lengths
  • Not A Triangle: The given side lengths cannot form a valid triangle

Triangle Validity Rule: For three sides A, B, and C to form a triangle, the sum of any two sides must be greater than the third side.

Table Schema

Triangles
Column Name Type Description
A PK int Length of first side
B PK int Length of second side
C PK int Length of third side
Primary Key: (A, B, C)
Note: Each row represents the three sides of a triangle. The combination of A, B, C forms the primary key.

Input & Output

Example 1 — Mixed Triangle Types
Input Table:
A B C
10 10 10
13 15 30
20 21 22
Output:
A B C triangle_type
10 10 10 Equilateral
13 15 30 Not A Triangle
20 21 22 Scalene
💡 Note:

First triangle (10,10,10): All sides equal → Equilateral. Second triangle (13,15,30): 13+15=28 < 30, violates triangle inequality → Not A Triangle. Third triangle (20,21,22): All sides different and valid → Scalene.

Example 2 — Isosceles Triangle
Input Table:
A B C
5 5 8
10 12 10
Output:
A B C triangle_type
5 5 8 Isosceles
10 12 10 Isosceles
💡 Note:

Both triangles have exactly two sides of equal length: (5,5,8) has A=B, and (10,12,10) has A=C. Both satisfy triangle inequality and are classified as Isosceles.

Constraints

  • 1 ≤ A, B, C ≤ 100
  • All side lengths are positive integers

Visualization

Tap to expand
Classifying Triangles by Side Lengths INPUT A B C Type 20 20 20 ? 20 20 30 ? 20 30 40 ? 13 14 30 ? Triangle Types: Equilateral Isosceles Scalene Not A Triangle ALGORITHM STEPS 1 Check Validity A+B>C, B+C>A, A+C>B 2 Check Equilateral If A = B = C 3 Check Isosceles If A=B OR B=C OR A=C 4 Else Scalene All sides different SELECT A, B, C, CASE WHEN A+B<=C OR ... THEN 'Not A Triangle' WHEN A=B AND B=C THEN 'Equilateral' WHEN A=B OR B=C... END AS Type FINAL RESULT A B C Type 20 20 20 Equilateral 20 20 30 Isosceles 20 30 40 Scalene 13 14 30 Not A Triangle Classified Triangles: OK OK OK Invalid Key Insight: The Triangle Inequality Theorem states that the sum of any two sides must be greater than the third side. Use CASE statements with ordered conditions: check validity first, then equilateral, isosceles, and finally scalene. Order matters! Check from most specific (equilateral) to least specific (scalene) for correct classification. TutorialsPoint - Classifying Triangles by Lengths | Optimal Solution
Asked in
Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~8 min Avg. Time
856 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