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:

Hi, could anyone please point out the mistake in my query wriiten below. I am not able to figure out why it says wrong answer!

select case when A+B <=C OR B+C <=A OR C+A <=B then 'not a triangle'
when A=B and B=C and C=A then 'Equilateral triangle'
when A=B or B=C or C=A then 'Isosceles triangle'
else 'Scalene triangle'
end
from TRIANGLES;

The sql query conditions are correct.The statements in the then of sql case should be modified.Change 'Equilateral triangle' to 'Equilateral' and do other changes as below.

select
case
when A+B <=C OR B+C <=A OR C+A <=B then 'Not A Triangle'
when A=B and B=C and C=A then 'Equilateral'
when A=B or B=C or C=A then 'Isosceles'
else 'Scalene'
end
from TRIANGLES;

end is the ending of case statement started at the start of query and from triangle is the part of 'select-from-where' clause in which where is replaced by triangles which is the name of our table/relation in which three sides of triangles are stored.

select case when a+b<=c and b+c<=a and a+c<=b then 'not a triangle'
when a=b and b=c then 'equilateral'
when a=b or b=c or c=a then 'isosceles'
else 'scalene'
end
from triangles

i wrote this code but then why is it showing error for mysql

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;

Hi, Could you please tell me what's the difference between your code and the code below?

SELECT CASE WHEN A + B <= C OR B + C <= A OR C + A <= B THEN 'Not a Triangle'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
WHEN A = B AND B = C And C = A THEN 'Equilateral'
ELSE 'Scalene'
END
FROM TRIANGLES

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

Edit: Found out it was because the 'a' in 'Not a Triangle' wasn't capitalized. That's more nit-picky than the sql language itself!

I have the exact same thing, good job. Only difference is that I would get an error if I didn't run parenthesis in the conditions like (a+b) <= c. Adding paretheses gave me the right answer....

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 A!=C and B!=C then 'Scalene'
else 'Isosceles'
end
from TRIANGLES

I came up with nearly the same exact code. However I am receiving errors. I am getting "isoceles" returned instead of "Not a triangle" for one of the test cases.

SELECT
CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR A = C OR B = C THEN 'Isosceles'
WHEN A + B <= C OR A + C <= B OR B + C <= A THEN 'Not A Triangle'
ELSE 'Scalene'
END
FROM TRIANGLES;

Hi Antoniro, I wrote exact the same answer as yours. And I compared the each result in my output with the expected one. Everything is correct but my answer is wrong. The only difference is I have a 16th line which is an empty line in my output. Do you have any idea for this?

You can get away with not adding A + C <= B OR B + C <= A because these statements are implied in A+B<=C.

It is very funny how the checker really does not like any "spelling mistake".

SELECT CASE WHEN A + B<=C 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;

## Type of Triangle

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

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.

Hi, could anyone please point out the mistake in my query wriiten below. I am not able to figure out why it says wrong answer!

select case when A+B <=C OR B+C <=A OR C+A <=B then 'not a triangle' when A=B and B=C and C=A then 'Equilateral triangle' when A=B or B=C or C=A then 'Isosceles triangle' else 'Scalene triangle' end from TRIANGLES;

Thank you for replying. Although, I did try that too. It did not work.

The sql query conditions are correct.The statements in the then of sql case should be modified.Change 'Equilateral triangle' to 'Equilateral' and do other changes as below.

select case when A+B <=C OR B+C <=A OR C+A <=B then 'Not A Triangle' when A=B and B=C and C=A then 'Equilateral' when A=B or B=C or C=A then 'Isosceles' else 'Scalene' end from TRIANGLES;

case when (a=b and b!=c and a!=c ) or (b=c and a!=b and c!=a) or (a=c and b!=c and b!=c) then 'Isosceles'

Your Query is Correct but the output should match with question

'not a triangle' -> "Not A Triangle" 'Equilateral triangle' -> "Equilateral" 'Isosceles triangle' -> "Isosceles" 'Scalene triangle' -> "Scalene"

case when (a=b and b!=c and a!=c ) or (b=c and a!=b and c!=a) or (a=c and b!=c and b!=c) then 'Isosceles'

tweak the isosceles part to this :

case when (a=b and b!=c and a!=c ) or (b=c and a!=b and c!=a) or (a=c and b!=c and b!=c) then 'Isosceles'

Why write end from triangles?

end is the ending of case statement started at the start of query and from triangle is the part of 'select-from-where' clause in which where is replaced by triangles which is the name of our table/relation in which three sides of triangles are stored.

why can't use an if statement here instead if case?

select case when a+b<=c and b+c<=a and a+c<=b then 'not a triangle' when a=b and b=c then 'equilateral' when a=b or b=c or c=a then 'isosceles' else 'scalene' end from triangles

i wrote this code but then why is it showing error for mysql

1) Compare casing of letters in printed strings with those of expected output 2) The 'Not A Triangle' conditions appear to be incorrect

did you get the answer for this ?

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;

THIS WILL WORK NOW IN MYSQL!!!!

It does because the loop will iterate without checking condition

Hi, Could you please tell me what's the difference between your code and the code below?

SELECT CASE WHEN A + B <= C OR B + C <= A OR C + A <= B THEN 'Not a Triangle' WHEN A = B OR B = C OR A = C THEN 'Isosceles' WHEN A = B AND B = C And C = A THEN 'Equilateral' ELSE 'Scalene' END FROM TRIANGLES

First, you should check case of Equilateral before case of Isosceles. Second, this is very stupid that you must use "Not A Triangle"

why WHEN A + B <= C OR A + C <= B OR B + C <= A THEN 'Not A Triangle should be the first one?

I have exactly this and it still says it's wrong.

end FROM Triangles

Edit: Found out it was because the 'a' in 'Not a Triangle' wasn't capitalized. That's more nit-picky than the sql language itself!

I was facing same issue, this helped. Thanks for a good catch.

I have the exact same thing, good job. Only difference is that I would get an error if I didn't run parenthesis in the conditions like (a+b) <= c. Adding paretheses gave me the right answer....

WHEN A = B OR A = C OR B = C THEN 'Isosceles'

I don't understand this part

In case A=B, C can equal A and this would make it an equilateral, shouldn't you say and C!= A??

thanks a lot

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 A!=C and B!=C then 'Scalene' else 'Isosceles' end from TRIANGLES

THANK YOU! easy to read

Why doesn't it work for MySQL?

Okay, it works for that too. Thank You

I came up with nearly the same exact code. However I am receiving errors. I am getting "isoceles" returned instead of "Not a triangle" for one of the test cases.

SELECT CASE WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B OR A = C OR B = C THEN 'Isosceles' WHEN A + B <= C OR A + C <= B OR B + C <= A THEN 'Not A Triangle' ELSE 'Scalene' END FROM TRIANGLES;

HELP??

Hi Antoniro, I wrote exact the same answer as yours. And I compared the each result in my output with the expected one. Everything is correct but my answer is wrong. The only difference is I have a 16th line which is an empty line in my output. Do you have any idea for this?

Thanks, Alice

You can get away with not adding A + C <= B OR B + C <= A because these statements are implied in A+B<=C.

It is very funny how the checker really does not like any "spelling mistake".

SELECT CASE WHEN A + B<=C 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;