- Practice
- SQL
- Advanced Select
- Type of Triangle
- Discussions

# Type of Triangle

# Type of Triangle

madshov + 28 comments Checking for the 'Not A Triangle' case should use <= as comparator, even though it is stated in the description that A+B should be strictly less than C.

OMendozaAsked to answer + 1 comment You are right. The statement is not fully accurate.

dvsakgec + 4 comments You are right. I finally solved it but issue with the problem statement is what they say in problem is correct 'triangle inequality' whereas what they are asking for is incorrect. Equality exists for triangle with area zero. So still it is a triangle but zero area (Can be argued otherwise also) but problem statement is wrong.

**Would like to correct my statement that triangle with area=0 is still a triangle is incorrect.**dvsakgec + 0 comments Also thanks for the tip, I was wondering what am I doing wrong.

ge_anand + 0 comments You are so right too. Brilliant thought. I also realized that Zero area is only true for the equality condition (Ex. A+B=C) but not for the less than case (A+B

piedeer + 0 comments same, I was losing it over where I am going wrong!

vvojtisek + 1 comment I would disagree as triangle is defined by 3 points on the plane that are not on the same line. Your area "zero" triagle is not a triagle as all 3 point are in line which violates definition of the triangle.

ranadpsingh99 + 0 comments yes offcourse sum of any two sides must be greater than the third one.

organizer + 1 comment [deleted]rpravinkumar25 + 8 comments SELECT CASE

WHEN A + B > C 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;subhajitdhr + 16 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

bourax + 7 comments A != B OR B != C OR A != C also can be not triangle

mega_mind + 2 comments (3, 4, 5) is a triangle, but (3, 4, 50) is not.

sreeharsha35 + 0 comments 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

yadavnilesh520 + 1 comment 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.........

nguyenngoctram_1 + 1 comment 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

taojiang2018she1 + 0 comments I dont understand your meaning, it says in every sub that A+B>C

Sumit_Suman + 0 comments 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.

reddysethuvardh1 + 0 comments that is the raason he gave when A+B>c;

raajsinha7797 + 0 comments no, it is the case of a scalene triangle. but a case where A+B

vishnujangid612 + 1 comment A!=B AND B!=C I think this would work

ketakrs + 0 comments [deleted]

appy_praveen + 0 comments check in example it is scalene type

trehansalil1 + 0 comments No, It will be a traingle because we hve already checked whether it is a triangle or not

yisilala + 2 comments 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'".

alxcervantes_uk + 1 comment WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B OR B = C OR A = C THEN 'Isosceles' ELSE 'Scalene'

sebastian17 + 3 comments Why does not A = B = C work for Equilateral? Whats the difference between A = B and B = C?

Adityamohan + 1 comment 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

peotavio + 0 comments 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

VDCHBD + 0 comments this is simple math logic if A=B and B=C it means A=C....

ritendra_28 + 0 comments operator overlosding dont work here

harshshah77 + 0 comments 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?

chhay_seng_sim_1 + 1 comment 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;

diegomsaiz + 0 comments 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!!

pugaliaavichal + 1 comment FOR ISOSCELES shouldn't it be A=B and B!=C and so on ? Otherwise iscosceles and equailateral might overlap

chhay_seng_sim_1 + 0 comments 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.

antoniro + 14 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:

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 is for MS SQL Server.

little_mushroom + 0 comments [deleted]LearnCrazy + 4 comments 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;

frozendamn + 1 comment [deleted]LearnCrazy + 1 comment Thank you for replying. Although, I did try that too. It did not work.

frozendamn + 0 comments [deleted]

shankarpentyala + 0 comments 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;

robin_csil + 0 comments [deleted]symbol2_286 + 0 comments 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"

ishabandi + 1 comment Why write end from triangles?

yashrajarora0 + 0 comments 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.

jasmine_kaur + 3 comments 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

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

kaneriya_bhouti1 + 1 comment did you get the answer for this ?

kaneriya_bhouti1 + 0 comments 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!!!!

prativadis56 + 0 comments It does because the loop will iterate without checking condition

beingpallavi + 1 comment 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

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

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

cburas2311 + 1 comment I have exactly this and it still says it's wrong.

`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!

ankitapatidar99 + 0 comments I was facing same issue, this helped. Thanks for a good catch.

nmcastro1 + 0 comments 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....

lobna_albehery + 0 comments 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??

dgknca + 0 comments thanks a lot

Jukgadu + 0 comments 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

austindholmes + 0 comments 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??

wellingbei + 0 comments 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

RatFire + 0 comments whay have you written CASE twice :)

saundaryat + 0 comments [deleted]raadu + 0 comments For Scalene Triangle shouldn't it be like this?

WHEN A!=B AND B!=C THEN 'Scalene'

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.

soumyarocks1234 + 1 comment How is

WHEN A != B OR B != C OR A != C THEN 'Scalene' possible in 20,20,21 ? You should use AND condition right?harshshah77 + 0 comments YES you are correct AND should be used, but if you use OR the test is getting submitted I dont know how?

spyely + 0 comments [deleted]naveen_neevan + 1 comment can you please tell me, why not just

**ELSE 'Not A Triangle'**But,

**END ELSE 'Not A Triangle' END**sharan_theja + 0 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;

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.

ketakrs + 1 comment or may be

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

joaopacatarino + 0 comments 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!

nomanibrahim7 + 0 comments 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.

rakeshkundu + 0 comments should be WHEN A != B and B != C and A != C THEN 'Scalene'

jsrmaditya96 + 0 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 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.

dbhai982 + 0 comments Why have you used 'OR' to find Scalene triangle, I think 'AND' should be used there.

kalyanpinni + 1 comment will not work for cases like 12 10 1 <- This will produce output as scalene byt it should not be a triangle.

suraj110693 + 0 comments SELECT * FROM ( 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 AND C <> A) OR (A <> B AND B = C ) OR (A = C AND B <> C ) THEN 'Isosceles' WHEN (A <> B AND B <> C AND C <> A) THEN 'Scalene' END ) AS TRIANGLE_TYPE FROM TRIANGLES )Z;

mailme_saquibali + 0 comments SELECT CASE WHEN A+B>C THEN CASE WHEN A=B AND B=C Then 'Equilateral' When A=B OR A=C OR B=C THEN 'Isosceles' When A!=B OR B!=C OR C!=A Then 'Scalene' END ELSE 'Not a Triangle' END FROM Triangles;

I wrote this, compared the answer but the compiler claims a fault in it.

gurvinder8 + 0 comments why we are using** A + B > C**

GuruKarthik + 0 comments i can't able to understand why you mentioning

**"when A+B>C"**at startiting can you explain it why?Meenal113 + 0 comments Thanks a lot.

tjtrupti + 0 comments We do not require a condition to check for Scalene, we can directly write ELSE instead of the condition.

trehansalil1 + 0 comments SELECT CASE WHEN ((A + C > B) and (A + B >C) and (C + B >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;

This is a better solution pravin!

sp1dey + 0 comments Thank you!

abeaucha + 0 comments This got me too

abhishek_t01 + 2 comments Is it guaranteed that C will always be the greatest side?

sarussell2012 + 0 comments I do not think so. The table is small so you can look at its contents yourself.

mega_mind + 1 comment you'll have to check for all the 3 conditions as in (A+B>C AND B+C>A AND C+A>B)

roopaligupta0081 + 0 comments not AND but OR

Abdul501 + 0 comments Thank you. .got it cheers

shubham_goel + 0 comments Thanks

bhars86 + 0 comments Thank you! This was the missing test case in my solution.

chauhan_heta + 1 comment Thank You!

I_Infinite + 0 comments [deleted]

AllisonP + 5 comments It's assumed that anyone attempting this challenge understands basic Geometry. Any rules will be stated in the Problem Statement at the top of the page. The Explanation section is not stating a rule, it's stating an inequality relevant to the respective A, B, and C values for that Sample Case. The reason why those values do not form a triangle is because 27 is a number that is less than 30. There is a difference between the two numbers. If A and B added up to 30, then the Explanation would say that A + B = C because that would be the immediate reason causing it to not be a triangle.

davidmstammers + 1 comment And yet they are stated like rules in the description... I too was stuck as were all these other people. Perhaps you could be a little less stubborn and fix a clearly misleading description.

rodbourn + 0 comments I'll just leave this here

giaym + 0 comments it's technically only assumed they know sql :P, unless you mean it is word of god? in that case it is whatever you say it is I guess

pmatakevich + 0 comments "It's assumed that anyone attempting this challenge understands basic Geometry".

- Condescending remark, from a stubborn Mod. Learn to take criticism and improve.

rodbourn + 0 comments [deleted]shapeshifter42 + 0 comments I've come here to learn/revise sql, not learn/revise geometry. I think it's odd that the rules at the top cover the basic triangle types that most of us would remember from school, but not the triangle inequality stuff that fewer of us would recall, yet which is only alluded to by example. I didn't know about the latter at all (or had clean forgotten) and just added an 'else' for it.

rajakash050 + 0 comments Yes , thanks for the hint, admin shoud provide proper info.

I_Infinite + 0 comments Thanks Dude

hemanth_bht + 0 comments Thank You

rasaab1029 + 0 comments The case A + B = C corresponds to a Triangle of zero area (zero angles). The technical definition will sometimes allow this, but this problem does not. So, based on the result, the problem is actually stated correctly. The problem is we don't know which axioms she is using.

PranayNeema + 1 comment select if((A + B > C),if(( A=B and B=C),'Equilateral', if(( A=B or A=C or B=C),'Isosceles',if((A!=B and B!=C),'Scalene','Not A Triangle'))),'Not A Triangle') as types from TRIANGLES;

dadaza770 + 0 comments My solution:

SELECT IF((A+ B) > C and (A + C)> B and (B + C) > A , IF(A = B and A = C and C=B,'Equilateral', IF(A=B OR B = C OR A = C,'Isosceles','Scalene')) ,'Not A Triangle') TRIANGLES FROM TRIANGLES

rigoczkitamas + 4 comments wasting 10 minutes to figure out what's wrong; typed "a" instead of "A" at Not A Triangle :D

GabeM + 0 comments Thank you! Makes sense now becuase it was expecting that output

sean_hoar + 0 comments This was the missing piece of the puzzle for me also! I've been spending at least an hour trying to figure out why it wasn't testing correctly. This was it.

mjweidenhamer + 0 comments Yeah, the check for a correct solution clearly needs to be fixed to accept 'Not a Triangle', since the word 'a' is never capitalized unless it's the first word in a sentence.

chugh_bhavya + 0 comments Thanks! I did the same mistake.

Ashish167 + 0 comments Can you post your solution for this because I am not getting exactly where I am getting wrong.

**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;**shailendra_bind1 + 0 comments thanks for comment after use of <= as comparator i pass the test

select CHOOSE(iif(A+B<=C,4,iif(A=B And A=C,2,iif(A=B OR A=C,1,3))),'Isosceles','Equilateral','Scalene','Not A Triangle') from triangles

manojattal + 0 comments Thanks for the tip!

olegraev + 0 comments It's an error. When A+B=C - It's a line segment, but not a triangle..

maxiiminu + 0 comments The full condition for a trinagle exist is: | b - c | < a < b + c | a - c | < b < a + c | a - b | < c < a + b If you negate this condition, the row is not a triangle.

jorgesanure + 0 comments I was getting crazy wondering why my query was not passing the test cases.

Your tip save my time.

i had to change this code:

/* Enter your query here. */ SELECT IF( A = B AND A = C, 'Equilateral', IF( A = B OR A = C OR B = C, 'Isosceles', IF( A+B < C OR A+C < B OR B+C < A, 'Not A Triangle', 'Scalene' ) ) ) FROM TRIANGLES

To this other:

/* Enter your query here. */ SELECT IF( A+B <= C, 'Not A Triangle', IF( A = B AND A = C, 'Equilateral', IF( A = B OR A = C OR B = C, 'Isosceles', 'Scalene' ) ) ) FROM TRIANGLES

to get pass the test cases

vash47 + 0 comments thanks b0ss

yanyantarong + 0 comments I literally just translated the condition in the code - "not larger" so I used !>, and it worked the first time.

krohit2375 + 0 comments SELECT CASE WHEN A + B > C AND A + C > B AND B + C > A THEN CASE WHEN A = B AND B = C AND C = A THEN 'Equilateral' WHEN A = B OR B = C OR C = A THEN 'Isosceles' WHEN A != B AND B != C AND C != A THEN 'Scalene' ELSE 'Not A Triangle' END ELSE 'Not A Triangle'END FROM TRIANGLES;

john_matt_barbo1 + 0 comments The solution below would still pass with A=100, B=1, and C=1, which isnt correct.

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

jrreps + 0 comments Saved me from pulling my hair out! that was ugh.... a waste of additional 30mins hunting for a non-issue.

vitamin + 13 comments My solution

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' ELSE 'Scalene' END ELSE 'Not A Triangle' END FROM TRIANGLES;

giriamrita92 + 0 comments cool..!

J_HarryFong87 + 0 comments Thanks, great code.

clogos + 0 comments Thanks!

raghavb50 + 0 comments Thanks, very helpful and elegant solution. Nice formatting.

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

arvindmohanraj + 0 comments Great!!!...

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

nyartsgnaw + 0 comments [deleted]wattersmt + 0 comments Thank you. I always learn more from reading the comments than the actual problem.

antoniro + 1 comment 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 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 is for MS SQL Server.

appunamiya + 0 comments 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?

codegritty + 1 comment Hello, thank you for sharing. Do you mind explaning this part...

WHEN A + B > C AND B + C > A AND A + C > B THEN

Thank you in advance!

akylus + 0 comments 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

MykKhaz + 0 comments Cool and readable! Thank you!

nithish642k + 0 comments neat!

Unremarkable + 3 comments SELECT CASE WHEN 2 * GREATEST(A, B, C) >= (A + B + C) THEN "Not A Triangle" WHEN A = B AND A = C THEN "Equilateral" WHEN A = B OR A = C OR B = C THEN "Isosceles" ELSE "Scalene" END FROM TRIANGLES

elliott_j_harri1 + 1 comment I had very similar code but I put the 'Iscoceles' case statment before the 'Not a Triangle' one and it caused an error. The order in which you check the statments can really reduce the logic in your code :)

SELECT CASE WHEN C >= A + B 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 AS 'Triangles' FROM TRIANGLES;

antoniro + 1 comment Exactly! I have been saying that in few 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:

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

I think this is the cleanest and shortest answer...

veerankisatya80 + 0 comments When the Isosceles and Equilateral case statements order are reversed, that is the Equi is checked after Isos then the test case fails. Is it a necessaity to do this in this ordeR?

codegritty + 1 comment Hello, can you please explaing this...

WHEN 2 * GREATEST(A, B, C) >= (A + B + C)

Thank you in advance!

antoniro + 2 comments The general rule/condition for a valid triangle is that the longest side should be less than the sum of the two other sides:

`longestSide < sumOfOtherSides`

(1)For example, if you have A, B, and C, where B is the longest side, then the condition

`B < (A+C)`

is true for a valid triangle, and false otherwise.Now, the sum of all the sides of the triangle is

`(A+B+C)`

. Given B is the longest side, if you want to get the sum of other sides, you have to remove B from the sum of all sides, thus`(A+C) = (A+B+C) - (B)`

or in other words:`sumOfOtherSides = sumOfAllSides - longestSide`

(2)With this in mind, the first inequation (1) can now be written as :

`longestSide < sumOfAllSides - longestSide`

which is equivalent to

`longestSide + longestSide < sumOfAllSides`

or

`2 * longestSide < sumOfAllSides`

(3)So, a triangle is valid if and only if inequation (3) is true. If (3) is false, then we have

`2 * longestSide >= sumOfAllSides`

. In MySQL and Oracle SQL, the`GREATEST()`

function returns the greatest value of the guiven arguments. Hence, you have`longestSide = GREATEST(A, B, C)`

.Everything above put together, the final formula to check if a triangle is NOT valid is :

`2 * GREATEST(A, B, C) >= (A + B + C)`

Hope my explanation was clear and detailed!

pragya_maheshwa1 + 0 comments [deleted]MAbdElRaouf + 0 comments Thank you for sharing this information.

marinskiy + 1 comment Here is Oracle solution from my HackerrankPractice repository:

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

Feel free to ask if you have any questions :)

stasisOo + 0 comments that's one sexy query not gonna lie

soryk + 0 comments SELECT CASE WHEN A+B>C AND B+C>A AND C+A>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 C<>A THEN "Scalene" END ELSE "Not A Triangle" END FROM TRIANGLES

rxrxr + 6 comments This is a poorly written problem. Two mistakes that has gotten others: 1) the table name is triangles not triangle. 2) It should read 'Not a Triangle' (with lower case 'a').

FShieldheartAsked to answer + 2 comments **Endorsed by rxrxr**Agreed. Though, semantic quality of the problem does not diminish the technical quality.

jianrushi + 0 comments The author changed the 'Not a Traingle' to 'Not A Triangle ' as described in the question, but the table is still named triangles

AvmnuSngHackerRank AdminChallenge Author + 1 comment The typos are corrected. The only correction is table name, it is

*Triangles*not*Triangle*. The output*Not A Triangle*is considered as correct now.chaseraz + 0 comments It was correct as it was before. Articles (a, an, etc.) should not be capitalized in a title.

chaseraz + 0 comments [deleted]jpujari + 0 comments that lowercase 'a' got me

divya_taurus + 0 comments one problem is that it does not account for "some of 2 sides = 3rd side" condition. in that case also it will not be a triangle, but a straight line. I would agree with others that semantic quality of the problem does not diminish the technical quality.

byron_xu + 1 comment The following code works very well. it contains two nested cases. outer case is about a, b, c forms a triangle whether or not. the inner case is about what a types of triangle is it.

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;

rochelly_fernan1 + 0 comments Thanks for helping me !!! ;)

Envee_NV + 1 comment Oh God!!! I was using "Not a triangle". Changing it to "Not A Triangle" worked. Never thought case sensitive that too a statement would make a difference.

haardiksharma + 0 comments Ahhh

julianacaetana + 2 comments Help? MS SQL Server!

SELECT A,B,C, CASE WHEN ((A = B) AND (B <> C) OR (B = C) AND (A <> B) OR (A = C) AND (A <> B)) THEN 'Isosceles' WHEN ((A = B) AND (B = C) AND (A = C)) THEN 'Equilateral' WHEN ((A <> B) AND (B <> C) AND (A <> C) AND NOT ((A + B) <= C) OR ((A + C) <= B) OR ((C + B) <= A)) THEN 'Scalene' WHEN (((A + B) <= C) OR ((A + C) <= B) OR ((C + B) <= A)) THEN 'Not A Triangle' END FROM TRIANGLES ORDER BY 1,2,3;

zakialam838 + 5 comments select

case when a+b<=c or b+c<=a or a+c<=b then 'Not A Triangle' when a=b and b=c and c=a then 'Equilateral' when a=b or b=c or a=c then 'Isosceles'else 'Scalene' end from TRIANGLES ;

first check for not a triangle condition, it worked for me...

ChChNz + 0 comments Thank you so much, but for Equilateral, you only need to write 2 conditions, like a=b and a=c would be enough.

lkrueger + 0 comments that was my problem, I needed to check for "Not A Triangle" condition first

getgimphed + 0 comments [deleted]getgimphed + 1 comment SELECT "Isosceles" from TRIANGLES where ( ((A = B) or (A = C) or (C = B) ) and not ( A = B and A = C ) and not (( (A + B) < C ) or ( (C + B) < A ) or ( (C + A) < B )) );

SELECT "Equilateral" from TRIANGLES where ( A = B and A = C );

SELECT "Scalene" from TRIANGLES where ( ((A <> B) and (A <> C) and ( B <> C )) and not (( (A + B) < C ) or ( (C + B) < A ) or ( (C + A) < B )) );

SELECT "Not A Triangle" from TRIANGLES where (( (A + B) < C ) or ( (C + B) < A ) or ( (C + A) < B ));

Why does it says wrong answer ??

or this

CASE WHEN (((A = B) or (A = C) or (C = B) ) and not ( A = B and A = C ) and not (( (A + B) < C ) or ( (C + B) < A ) or ( (C + A) < B ))) THEN SELECT "Isosceles"; WHEN ( A = B and A = C ) THEN SELECT "Isosceles"; WHEN (((A <> B) and (A <> C) and ( B <> C )) and not (( (A + B) < C ) or ( (C + B) < A ) or ( (C + A) < B ))) THEN SELECT "Scalene"; WHEN (( (A + B) < C ) or ( (C + B) < A ) or ( (C + A) < B )) THEN SELECT "Not A Triangle"; END CASE

lewiscowles + 0 comments I think you need to read about UNION for the first, the second the CASE should live inside the SELECT not the other way around. I've never seen it the other way around

mailme2jithin + 1 comment case when a+b-c=c then 'equilateral'---what is wrong in this logic.. plz help

sankirna1292 + 0 comments 5+5-5 = 5 --> Ok 5+7-6=6 ----> Fail

induma_kas + 1 comment Do Output only the triangle names

Donts 1.Remove column values 2.Remove order by 3.Only Equilateral, Scalene, Isocelese and Not A Triangle are expected outputs.. So 20,20,40 should be reported a 'Not A Triangle'. (This is confusing to me also)

Try it ..Cheers...

Callipygean + 1 comment I tried this, which worked when I set the problem up on my own MySQL but failed the code test. Anyone know why?

select case when A>=B+C or B>A+C or C>=A+B then 'Not a triangle' when A=B and B=C then 'Equilateral' when A=B and B != C then 'Isosceles' when A!=B and B!=C and A!=C then 'Scalene' end as Type from triangles;

Callipygean + 0 comments Sorry, there was a logical error there, which I found by adding more examples. This now seems to work fine though, but it still doesn't like it:

select case when A>=B+C or B>A+C or C>=A+B then 'Not a triangle' when A=B and B=C then 'Equilateral' when (A=B and B!=C) or (A=C and C!=B) or (B=C and C!=A) then 'Isosceles' when A!=B and B!=C and A!=C then 'Scalene' else 'Balls Up' end as Type from triangles;

Sort 832 Discussions, By:

Please Login in order to post a comment