SQL & DB/HackerRank SQL Problem

[HackerRank SQL] Type of Triangle

YSY^ 2021. 2. 27. 18:00

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:

  • Equilateral: It's a triangle with  3sides of equal length.
  • Isosceles: It's a triangle with  2sides of equal length.
  • Scalene: It's a triangle with  3sides of differing lengths.
  • Not A Triangle: The given values of A, B, and C don't form a triangle.

Input Format

The TRIANGLES table is described as follows:

Each row in the table denotes the lengths of each of a triangle's three sides.

Sample Input

Sample Output

Isosceles Equilateral Scalene Not A Triangle

Explanation

Values in the tuple (20,20,23) form an Isosceles triangle
Values in the tuple (20,20,20) form an Equilateral triangle

Values in the tuple (20,21,22) form a Scalene triangle
Values in the tuple (13,14,30)  cannot form a triangle 


Answer

select 
    case when A + B > C and B + C > A and A + C > B then #삼각형의 기본조건
        case
        when A = B and A = C and B = C then 'Equilateral' #Equilateral의 조건
        when A = B or B = C or C = A then 'Isosceles' #Isosceles의 조건
        else 'Scalene' #해당되지 않으면 Scalene
        end 
    else 'Not A Triangle' #삼각형의 조건에 해당되지 않으면 삼각형이 아님
    end 
from TRIANGLES 

 

728x90
반응형