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.
SELECT CASE
WHEN A + B > C AND A+C>B AND B+C>A THEN CASE WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B OR B = C OR A = C THEN 'Isosceles' WHEN A != B OR B != C OR A != C THEN 'Scalene' END
ELSE 'Not A Triangle' END FROM TRIANGLES;
YOU HAVE TO CHECK FOR ALL COMBINATIONS OF THE CONDITION
select
case
when A+B > C AND A=B AND B=C AND A=C
then 'Equilateral'
when A+B > C AND A=B OR B=C OR A=C
then 'Isosceles'
when A+B > C AND A!=B
then 'Scalene'
else
'Not A Triangle'
End
from TRIANGLES
if "Case1: A + B > C AND A+C>B AND B+C>A" is true then only after that you have to verify "case2: A != B OR B != C OR A != C" which kind of triangle it is. If it is not a valid triangle, case1 will be false and you don't have to worry about the rest cases.
when you use "WHEN A != B OR B != C OR A != C THEN 'Scalene'" why do you use "OR"? shouldn't it be "AND" because ALL 3 sides are not equal? so it will be "WHEN A != B AND B != C THEN 'Scalene'".
Yes you are right AND should be used instead of OR, but even if you use OR then the test case is been submitted..., how is this possible, or there is a fault in the question?
You could also not write the last condition before 'Not A Triangle' to simplify the Query:
SELECT CASE WHEN
A + B > C AND A + C > B AND B + C > A THEN CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene' END
ELSE 'Not A Triangle' END FROM TRIANGLES;
If A = B, it doesn't say anything about C (it is not sure than the triangle is ISOSCELES; in fact, it could be EQUILATERAL if A = C).
Here's my version of the script:
SELECT
CASE
WHEN A + B > C AND A + C > B AND B + C > A THEN
CASE WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B AND A !=C OR B = C AND B!=A OR A = C AND A!=B THEN 'Isosceles'
ELSE 'Scalene'
END ELSE 'Not A Triangle'
END FROM TRIANGLES;
It's not necessary because in that case, if A = B AND B = C, which is the first case, then it's an equilateral triangle. That's why I put that case as the first one so it won't get to the isosceles triangle case.
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?
SELECT
CASE
WHEN A + B > C AND A+C>B AND B+C>A THEN
CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
WHEN A != B OR B != C OR A != C THEN 'Scalene'
END
ELSE 'Not A Triangle'
END FROM TRIANGLES;
Here, there are two case statements are available, inside CASE doesn't have any else statement. First END is for inside CASE and second END is for outside case.
SELECT CASE
WHEN A+B >C AND A+C > B AND B+C > A THEN CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene'
END
ELSE 'Not A Triangle'
END
FROM TRIANGLES
Hi, I don't understand the last bit, it has two END statements. I'm not familiar yet with CASE. Could you please elaborate your thought process here? Thank you!
if sum of any two edges is greater then third edge, we can say it forms a triangle. So there is no need to check B+C>A , A+C>B. Because If A+B>C, it is enough to say they are going to form a triangle.
SELECT CASE WHEN A + B > C AND A+C>B AND B+C>A 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;
Small suggestion:- For Scalene triangle, no two sides must simultaneously be equal. So, A != B and B != C and A != C might be the complete condition.
Type of Triangle
You are viewing a single comment's thread. Return to all comments →
SELECT CASE WHEN A + B > C AND A+C>B AND B+C>A THEN CASE WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B OR B = C OR A = C THEN 'Isosceles' WHEN A != B OR B != C OR A != C THEN 'Scalene' END ELSE 'Not A Triangle' END FROM TRIANGLES;
YOU HAVE TO CHECK FOR ALL COMBINATIONS OF THE CONDITION
A != B OR B != C OR A != C also can be not triangle
(3, 4, 5) is a triangle, but (3, 4, 50) is not.
but this fails in the first condition in the query CASE WHEN A + B > C AND A+C>B AND B+C>A so the query is correct
select case when A+B > C AND A=B AND B=C AND A=C then 'Equilateral' when A+B > C AND A=B OR B=C OR A=C then 'Isosceles' when A+B > C AND A!=B then 'Scalene' else 'Not A Triangle' End from TRIANGLES
TRY THIS.........
IF (A,B,C)= (20,1,1) then foolow your code the result will be "Isosceles'
But in fact this is not a triangle because A< B+C So the point is you forget the condition B+C>A AND A+C>B
But i wonder why the code is accepted in this case
I dont understand your meaning, it says in every sub that A+B>C
if "Case1: A + B > C AND A+C>B AND B+C>A" is true then only after that you have to verify "case2: A != B OR B != C OR A != C" which kind of triangle it is. If it is not a valid triangle, case1 will be false and you don't have to worry about the rest cases.
that is the raason he gave when A+B>c;
no, it is the case of a scalene triangle. but a case where A+B
A!=B AND B!=C I think this would work
check in example it is scalene type
No, It will be a traingle because we hve already checked whether it is a triangle or not
when you use "WHEN A != B OR B != C OR A != C THEN 'Scalene'" why do you use "OR"? shouldn't it be "AND" because ALL 3 sides are not equal? so it will be "WHEN A != B AND B != C THEN 'Scalene'".
WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B OR B = C OR A = C THEN 'Isosceles' ELSE 'Scalene'
Why does not A = B = C work for Equilateral? Whats the difference between A = B and B = C?
when A=B=C execute then if A=B then it return boolean value if true then 1 else 0 then it compare 1=c
e.g. (10,10,10)
when execute A=B=C then (10=10)=10 take as (10=10) return 1 because its true then in next stape it compare 1=10 in next step and return false
This explanation is not true. counter example: ((A=B)=(B=C)) doesnt work. step 1: ((A=B)=(B=C)) -> (1) = (B=C) step 2: ((1) = (B=C)) -> (1) = (1) step 3: (1) = (1) -> 1 But doesnt work
this is simple math logic if A=B and B=C it means A=C....
operator overlosding dont work here
Yes you are right AND should be used instead of OR, but even if you use OR then the test case is been submitted..., how is this possible, or there is a fault in the question?
You could also not write the last condition before 'Not A Triangle' to simplify the Query:
SELECT CASE WHEN A + B > C AND A + C > B AND B + C > A THEN CASE WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B OR B = C OR A = C THEN 'Isosceles' ELSE 'Scalene' END ELSE 'Not A Triangle' END FROM TRIANGLES;
If A = B, it doesn't say anything about C (it is not sure than the triangle is ISOSCELES; in fact, it could be EQUILATERAL if A = C). Here's my version of the script: SELECT CASE WHEN A + B > C AND A + C > B AND B + C > A THEN CASE WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B AND A !=C OR B = C AND B!=A OR A = C AND A!=B THEN 'Isosceles' ELSE 'Scalene' END ELSE 'Not A Triangle' END FROM TRIANGLES;
Hope you enjoy it, fellas!!
FOR ISOSCELES shouldn't it be A=B and B!=C and so on ? Otherwise iscosceles and equailateral might overlap
It's not necessary because in that case, if A = B AND B = C, which is the first case, then it's an equilateral triangle. That's why I put that case as the first one so it won't get to the isosceles triangle case.
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;
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"
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.
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
whay have you written CASE twice :)
For Scalene Triangle shouldn't it be like this?
If A!=B OR B!=C OR A!=C then for A!=B, B=C or A=C can happen.
However, hackerrank is accepting both answers.
How is
WHEN A != B OR B != C OR A != C THEN 'Scalene' possible in 20,20,21 ? You should use AND condition right?
YES you are correct AND should be used, but if you use OR the test is getting submitted I dont know how?
can you please tell me, why not just
ELSE 'Not A Triangle'
But,
END ELSE 'Not A Triangle' END
SELECT CASE WHEN A + B > C AND A+C>B AND B+C>A THEN CASE WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B OR B = C OR A = C THEN 'Isosceles' WHEN A != B OR B != C OR A != C THEN 'Scalene' END ELSE 'Not A Triangle' END FROM TRIANGLES;
Here, there are two case statements are available, inside CASE doesn't have any else statement. First END is for inside CASE and second END is for outside case.
or may be
Hi, I don't understand the last bit, it has two END statements. I'm not familiar yet with CASE. Could you please elaborate your thought process here? Thank you!
if sum of any two edges is greater then third edge, we can say it forms a triangle. So there is no need to check B+C>A , A+C>B. Because If A+B>C, it is enough to say they are going to form a triangle.
should be WHEN A != B and B != C and A != C THEN 'Scalene'
SELECT CASE WHEN A + B > C AND A+C>B AND B+C>A 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;
Small suggestion:- For Scalene triangle, no two sides must simultaneously be equal. So, A != B and B != C and A != C might be the complete condition.
Why have you used 'OR' to find Scalene triangle, I think 'AND' should be used there.