[SQL] HackerRank - Advanced Select
[ Advanced Select ]
Type of Triangle

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 3 sides of equal length.
- Isosceles: It's a triangle with 2 sides of equal length.
- Scalene: It's a triangle with 3 sides of differing lengths.
- Not A Triangle: The given values of A, B, and C don't form a triangle.
세 변의 길이를 사용하여 TRIANGLES 테이블의 각 레코드 유형을 출력하시오.
Equilateral(정삼각형): 세 변의 길이가 같다.Isosceles(이등변삼각형): 두 변의 길이가 같다.Scalene(부등변삼각형): 세 변의 길이가 모두 다르다.Not A Triangle(삼각형이 아니다): A, B, C는 삼각형을 형성하지 않는다.
문제 이해를 돕기 위한 예시
Values in the tuple
(20, 20, 23)form an Isosceles triangle, becauseA = B
(20, 20, 23)은 A ≡ B이기 때문에 Isosceles triangle(이등변삼각형)이다.
Values in the tuple
(20, 20, 20)form an Equilateral triangle, becauseA = B = C.
(20, 20, 20)은 A = B = C이기 때문에 Equilateral triangle(정삼각형)이다.
Values in the tuple
(20, 21, 22)form a Scalene triangle, becauseA ≠ B ≠ C.
(20, 21, 22)은 A ≠ B ≠ C이기 때문에 Scalene triangle(부등변삼각형)이다.
Values in the tuple
(13, 14, 30)cannot form a triangle because the combined value of sides and is not larger than that of side .
(13, 14, 30)은 A + B 값이 C보다 크지 않기 때문에 Not A triangle(삼각형이 아니다)이다.
💡 Solve
SELECT
CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A + B <= C OR A + C <= B OR B + C <= A THEN 'Not A Triangle'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene'
END
FROM TRIANGLES;
문제 이해를 돕기 위한 예시를 잘 참고하여 코드를 작성하면 된다.- 세 변의 길이가 모두 다르다는 조건으로
Scalene를 먼저 코드로 작성하게 되면Not A Triangle인 경우가 잘못 판단될 수 있으므로 위와 같이Not A Triangle인 경우를 먼저 검사해줘야 한다.
Author And Source
이 문제에 관하여([SQL] HackerRank - Advanced Select), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@mmy789/Advanced-Select저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)