SQL 코드카타
조건에 맞는 도서 리스트 출력하기
BOOK 테이블에서 2021년에 출판된 '인문' 카테고리에 속하는 도서 리스트를 찾아서 도서 ID, 출판일을 출력
결과는 출판일을 기준으로 오름차순 정렬
- 2021년에 출판된 : BETWEEN 2021-01-01 AND 2021-12-31
- 날짜형으로 바꾸기 : DATE_FORMAT
- 인문 카테고리 : CATEGORY = '인문
- 출판일 기준으로 오름차순 : ORDER BY
SELECT BOOK_ID, DATE_FORMAT(PUBLISHED_DATE, '%Y-%m-%d') AS PUBLISHED_DATE
from BOOK
WHERE PUBLISHED_DATE BETWEEN '2021-01-01' AND '2021-12-31'
AND CATEGORY = '인문'
ORDER BY PUBLISHED_DATE ASC;
Friend Requests II: Who Has the Most Friends
가장 많은 친구와 가장 많은 친구 수를 가진 사람을 찾는 솔루션
테스트 케이스는 한 사람만이 가장 많은 친구를 갖도록 생성
select t.id, sum(t.num) as num
from
(select r.accepter_id as id, count(r.accepter_id) as num
from requestaccepted r
group by id
union all
select rr.requester_id as id, count(rr.requester_id) as num
from requestaccepted rr
group by id) t
group by id
order by num desc
limit 1
African Cities
Given the CITYand COUNTRYtables, query the names of all cities where the CONTINENT is 'Africa'
- table 조인
- CONTINENT is 'Africa' : where문
select c.name
from city c
inner join country o
on o.code = c.countrycode
where o.continent = 'africa'