HackerRank SQL
Published in
2 min readMar 26, 2021
Type of Triangle
Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
- Equilateral: It’s a triangle with 3 sides of equal length.
- Isosceles: It’s a triangle with 2 sides of equal length.
- Scalene: It’s a triangle with 3 sides of differing lengths.
- Not A Triangle: The given values of A, B, and C don’t form a triangle.
Input Format
The TRIANGLES table is described as follows:
Each row in the table denotes the lengths of each of a triangle’s three sides.
Sample Input
Sample Output
Isosceles
Equilateral
Scalene
Not A Triangle
Solution(My SQL):
select case when A+B > C and A+C > B and B+C > A then -- is a triangle
case when A = B and B = C and A = C then 'Equilateral'
when A = B or B = C or A = C then 'Isosceles'
else 'Scalene'
end
-- is not a triangle else 'Not A Triangle' end
from TRIANGLES
;