프로그래머스 SQL 고득점 Kit를 풀어봤습니다.
https://programmers.co.kr/learn/challenges?tab=sql_practice_kit
SELECT
모든 레코드 조회하기
SELECT *
from animal_ins
order
by animal_id;
역순 정렬하기
SELECT name, datetime
from animal_ins
order
by animal_id desc;
아픈 동물 찾기
SELECT animal_id
,name
from animal_ins
where intake_condition = 'sick'
order
by animal_id;
어린 동물 찾기
SELECT animal_id
,name
from animal_ins
where intake_condition <> 'aged'
order
by animal_id;
동물의 아이디와 이름
SELECT animal_id
,name
from animal_ins
order
by animal_id;
여러 기준으로 정렬하기
SELECT animal_id
,name
,datetime
from animal_ins
order
by name
,datetime desc;
상위 n개 레코드
SELECT name
from animal_ins
order
by datetime
limit 1;
SUM, MAX, MIN
최댓값 구하기
SELECT datetime
from animal_ins
order
by datetime desc
limit 1;
최솟값 구하기
SELECT datetime
from animal_ins
order
by datetime
limit 1;
동물 수 구하기
SELECT count(animal_id)
from animal_ins;
중복 제거하기
SELECT count(distinct name)
from animal_ins
where name is not null;
GROUP BY
고양이와 개는 몇 마리 있을까
SELECT animal_type
,count(animal_type) as count
from animal_ins
group
by animal_type
order
by animal_type;
동명 동물 수 찾기
select name, count(name)
from animal_ins
group by name
having count(name) >= 2
order by name;
입양 시각 구하기 (1)
SELECT hour(a.datetime) as hour, count(animal_id) as count
from animal_outs as a
group by hour(a.datetime)
having hour >= 9 and hour < 20
order by hour(a.datetime);
입양 시각 구하기 (2)
- 아직 풀지 못했습니다
IS NULL
이름이 없는 동물의 아이디
SELECT animal_id
from animal_ins
where name is null
order
by animal_id;
이름이 있는 동물의 아이디
SELECT animal_id
from animal_ins
where name is not null
order
by animal_id;
NULL 처리하기
SELECT animal_type,
case when name is null then 'No name'
else name end as name
, sex_upon_intake
from animal_ins;
JOIN
없어진 기록 찾기
SELECT b.animal_id
,b.name
from animal_ins as a
right
join animal_outs as b
on a.animal_id = b.animal_id
where a.animal_id is null;
있었는데요 없었습니다
SELECT a.animal_id, a.name
from animal_ins as a
inner
join animal_outs as b
on a.animal_id = b.animal_id
where a.datetime > b.datetime
order by a.datetime;
오랜 기간 보호한 동물 (1)
SELECT a.name, a.datetime
from animal_ins as a
left
join animal_outs as b
on a.animal_id = b.animal_id
where b.animal_id is null
order by a.datetime
limit 3;
보호소에서 중성화한 동물
SELECT a.animal_id, a.animal_type, a.name
from animal_ins as a
inner
join animal_outs as b
on a.animal_id = b.animal_id
where a.sex_upon_intake like 'intact%'
and (b.sex_upon_outcome like 'spayed%' or b.sex_upon_outcome like 'neutered%')
order by a.animal_id;
String, Date
루시와 엘라 찾기
SELECT animal_id, name, sex_upon_intake
from animal_ins
where name in ('Lucy', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty');
이름에 el이 들어가는 동물 찾기
SELECT animal_id, name
from animal_ins
where name like '%el%'
and animal_type = 'Dog'
order by name;
중성화 여부 파악하기
SELECT animal_id
, name
, case when sex_upon_intake like '%Neutered%' then 'O'
when sex_upon_intake like '%Spayed%' then 'O'
else 'X' end as '중성화'
from animal_ins
order by animal_id;
오랜 기간 보호한 동물 (2)
SELECT A.animal_id, A.name
from animal_ins as A
left
join animal_outs as B
on A.animal_id = B.animal_id
order by B.datetime - A.datetime desc
limit 2;
DATETIME에서 DATE로 형 변환
SELECT animal_id
, name
, date_format(A.datetime, '%Y-%m-%d') as '날짜'
from animal_ins as A
order by animal_id;
'SQL > SQL 문제' 카테고리의 다른 글
[SQL] HackerRank - Placements(M) (0) | 2022.06.01 |
---|---|
[SQL] HackerRank - The Report(M) (0) | 2022.06.01 |
[SQL] HackerRank - Blunder, Census, African, 75 Marks(E) (0) | 2022.05.19 |
[SQL] HackerRank - Employee, Revising 5문제(E) (0) | 2022.05.17 |
[SQL] HackerRank - Revising, Select, Population, Japan 9문제(E) (0) | 2022.05.16 |