HackerRank SQL
Published in
2 min readAug 3, 2020
Type of Triangle
Problem:
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 sides of equal length.
- Isosceles: It’s a triangle with sides of equal length.
- Scalene: It’s a triangle with 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
Logic:
From the outside to the inside: SELECT IF(criteria, ‘Not A Triangle’) FROM TRIANGLES; AND THEN WORK ON THE “criteria” part by using two IF statements: IF(A=B AND B=C,’’,IF(A=B OR B=C OR A=C,’’,’’ ))
Solution:
SELECT IF(A+B>C AND A+C>B AND B+C>A,
IF(A=B AND B=C,'Equilateral',
IF(A=B OR B=C OR A=C,
'Isosceles',
'Scalene')),
'Not A Triangle') FROM TRIANGLES;