Problem statement:
Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the followin
Give it a try. That should definitely work.
SELECT
CASE
WHEN A + B <= C OR A + C <= B OR B + C <= A THEN 'Not A Triangle'
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR A = C OR B = C THEN 'Isosceles'
ELSE 'Scalene'
END
FROM TRIANGLES;
For MySQL:
select
case
when (a+b>c) and (a+c>b) and (b+c>a) then
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
else 'Not A Triangle'
end
from TRIANGLES
SELECT CASE
WHEN (A + B <= C) OR (B+C <= A) OR (A+C <= B) THEN "Not A Triangle"
WHEN (A=B) AND (B=C) THEN "Equilateral"
WHEN (A=B) OR (C=A) OR (B=C) THEN "Isosceles"
ELSE "Scalene"
END
FROM TRIANGLES
MySQL:
select
case
when ((A+B)<=C) || ((C+B)<=A) || ((A+C)<=B) then 'Not A Triangle'
when A=B && B=C && A=C then 'Equilateral'
when ((A=B) && (A+B)>C) || ((B=C) && (B+C)>A) then 'Isosceles'
when A!=B && B!=C && A!=C then 'Scalene'
end
from TRIANGLES;
Explanation:
First, check whether the values satisfies the requirement of a triangle(i.e sum of two sides must be greater than the other side) - Not A Triangle
Then, look for the type of the triangle
select case when a=b and b=c then 'Equilateral' when a+b <=c or b+c<=a or a+c
/* THE BELOW CODE WILL CHECK ALL THE POSSIBLE CASES OF A TRIANGLE, THIS CODE WILL WORK 100% */
select case
when A+B<=C OR A+C<=B OR C+B<=A THEN "Not A Triangle"
when (A<>B AND A+B>C) AND (B<>C AND B+C>A) AND (A<>C AND A+C>B) THEN
"Scalene"
when (A=B AND B<>C AND A+B>C) OR (B=C AND B<>A AND B+C>A) OR (A=C AND C<>B
AND A+C>B) THEN "Isosceles"
when A=B AND B=C THEN "Equilateral"
end as triangle
from TRIANGLES;