SQL
[프로그래머스] SQL 코딩테스트 레벨 2
지속가능한 성장을
2020. 12. 20. 13:09
최솟값 구하기
문제 풀이
select datetime
from animal_ins
order by datetime limit 1
동물 수 구하기
문제 풀이
select count(*)
from animal_ins
중복 제거하기
문제 풀이
select count(DISTINCT(name))
from animal_ins
where name is not null;
고양이와 개는 몇 마리 있을까
문제 풀이
select animal_type, count(animal_type) as 'count'
from animal_ins
group by animal_type
order by animal_type;
null 처리하기
문제 풀이
- Mysql :
select animal_Type, ifnull(name, 'No name') as 'NAME', sex_upon_intake from animal_ins;
- ORCLE:
select animal_type, nvl(name, 'No name'), sex_upon_intake from animal_ins order by animal_id;
차이점 : orcle은 NVL
을, MySQL은 ifnull
함수를 지원한다.
동명 동물 수 찾기
문제 풀이
select name, count(name) as 'count'
from animal_ins
group by name
having count(name) >= 2
order by name
루시와 엘라 찾기
문제 풀이
select animal_id, name, sex_upon_intake
from animal_ins
where name in ('Lucy', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty')
order by animal_id
이름에 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 = 'Neutered Male' then 'O'
when SEX_UPON_INTAKE = 'Spayed Female' then 'O'
else 'X'
end '중성화'
from animal_ins
order by animal_id
DATETIME에서 DATE로 형 변환
문제 풀이
select animal_id, name, to_char(datetime, 'YYYY-MM-DD')
from animal_ins
order by animal_id
입양 시각 구하기(1)
문제 풀이
select hour(datetime) as HOUR, count(hour(datetime)) as 'COUNT'
from animal_outs
group by hour(datetime)
having HOUR >= 9 and HOUR<20
order by HOUR