📂 JOIN
1. 없어진 기록 찾기 - LEFT OUTER JOIN
천재지변으로 인해 일부 데이터가 유실되었습니다. 입양을 간 기록(ANIMAL_OUTS)은 있는데, 보호소에 들어온 기록(ANIMAL_INS)이 없는 동물의 ID와 이름을 ID 순으로 조회하는 SQL문을 작성해주세요.
* Left Outer Join 을 사용해 ANIMAL_INS 테이블에 데이터가 없어서 join 되지 않는 레코드도 뽑아낼 수 있도록 함
SELECT O.ANIMAL_ID, O.NAME
FROM ANIMAL_OUTS AS O LEFT OUTER JOIN ANIMAL_INS AS I ON I.ANIMAL_ID = O.ANIMAL_ID
WHERE I.ANIMAL_ID IS NULL
ORDER BY O.ANIMAL_ID;
2. 있었는데요 없었습니다 - JOIN
관리자의 실수로 일부 동물의 입양일이 잘못 입력되었습니다. 보호 시작일보다 입양일이 더 빠른 동물의 아이디와 이름을 조회하는 SQL문을 작성해주세요. 이때 결과는 보호 시작일이 빠른 순으로 조회해야합니다.
SELECT I.ANIMAL_ID, I.NAME
FROM ANIMAL_INS AS I JOIN ANIMAL_OUTS AS O ON I.ANIMAL_ID = O.ANIMAL_ID
WHERE I.DATETIME > O.DATETIME
ORDER BY I.DATETIME ASC;
3. 오랜 기간 보호한 동물(1) - LEFT OUTER JOIN, LIMIT
아직 입양을 못 간 동물 중, 가장 오래 보호소에 있었던 동물 3마리의 이름과 보호 시작일을 조회하는 SQL문을 작성해주세요. 이때 결과는 보호 시작일 순으로 조회해야 합니다.
SELECT I.NAME, I.DATETIME
FROM ANIMAL_INS AS I LEFT OUTER JOIN ANIMAL_OUTS AS O ON I.ANIMAL_ID = O.ANIMAL_ID
WHERE O.ANIMAL_ID IS NULL
ORDER BY I.DATETIME ASC
LIMIT 3;
4. 보호소에서 중성화한 동물
보호소에서 중성화 수술을 거친 동물 정보를 알아보려 합니다. 보호소에 들어올 당시에는 중성화되지 않았지만, 보호소를 나갈 당시에는 중성화된 동물의 아이디와 생물 종, 이름을 조회하는 아이디 순으로 조회하는 SQL 문을 작성해주세요.
#Inner Join으로 보호소 들어와서 입양간 친구들만 필터링 후, 성별 및 중성화 여부가 달라졌을 때
SELECT I.ANIMAL_ID, I.ANIMAL_TYPE, I.NAME
FROM ANIMAL_INS AS I INNER JOIN ANIMAL_OUTS AS O ON I.ANIMAL_ID = O.ANIMAL_ID
WHERE I.SEX_UPON_INTAKE != O.SEX_UPON_OUTCOME
ORDER BY I.ANIMAL_ID ASC;
📂 String, Date
1. 루시와 엘라 찾기 - IN
동물 보호소에 들어온 동물 중 이름이 Lucy, Ella, Pickle, Rogan, Sabrina, Mitty인 동물의 아이디와 이름, 성별 및 중성화 여부를 조회하는 SQL 문을 작성해주세요.
SELECT animal_id, name, sex_upon_intake
FROM animal_ins
WHERE name in ('Lucy', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty');
2. 이름에 el이 들어가는 동물 찾기 - LIKE %(글자수 정해줌), _(글자수 정해주지 않음)
보호소에 돌아가신 할머니가 기르던 개를 찾는 사람이 찾아왔습니다. 이 사람이 말하길 할머니가 기르던 개는 이름에 'el'이 들어간다고 합니다. 동물 보호소에 들어온 동물 이름 중, 이름에 EL이 들어가는 개의 아이디와 이름을 조회하는 SQL문을 작성해주세요. 이때 결과는 이름 순으로 조회해주세요. 단, 이름의 대소문자는 구분하지 않습니다.
SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE NAME LIKE '%el%' AND ANIMAL_TYPE = 'Dog'
ORDER BY NAME;
3. 중성화 여부 파악하기 - CASE - (WHEN - THEN) - ELSE - END AS
보호소의 동물이 중성화되었는지 아닌지 파악하려 합니다. 중성화된 동물은 SEX_UPON_INTAKE 컬럼에 'Neutered' 또는 'Spayed'라는 단어가 들어있습니다. 동물의 아이디와 이름, 중성화 여부를 아이디 순으로 조회하는 SQL문을 작성해주세요. 이때 중성화가 되어있다면 'O', 아니라면 'X'라고 표시해주세요.
SELECT DISTINCT ANIMAL_ID, NAME,
CASE
WHEN sex_upon_intake LIKE '%Spayed%'
THEN 'O'
WHEN sex_upon_intake LIKE '%Neutered%'
THEN 'O'
ELSE 'X'
END AS '중성화'
FROM animal_ins
ORDER BY animal_id asc;
4. 오랜 기간 보호한 동물(2) - 날짜차이 DATEDIFF(date1, date2)
입양을 간 동물 중, 보호 기간이 가장 길었던 동물 두 마리의 아이디와 이름을 조회하는 SQL문을 작성해주세요. 이때 결과는 보호 기간이 긴 순으로 조회해야 합니다.
SELECT I.ANIMAL_ID, I.NAME
FROM ANIMAL_INS AS I INNER JOIN ANIMAL_OUTS AS O ON I.ANIMAL_ID = O.ANIMAL_ID
ORDER BY DATEDIFF(O.DATETIME, I.DATETIME) DESC
LIMIT 2;
5. DATETIME에서 DATE로 형 변환 - DATE_FORMAT(date, '%Y-%m-%d')
ANIMAL_INS 테이블에 등록된 모든 레코드에 대해, 각 동물의 아이디와 이름, 들어온 날짜를 조회하는 SQL문을 작성해주세요. 이때 결과는 아이디 순으로 조회해야 합니다.
SELECT ANIMAL_ID, NAME, DATE_FORMAT(DATETIME, '%Y-%m-%d')
FROM ANIMAL_INS
ORDER BY ANIMAL_ID;
'Problem Solving > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 자물쇠와 열쇠(python) - 2020 카카오 신입 공채 (0) | 2021.06.06 |
---|---|
[SQL] Summer/Winter Coding(2019) 우유와 요거트가 담긴 장바구니 (0) | 2021.05.06 |
[SQL고득점Kit] 프로그래머스 GROUP BY, IS NULL (0) | 2021.02.19 |
[SQL고득점Kit] 프로그래머스 SUM, MAX, MIN문 (0) | 2021.02.13 |
[SQL고득점Kit] 프로그래머스 SELECT문 (0) | 2021.02.13 |