Table: Triangles
+-------------+------+ | Column Name | Type | +-------------+------+ | A | int | | B | int | | C | int | +-------------+------+ (A, B, C) is the primary key for this table. Each row include the lengths of each of a triangle's three sides.
Write a query to find the type of triangle. Output one of the following for each row:
3 sides of equal length.2 sides of equal length.3 sides of differing lengths.A, B, and C don't form a triangle.Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Triangles table: +----+----+----+ | A | B | C | +----+----+----+ | 20 | 20 | 23 | | 20 | 20 | 20 | | 20 | 21 | 22 | | 13 | 14 | 30 | +----+----+----+ Output: +----------------+ | triangle_type | +----------------+ | Isosceles | | Equilateral | | Scalene | | Not A Triangle | +----------------+ Explanation: - Values in the first row from an Isosceles triangle, because A = B. - Values in the second row from an Equilateral triangle, because A = B = C. - Values in the third row from an Scalene triangle, because A != B != C. - Values in the fourth row cannot form a triangle, because the combined value of sides A and B is not larger than that of side C.
Problem Overview: Each row in the table contains three side lengths of a triangle. Your task is to classify the triangle as Equilateral, Isosceles, Scalene, or Not A Triangle based on the triangle inequality rule and equality of sides.
The key rule is the triangle inequality: the sum of any two sides must be greater than the third side. If this condition fails for any pair, the sides cannot form a triangle. Once the triangle validity is confirmed, classification becomes a straightforward comparison of the three side lengths.
Approach 1: Using CASE WHEN Statement (O(n) time, O(1) space)
This problem maps directly to conditional logic in SQL. You iterate through each row of the table and evaluate the triangle conditions using a CASE WHEN expression. The first check ensures the triangle inequality holds: a + b > c, a + c > b, and b + c > a. If any of these fail, return 'Not A Triangle'.
If the row passes the validity check, classify the triangle using side equality comparisons. When all three sides are equal (a = b AND b = c), the triangle is Equilateral. If exactly two sides match (a = b OR a = c OR b = c), it is Isosceles. If all sides are different, the triangle is Scalene. The CASE expression evaluates conditions sequentially, so ordering matters: check validity first, then equilateral, then isosceles.
This approach works efficiently because SQL evaluates each row independently. The database engine simply applies conditional checks without joins or additional scans, resulting in linear time relative to the number of rows in the table.
From a SQL perspective, this is a classic conditional transformation problem. It demonstrates how to embed business logic directly in queries using CASE expressions. These patterns appear frequently in database reporting queries and analytics pipelines where raw values must be categorized.
Recommended for interviews: The CASE WHEN approach is the expected solution. Interviewers want to see that you know the triangle inequality rule and can translate conditional logic into SQL. There is no need for subqueries or joins. A clean CASE expression with properly ordered conditions shows strong SQL fundamentals.
We can use the CASE WHEN statement to determine the type of the triangle.
First, we need to determine whether the three sides can form a triangle. If not, we return Not A Triangle.
Then, we check if the lengths of the three sides are equal. If they are, we return Equilateral.
Next, we check if there are two sides with equal length. If there are, we return Isosceles.
Otherwise, it means that the lengths of the three sides are all different, so we return Scalene.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| CASE WHEN Conditional Classification | O(n) | O(1) | Best general solution for SQL queries where each row must be categorized based on conditions. |
| CASE with Precomputed Validity Check | O(n) | O(1) | Useful when separating triangle validation logic from classification for readability in larger SQL pipelines. |
Leetcode 3053 - Classifying Triangles by Lengths - Solved & Explained by Everyday Data Science • Everyday Data Science • 521 views views
Practice Classifying Triangles by Lengths with our built-in code editor and test cases.
Practice on FleetCode