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.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. SQL
  3. Advanced Select
  4. Type of Triangle
  5. Discussions

Type of Triangle

  • Problem
  • Submissions
  • Leaderboard
  • Discussions

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

  • subhajitdhr 3 years ago+ 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

    91|
    ParentPermalink
    • bourax 3 years ago+ 7 comments

      A != B OR B != C OR A != C also can be not triangle

      -23|
      ParentPermalink
      • mega_mind 3 years ago+ 2 comments

        (3, 4, 5) is a triangle, but (3, 4, 50) is not.

        5|
        ParentPermalink
        • sreeharsha35 2 years ago+ 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

          4|
          ParentPermalink
        • yadavnilesh520 2 years ago+ 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.........

          -1|
          ParentPermalink
          • nguyenngoctram_1 1 year ago+ 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

            1|
            ParentPermalink
            • taojiang2018she1 9 months ago+ 0 comments

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

              0|
              ParentPermalink
      • Sumit_Suman 3 years ago+ 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.

        0|
        ParentPermalink
      • reddysethuvardh1 2 years ago+ 0 comments

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

        0|
        ParentPermalink
      • raajsinha7797 2 years ago+ 0 comments

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

        0|
        ParentPermalink
      • vishnujangid612 12 months ago+ 1 comment

        A!=B AND B!=C I think this would work

        0|
        ParentPermalink
        • ketakrs 6 months ago+ 0 comments
          [deleted]
          0|
          ParentPermalink
      • appy_praveen 7 months ago+ 0 comments

        check in example it is scalene type

        0|
        ParentPermalink
      • trehansalil1 3 months ago+ 0 comments

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

        0|
        ParentPermalink
    • yisilala 3 years ago+ 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'".

      11|
      ParentPermalink
      • alxcervantes_uk 3 years ago+ 1 comment

        WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B OR B = C OR A = C THEN 'Isosceles' ELSE 'Scalene'

        0|
        ParentPermalink
        • sebastian17 3 years ago+ 3 comments

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

          0|
          ParentPermalink
          • Adityamohan 3 years ago+ 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

            3|
            ParentPermalink
            • peotavio 3 years ago+ 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

              0|
              ParentPermalink
          • VDCHBD 2 years ago+ 0 comments

            this is simple math logic if A=B and B=C it means A=C....

            -1|
            ParentPermalink
          • ritendra_28 2 years ago+ 0 comments

            operator overlosding dont work here

            0|
            ParentPermalink
      • harshshah77 2 years ago+ 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?

        0|
        ParentPermalink
    • chhay_seng_sim_1 3 years ago+ 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;

      12|
      ParentPermalink
      • diegomsaiz 2 years ago+ 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!!

        4|
        ParentPermalink
    • pugaliaavichal 3 years ago+ 1 comment

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

      0|
      ParentPermalink
      • chhay_seng_sim_1 3 years ago+ 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.

        0|
        ParentPermalink
    • antoniro 3 years ago+ 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.

      43|
      ParentPermalink
      • little_mushroom 2 years ago+ 0 comments
        [deleted]
        0|
        ParentPermalink
      • LearnCrazy 2 years ago+ 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;

        0|
        ParentPermalink
        • frozendamn 2 years ago+ 1 comment
          [deleted]
          -4|
          ParentPermalink
          • LearnCrazy 2 years ago+ 1 comment

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

            0|
            ParentPermalink
            • frozendamn 2 years ago+ 0 comments
              [deleted]
              0|
              ParentPermalink
        • shankarpentyala 2 years ago+ 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;

          4|
          ParentPermalink
        • robin_csil 2 years ago+ 0 comments
          [deleted]
          -1|
          ParentPermalink
        • symbol2_286 3 months ago+ 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"

          0|
          ParentPermalink
      • ishabandi 2 years ago+ 1 comment

        Why write end from triangles?

        1|
        ParentPermalink
        • yashrajarora0 2 years ago+ 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.

          4|
          ParentPermalink
      • jasmine_kaur 2 years ago+ 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

        -1|
        ParentPermalink
        • rjromanas 2 years ago+ 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

          0|
          ParentPermalink
        • kaneriya_bhouti1 2 years ago+ 1 comment

          did you get the answer for this ?

          0|
          ParentPermalink
          • kaneriya_bhouti1 2 years ago+ 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!!!!

            3|
            ParentPermalink
        • prativadis56 2 years ago+ 0 comments

          It does because the loop will iterate without checking condition

          0|
          ParentPermalink
      • beingpallavi 2 years ago+ 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

        0|
        ParentPermalink
        • bigheadsherry 7 months ago+ 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"

          0|
          ParentPermalink
      • dianlearn 1 year ago+ 0 comments

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

        0|
        ParentPermalink
      • cburas2311 1 year ago+ 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!

        2|
        ParentPermalink
        • ankitapatidar99 6 months ago+ 0 comments

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

          0|
          ParentPermalink
      • nmcastro1 1 year ago+ 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....

        1|
        ParentPermalink
      • lobna_albehery 9 months ago+ 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??

        0|
        ParentPermalink
      • dgknca 9 months ago+ 0 comments

        thanks a lot

        0|
        ParentPermalink
      • Jukgadu 5 months ago+ 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

        0|
        ParentPermalink
      • PREETHA24 4 months ago+ 1 comment

        Why doesn't it work for MySQL?

        0|
        ParentPermalink
        • PREETHA24 4 months ago+ 0 comments

          Okay, it works for that too. Thank You

          0|
          ParentPermalink
      • austindholmes 3 months ago+ 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??

        0|
        ParentPermalink
      • wellingbei 3 months ago+ 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

        2|
        ParentPermalink
    • RatFire 2 years ago+ 0 comments

      whay have you written CASE twice :)

      0|
      ParentPermalink
    • saundaryat 2 years ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • raadu 2 years ago+ 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.

      0|
      ParentPermalink
    • soumyarocks1234 2 years ago+ 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?

      0|
      ParentPermalink
      • harshshah77 2 years ago+ 0 comments

        YES you are correct AND should be used, but if you use OR the test is getting submitted I dont know how?

        0|
        ParentPermalink
    • spyely 10 months ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • naveen_neevan 9 months ago+ 1 comment

      can you please tell me, why not just

      ELSE 'Not A Triangle'

      But,

      END ELSE 'Not A Triangle' END

      0|
      ParentPermalink
      • sharan_theja 9 months ago+ 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.

        1|
        ParentPermalink
    • ketakrs 6 months ago+ 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

      1|
      ParentPermalink
      • joaopacatarino 5 months ago+ 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!

        0|
        ParentPermalink
    • nomanibrahim7 4 months ago+ 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.

      0|
      ParentPermalink
    • rakeshkundu 3 months ago+ 0 comments

      should be WHEN A != B and B != C and A != C THEN 'Scalene'

      0|
      ParentPermalink
    • jsrmaditya96 3 months ago+ 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.

      0|
      ParentPermalink
    • dbhai982 1 month ago+ 0 comments

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

      0|
      ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature