1. 가장 큰 물고기 10마리 구하기
select ID, LENGTH from FISH_INFO where LENGTH is not null order by LENGTH DESC , ID ASC limit 10
- is not null
- order by (조건 1) , (조건 2)
- limit은 가장 끝에
2. 한 해에 잡은 물고기 구하기
select count(ID) as fish_count from fish_info where year(time) = 2021;
- as 로 칼럼 이름 다르게 저장하는 것
- where 절 쓰기
- 내장함수 year()
3. 잡은 물고기 중 가장 큰 물고기의 길이 구하기
select concat(max(length),'cm') as max_length from fish_info
- 내장함수 concat()
- 내장함수 max()
4. 잡은 물고기의 평균 구하기
select round(avg(ifnull(length,10)),2) as AVERAGE_LENGTH from fish_info
- 반올림해서 n째자리 수까지 나타내시오 : round(x, n)
- null인 부분 대치해서 리턴하는 함수 : ifnull(column, 대치할값)
5. 잔챙이 잡은 수 구하기
select count(ID) as fish_count from fish_info where length is null
- is null 연습문제
6. Python 개발자 찾기
select id, email, first_name, last_name from developer_infos where skill_1 = 'Python' or skill_2 = 'Python' or skill_3 = 'Python' order by ID asc
- or문 연습문제
7. 조건에 부합하는 중고 거래 댓글 조회하기
select a.title, a.board_id, b.reply_id, b.writer_id, b.contents, date_format(b.created_date, '%Y-%m-%d') as created_date from used_goods_board as a join used_goods_reply as b on a.board_id = b.board_id where year(a.created_date) = 2022 and month(a.created_date) = 10 order by b.created_date asc, a.title asc
- join 연습
- 내장함수 date_format(time | date, format)
8. 특정 옵션이 포함된 자동차 리스트 구하기
SELECT * from car_rental_company_car where options like "%네비게이션%" order by car_id desc
- like 연습
9. 자동차 대여 기록에서 장기/단기 대여 구분하기
SELECT history_id, car_id, date_format(start_date, '%Y-%m-%d') as start_date, date_format(end_date, '%Y-%m-%d') as end_date,
case when datediff(end_date, start_date) +1 >= 30 then '장기 대여'
else '단기 대여' end as rent_type
from car_rental_company_rental_history
where year(start_date) = 2022 and month(start_date) = 9
order by history_id desc
- mysql에서의 datediff 함수 용례 주의, 범위가 [) 라서 그냥 빼면 -1의 값이 나옴
10. 평균 일일 대여 요금 구하기
SELECT round(avg(daily_fee),0) as average_fee from car_rental_company_car where car_type = 'SUV'
- round 함수 연습
11. 조건에 맞는 도서 리스트 출력하기
SELECT book_id, date_format(published_date,'%Y-%m-%d') as published_date
from book
where year(published_date) = 2021 and category like '%인문%'
order by published_date asc
- date_format
12. 과일로 만든 아이스크림 고르기
SELECT a.flavor from first_half as a
join icecream_info as b
on a.flavor = b.flavor
where b.ingredient_type = 'fruit_based' and total_order > 3000
order by a.total_order desc
- join 연습