We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

You don't need to have multiple CASE statements next to each other or nested CASEs. If you check the conditions in the right order, only one CASE statement with multiple WHENs will do the job:

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 AND B <> C AND A <> C) OR (B = C AND A <> B AND A <> C ) OR ( A = C AND A <> B AND B <> C ) THEN 'Isoceles'
ELSE 'Scalene'
end
from TRIANGLES;

## Type of Triangle

You are viewing a single comment's thread. Return to all comments →

My solution

cool..!

Thanks, great code.

Thanks!

Thanks, very helpful and elegant solution. Nice formatting.

it worked i wrote a silly mistake regarding the condition of trinangle and got puzzled thanku buddy

Great!!!...

Many thanks for this solution! I'm new to CASE in SQL, and this nicely formatted approach helped me understand it.

Thank you. I always learn more from reading the comments than the actual problem.

You don't need to have multiple CASE statements next to each other or nested CASEs. If you check the conditions in the right order, only one CASE statement with multiple WHENs will do the job:

This is for MS SQL Server.

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 AND B <> C AND A <> C) OR (B = C AND A <> B AND A <> C ) OR ( A = C AND A <> B AND B <> C ) THEN 'Isoceles'

ELSE 'Scalene'

end from TRIANGLES;

why doesnt this give the right answer?

Hello, thank you for sharing. Do you mind explaning this part...

Thank you in advance!

Sum of 2 sides of a triangle should always be greater than the 3rd side. So we are checking the three possiblities for 3 pairs of sides

Cool and readable! Thank you!