MySQL 프로그래머스 - Level 1
- 1. 최댓값 구하기
- 2. 모든 레코드 조회하기
- 3. 역순 정렬하기
- 4. 아픈 동물 찾기
- 5. 어린 동물 찾기
- 6. 동물의 아이디와 이름
- 7. 이름이 없는 동물의 아이디
- 8. 여러 기준으로 정렬하기
- 9. 상위 n개 레코드
- 10. 이름이 있는 동물의 아이디
1. 최댓값 구하기
SELECT datetime as '시간' from animal_ins order by datetime desc limit 1
2. 모든 레코드 조회하기
SELECT * from animal_ins;
3. 역순 정렬하기
SELECT name, datetime from animal_ins order by animal_id desc;
4. 아픈 동물 찾기
SELECT animal_id, name from animal_ins where INTAKE_CONDITION = 'sick' order by animal_id ;
5. 어린 동물 찾기
-- 방법 1
select animal_id,name from animal_ins
where not intake_condition in ('aged')
order by animal_id;
-- 방법 2
select animal_id,name from animal_ins
where intake_condition != 'aged'
order by animal_id;
-- 방법 3
select animal_id,name from animal_ins
where intake_condition <> 'aged'
order by animal_id;
!= 는 <>로 자동 변환되어 사용되므로 퍼포먼스를 위해 <>를 사용하는 것이 좋다.
6. 동물의 아이디와 이름
SELECT animal_id,name from animal_ins order by animal_id;
7. 이름이 없는 동물의 아이디
SELECT animal_id from animal_ins where name is null;
where 문에서 null 확인은 is 사용
8. 여러 기준으로 정렬하기
select animal_id,name,datetime from animal_ins order by name, datetime desc;
9. 상위 n개 레코드
SELECT name from animal_ins order by datetime limit 1;
10. 이름이 있는 동물의 아이디
SELECT animal_id from animal_ins where name is not null order by animal_id;
pk가 id라면 자동으로 오름차순 정렬되긴 해서 따로 정렬 안해도 된다.