Advanced Select

Type of Triangle

https://www.hackerrank.com/challenges/what-type-of-triangle/problem?isFullScreen=true

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.

SELECT
    CASE 
    WHEN A+B<=C OR A+C<=C OR B+C<=A THEN 'Not A Triangle'
    WHEN A=B AND B=C AND A=C THEN 'Equilateral'
    WHEN A=B OR B=C OR A=C THEN 'Isosceles'
    ELSE 'Scalene'
    END
FROM TRIANGLES;

SQL์—์„œ์˜ ๋ถ„๊ธฐ๋ฌธ์„ ์น˜๋Š” ๋ฐฉ๋ฒ•์€

CASE, WHEN-THEN, ELSE, END

WHEN์—๋Š” ์กฐ๊ฑด๊ฐ’์„, THEN์—๋Š” ๊ฒฐ๊ณผ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด๋˜๋Š”๋ฐ, ๋ฆฌํ„ด๊ฐ’์—๋Š” null๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†๋‹ค๋Š” ์  ๊ธฐ์–ต

๊ธฐ๋ณธ์ ์œผ๋กœ if-else์˜ ๋ฐฉ์‹์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ, switch๋ฌธ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด

CASE ๋’ค์— switch์—์„œ ๊ฒ€์‚ฌํ•  ํ•ญ๋ชฉ์ด ๋ฌด์—‡์ธ์ง€ ์ ์–ด์ฃผ๊ณ , WHEN์—๋Š” ๊ทธ๋ƒฅ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ๊ฐ’์„ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค

The PADS

https://www.hackerrank.com/challenges/the-pads/problem?isFullScreen=true&h_r=next-challenge&h_v=zen

๋ญ”๊ฐ€ ์˜์–ด๋ฅผ ๋„ˆ๋ฌด ์ผ๋ผ์ผ๋ผ ์—ฌ์„œ... ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ง„ํ–‰

  1. OCCUPATIONS์˜ ๋ชจ๋“  ์ด๋ฆ„์„ ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ๋‚˜์—ดํ•˜๊ณ , ๋‚˜์—ดํ•  ๋•Œ ๊ฐ ์ง์—…์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์„œ ์ถœ๋ ฅํ•˜๋ผ

  2. OCCUPATIONS์—์„œ ๋‚˜์˜จ ๊ฐ ์ง์—…์˜ ๊ฐฏ์ˆ˜๋ฅผ 'There are a total of [์ˆซ์ž] [์ง์—…]s' ์ด ํฌ๋งท์œผ๋กœ ์ถœ๋ ฅํ•˜๋ผ

SELECT NAME || '(' || SUBSTR(OCCUPATION, 1, 1) || ')'
FROM OCCUPATIONS
ORDER BY NAME;

SELECT 'There are a total of ' || COUNT(OCCUPATION) || ' ' || LOWER(OCCUPATION) || 's.'
FROM OCCUPATIONS
GROUP BY OCCUPATION
ORDER BY COUNT(NAME), OCCUPATION;

๋ง‰ํ˜”๋˜ ๋ถ€๋ถ„์€ SELECT ๋ฌธ์—์„œ ๋ฌธ์ž์—ด์„ ๋”ํ•˜๋Š” ๊ฒƒ์ด์˜€๋Š”๋ฐ...

SQL์—์„œ๋Š” ๋ฌธ์ž์—ด + ์€ ||๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค...!

COUNT, UPPER, LOWER, SUBSTR๋Š” ๊ธฐ์กด์— ํ–ˆ๋˜๊ฒŒ ๊ธฐ์–ต๋‚˜๋”๋ผ

Last updated

Was this helpful?