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
Triangle Classification Problem OverviewInput: Side LengthsABC101010558CASE WHENLogicOutput: Triangle TypesABCtriangle_type101010Equilateral558IsoscelesClassification Rules:• Invalid: A+B≤C• Equilateral: A=B=C• Isosceles: Two equal• Scalene: All different
Understanding the Visualization
1
Input
Triangle side lengths A, B, C
2
Validate
Check triangle inequality
3
Classify
Determine triangle type
Key Takeaway
🎯 Key Insight: Always validate triangle inequality before classifying triangle types
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