[SQL] GROUP BY
Updated:
모든 문제의 출처는 프로그래머스 SQL 고득점 KIT입니다. (https://programmers.co.kr/learn/challenges)
예제 1
TIP
- 행의 개수를 알고 싶으면 COUNT(COLUMN_NAME) 사용!
code
SELECT ANIMAL_TYPE, COUNT(ANIMAL_TYPE) AS count
FROM ANIMAL_INS
GROUP BY ANIMAL_TYPE
ORDER BY ANIMAL_TYPE;
예제 2
code
SELECT NAME, COUNT
FROM (
SELECT NAME, COUNT(NAME) AS COUNT
FROM ANIMAL_INS
GROUP BY NAME
) SQ1
WHERE COUNT >= 2 AND NAME IS NOT NULL
ORDER BY NAME;
예제 3
code
SELECT HOUR(DATETIME) AS HOUR, COUNT(DATETIME) AS COUNT
FROM ANIMAL_OUTS
WHERE HOUR(DATETIME) >= 9 AND HOUR(DATETIME) <= 19
GROUP BY HOUR(DATETIME)
ORDER BY HOUR
예제 4
code
Leave a comment