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 sides of equal length.
- Isosceles: It's a triangle with sides of equal length.
- Scalene: It's a triangle with sides 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 form an Isosceles triangle, because .
Values in the tuple form an Equilateral triangle, because . Values in the tuple form a Scalene triangle, because .
Values in the tuple cannot form a triangle because the combined value of sides and is not larger than that of side .
[나의 풀이]
SELECT CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
WHEN A+B <= C OR A+C <= B OR B+C <= A THEN 'Not A Triangle'
ELSE 'Scalene' END AS A
FROM TRIANGLES
출력결과
A |
Equilateral |
Equilateral |
Isosceles |
Equilateral |
Isosceles |
Equilateral |
Not A Triangle |
Scalene |
Scalene |
: : |
[설명]
CASE WHEN 은 파이썬에서 IF문과 같다.
SQL 문 | 설명 |
WHEN A = B AND B = C THEN 'Equilateral' | 세 변의 길이가 같으면 'Equilateral' 출력 |
WHEN A = B OR B = C OR A = C THEN 'Isosceles' | 두 변의 길이가 같으면 'Isosceles' 출력 |
WHEN A+B <= C OR A+C <= B OR B+C <= A THEN 'Not A Triangle' | 두 변의 길이의 합이 다른 한 변보다 짧으면 'Not A Triangle' 출력 |
ELSE 'Scalene' END | 위의 조건들이 다 아니라면 'Scalene' 출력 |
'Test > MYSQL' 카테고리의 다른 글
Reformat Department Table (CASE WHEN/ THEN, GROUP BY) (0) | 2023.05.16 |
---|---|
Average Population (0) | 2023.05.10 |
Weather Observation Station 5 (0) | 2023.05.07 |
Weather Observation Station 9 (0) | 2023.05.06 |
Weather Observation Station 8 (0) | 2023.05.04 |