SQL_코드카타(2023.12.18)

김수경·2023년 12월 18일

코드카타

목록 보기
5/29

MEMBER_PROFILE 테이블에서 생일이 3월인 여성 회원의 ID, 이름, 성별, 생년월일을 조회하는 SQL문을 작성해주세요. 이때 전화번호가 NULL인 경우는 출력대상에서 제외시켜 주시고, 결과는 회원ID를 기준으로 오름차순 정렬해주세요.

SELECT member_id,
member_name,
gender,
date_format(date_of_birth, '%Y-%m-%d') 
from member_profile 
where date_format(date_of_birth, '%m') = 03
and gender = 'W'
and tlno is not null 
order by 1 

CAR_RENTAL_COMPANY_CAR 테이블과 CAR_RENTAL_COMPANY_RENTAL_HISTORY 테이블에서 자동차 종류가 '세단'인 자동차들 중 10월에 대여를 시작한 기록이 있는 자동차 ID 리스트를 출력하는 SQL문을 작성해주세요. 자동차 ID 리스트는 중복이 없어야 하며, 자동차 ID를 기준으로 내림차순 정렬해주세요.

SELECT distinct(c.car_id)
from CAR_RENTAL_COMPANY_CAR c inner join CAR_RENTAL_COMPANY_RENTAL_HISTORY h on c.car_id = h.car_id
where c.car_type = '세단'
and date_format(h.start_date, '%m') = 10 
order by 1 desc 

동물 보호소에 들어온 모든 동물의 정보를 ANIMAL_ID순으로 조회하는 SQL문을 작성해주세요. SQL을 실행하면 다음과 같이 출력되어야 합니다.

SELECT*
from animal_ins 
order by 1

REST_INFO 테이블에서 음식종류별로 즐겨찾기수가 가장 많은 식당의 음식 종류, ID, 식당 이름, 즐겨찾기수를 조회하는 SQL문을 작성해주세요. 이때 결과는 음식 종류를 기준으로 내림차순 정렬해주세요.

SELECT food_type,
rest_id,
rest_name,
max(favorites)
from rest_info 
group by 1 
order by 1 desc 
>> 오답. FAVORITES는 그룹별 max값이 잘 나오지만 RESTID, RESTNAME는 그룹 조건에 없기 때문에 랜덤값이 나와 오류가 발생 함
#서브쿼리에서 즐겨찾기 최대에 해당하는 구문 먼저 구해야 함.
#서브쿼리 강의 복습 후 다시 돌아와 확인해보자.
select a.food_type ,b.rest_id , b.rest_name, a.FAVORITES
from (
        select food_type,max(FAVORITES) FAVORITES
        from rest_info
        group by 1
    ) a  join rest_info b
    on a.food_type = b.food_type and a.FAVORITES = b.FAVORITES
order by 1 desc
profile
잘 하고 있는겨?

0개의 댓글