MySQL 코딩 공부

김민석·2022년 5월 29일

최근 프로젝트로 인해 SQL을 소홀히 한 점..
반성하며 다시 SQL 공부를 진득하게 해보려한다.
아마 빅분기 실기 공부와 SQL 공부를 3~4시간 씩 하면서 6월을 보내게 될 듯 하다.

이력서와 포트폴리오에 추가할 수 있는 점이 무엇일까 고민하면서 우선 서류 합격과 코딩 테스트 합격을 먼저 해야 면접을 갈 수 있기 때문에 기초부터 튼튼하게!

첫 날이니까 기본적인 것부터 돌아가보자(level1,2)
https://programmers.co.kr/learn/challenges?tab=all_challenges

문제
1. 모든 레코드 조회

SELECT * from animal_ins
order by animal_id

2.역순 정렬

SELECT name, datetime from animal_ins
order by animal_id desc

3.아픈 동물 찾기

SELECT animal_id, name from animal_ins 
where intake_condition = 'sick'
order by animal_id;	

4.어린 동물 찾기

SELECT animal_id, name from animal_ins
where intake_condition != 'Aged'
order by animal_id;

5.동물의 아이디와 이름

SELECT animal_id, name from animal_ins
order by animal_id;

6.여러 기준으로 정렬

SELECT animal_id, name, datetime from animal_ins
order by name asc , datetime desc; 

7.상위 n개

SELECT name from animal_ins
order by datetime asc
limit 1;

(서브쿼리 사용도 가능)

SELECT name
from animal_ins
where datetime = (select min(datetime) from animal_ins)

8.최댓값 구하기

SELECT max(datetime) from animal_ins

9.최솟값 구하기

SELECT min(datetime) from animal_ins

10.동물 수 구하기

SELECT count(animal_id) from animal_ins

11.중복 제거하기

SELECT count(distinct name) from animal_ins

12.고양이와 개 숫자

SELECT animal_type, count(animal_type) as count
from animal_ins
group by animal_type
order by 1

13.동명 동물 수 찾기

SELECT name, count(name) as count from animal_ins
group by 1
having count(name) >= 2
order by 1

14.입양 시각 구하기(1)

SELECT hour(datetime), count(datetime)
from animal_outs
where hour(datetime) between 9 and 20
group by hour(datetime)
order by 1

15.입양 시각 구하기(2)

with recursive hour as(
    select 0 as h
    union all
    select h+1 from hour
    where h < 23
)

SELECT hour.h as hour, count(hour(A.datetime)) as count
from hour
left join animal_outs as A
on hour.h = hour(datetime)
group by 1
order by 1

recursive 문
재귀 반복문에서 임시로 만든 테이블로
첫 select 문은 1회 수행하고
이후 select 문에선 조건까지 반복

16.이름이 없는 동물의 아이디

SELECT animal_id from animal_ins
where name is null
order by 1 asc

17.이름이 있는 동물의 아이디

SELECT animal_id from animal_ins
where name is not null
order by 1 asc

18.Null 처리

SELECT animal_type, 
    if (name is null, 'No name', name) as name,
    sex_upon_intake
from animal_ins
order by animal_id

Case 문도 가능

19.없어진 기록 찾기

select temp.animal_id, outs.name
from animal_outs outs
left join(SELECT B.animal_id, A.name
    from animal_outs B
    left join animal_ins A
    on A.animal_id = B.animal_id
    having A.name is null) temp
on temp.animal_id = outs.animal_id
having temp.animal_id is not null
and name is not null
order by temp.animal_id

20.있었는데요 없었습니다

SELECT A.animal_id, B.name from animal_ins A
right join animal_outs B
on A.animal_id = B.animal_id
where A.datetime > B.datetime
order by A.datetime

21.오랜기간 보호한 동물

select name, datetime
from animal_ins ins
right join (SELECT ins.animal_id from animal_ins ins
left join animal_outs outs
on ins.animal_id = outs.animal_id
where outs.datetime is null) A
on ins.animal_id = A.animal_id
order by datetime asc
limit 3
profile
데이터 사이언스를 공부하는 커피쟁이

0개의 댓글