'Write a query identifying the type of each record in the TRIANGLES table using its three side lengths

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.



Solution 1:[1]

SELECT CASE 
          WHEN ((a.A + a.B) <= a.C) 
              THEN 'Not A Triangle'
            ELSE
            CASE
            WHEN ((a.A = a.B) AND (a.B = a.C))
               THEN 'Equilateral'
               ELSE 
               CASE                
                 WHEN ((a.A = a.B) or (a.B = a.C) or (a.A = a.C))
                     THEN 'Isosceles'
                     ELSE 
                        CASE
                            WHEN ((a.A != a.B) AND (a.B != a.C) AND (a.A != a.C))
                                THEN 'Scalene '
                            END
                 END
          END
       END 
FROM TRIANGLES a;

Solution 2:[2]

short version:

select case
when ((t.A+t.B)<=t.C)
    then 'Not A Triangle'
else
    case 
        when (t.A = t.B) and (t.A = t.C) then 'Equilateral'
    else
        case 
            when (t.A = t.B) or (t.A=t.C) or (t.C=t.B) then 'Isosceles'
        else
            'Scalene '
        end
    end
end 
from TRIANGLES as t

Solution 3:[3]

HackeRrank MySQL - Type of Triangle Quiz!

select case
when ((t.A+t.B)<=t.c) then 'Not A Triangle'
else
    case
    when (t.A=t.B) and (t.B=t.c) then 'Equilateral'
    else
        case
        when (t.A = t.B) or (t.A = t.C) or (t.B = t.C) then 'Isosceles'
        else
            'Scalene'
        end
    end
end
from triangles as t;

Solution 4:[4]

SELECT

CASE

WHEN (A=B AND B=C AND C=A) THEN 'Equilateral'
WHEN (A=B OR A=C OR B=C) AND (A + B > C AND B+C > A AND C+A > B) THEN     'Isosceles'
WHEN A<>B<>C AND (A + B > C AND B+C > A AND C+A > B) THEN 'Scalene'
ELSE 'Not A Triangle'

END

FROM TRIANGLES;

Solution 5:[5]

SELECT 
(CASE 
WHEN (A=B AND B=C AND C=A) THEN 'Equilateral'
WHEN (A=B OR A=C OR B=C) AND (A + B > C AND B + C > A AND C + A > B) THEN 'Isosceles'
WHEN (A <> B AND B <>C AND C <> A) AND (A + B > C AND B + C > A AND C + A > B) THEN 'Scalene'
ELSE 'Not A Triangle'
 END)
FROM TRIANGLES;
--THIS WORKS FOR DB2 DATABASES AND SCALENCE CONDITION COULD BE EQUIVALENT TO A<>B<>C

Solution 6:[6]

SELECT  
CASE
    WHEN (A+B > C AND B+C > A AND A+C > B) THEN
    (CASE
        WHEN A = B AND B = C THEN "Equilateral"
        WHEN A = B OR B = C OR A = C THEN "Isosceles"
        WHEN A != B AND B != C AND A != C THEN "Scalene"
    END)
    ELSE "Not A Triangle"
END
FROM TRIANGLES

First, check if the value can form a triangle then check which type it is.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Halil ?brahim
Solution 2 IamSVP
Solution 3 Dakshitha96
Solution 4 Md Shohidul Haque
Solution 5 Syscall
Solution 6 Kiran Chhablani