Problem Solving
[MySQL] Type of Triangle | CASE문(조건문)
luminous13
2022. 3. 29. 15:01
문제
- 3변의 길이가 같으면 "Equilateral"을 2변의 길이가 같으면 " Isosceles", 세 변의 길이가 모두 다르면 "Scalene", 작은 두 변의 합보다 큰 변의 길이가 크거나 같으면 ""Not A Triangle"을 보여주는 쿼리를 작성하세요.
풀이
처음보다 유형이 문제였다. 알고보니 SQL에서도 CASE문을 이용해서 조건문을 작성할 수 있었다.
select
case
when a = b and b = c then "Equilateral"
when a + b <= c then "Not A Triangle"
when a != b and a != c and b != c then "Scalene"
else "Isosceles"
end
from triangles;
배운 것
CASE 문
CASE
WHEN 조건 THEN '반환값'
WHEN 조건 THEN '반환값'
ELSE '반환값'
END
출처: https://www.hackerrank.com/challenges/what-type-of-triangle/problem?isFullScreen=true
Type of Triangle | HackerRank
Query a triangle's type based on its side lengths.
www.hackerrank.com