SQL 코딩 테스트

song yuheon·2023년 8월 4일

SQL 코딩테스트

목록 보기
2/4

동물의 아이디와 이름 LEVEL1

SELECT animal_id,name from animal_ins
order by animal_id

이름 있는 동물의 아이디

SELECT animal_id from animal_ins
where name is not NULL

상위 n개의 레코드

SELECT name from animal_ins
order by datetime
limit 1

여러 기준으로 정렬하기

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

어린 동물 찾기

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

역순 정렬하기

SELECT name, datetime from animal_ins
order by animal_id desc

강원도에 위치한 생산공장 목록 출력하기

SELECT factory_id, factory_name, address  from food_factory
where address like '강원도%'

나이 정보가 없는 회원 수 구하기

SELECT count(*) from user_info
where age is NULL

경기도에 위치한 식품 창고 목록 출력하기

-- 코드를 입력하세요

SELECT WAREHOUSE_ID, WAREHOUSE_NAME, ADDRESS,
(
    case when freezer_yn is NULL then 'N'
    else freezer_yn end
) as FREEZER_YN
from food_warehouse
where address like '경기도%'
order by warehouse_id asc

조건에 맞는 회원 수 구하기

SELECT count(*) as users from user_info
where age between 20 and 29 and joined like '2021%'

이름이 없는 동물의 아이디

SELECT animal_id from animal_ins
where name is NULL
order by animal_id asc

가장 비싼 상품 구하기

SELECT max(price) as MAX_PRICE from product

흉부외과 또는 일반 외과 의사 목록 출력하기

SELECT DR_NAME,DR_ID, MCDP_CD,SUBSTRING(HIRE_YMD,1,10) as HIRE_YMD from doctor
where mcdp_cd in ('CS','GS')
order by hire_ymd desc, dr_name asc

12세 이하인 여자 환자 목록 출력하기

SELECT pt_name, pt_no, gend_cd, age, 
(
    case when tlno is NULL then 'NONE'
    else tlno end
) as TLNO
from patient
where age <=12 and gend_cd ='W'
order by age desc, pt_name asc

인기 있는 아이스크림

SELECT flavor from first_half
order by total_order desc, shipment_id asc

모든 레코드 조회하기

SELECT * from animal_ins
order by animal_id

조건에 맞는 도서 리스트 출력하기

SELECT book_id, substring(published_date,1,10) as published_date from book
where published_date like '2021%' and category = '인문'

평균 일일 대여 요금 구하기

SELECT round(avg(daily_fee)) as AVERAGE_FEE from car_rental_company_car
where car_type = 'SUV'

최대값 구하기

SELECT datetime as 시간 from animal_ins
order by datetime desc
limit 1

과일로 만든 아이스크림 고르기

SELECT i.flavor from icecream_info i
inner join first_half f on
i.flavor = f.flavor
where f.total_order >=3000 and i.ingredient_type like 'fruit%'
order by f.total_order desc

특정 옵션이 포함된 자동차 리스트 구하기

SELECT * from car_rental_company_car
where options like '%네비게이션%'
order by car_id desc

자동차 대여 기록에서 장기/단기 대여 구분하기

with table1 as (
    SELECT HISTORY_ID, CAR_ID, substring(start_date,1,10) as START_DATE, 
            substring(end_date,1,10) as END_DATE 
            from car_rental_company_rental_history
            where start_date like '2022-09%'
)
select t.HISTORY_ID, t.CAR_ID, t.START_DATE, t.END_DATE, 
        (
            case when datediff(t.end_date, t.start_date) >=29 then '장기 대여'
            else '단기 대여' end
        ) as RENT_TYPE
    from table1 t
    order by history_id desc

// datediff 함수 **
날짜를 뺄 수 있는 함수
datediff(종료일 , 시작일)

조건에 부합하는 중고거래 댓글 조회하기

SELECT ub.TITLE,ub.BOARD_ID,ur.REPLY_ID,ur.WRITER_ID,ur.CONTENTS,
    substring(ur.created_date,1,10) as CREATED_DATE
    from used_goods_board ub
inner join used_goods_reply ur on
ub.board_id = ur.board_id
where ub.created_date like '2022-10%'
order by ur.created_date asc, ub.title asc
profile
backend_Devloper

0개의 댓글