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 start_date like '2022-09%'
order by 1 descSELECT car_id, round(avg(datediff(end_date, Start_Date)+1),1) as AVERAGE_DURATION
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
group by 1
having AVERAGE_DURATION >= 7
order by 2 desc, 1 descselect *
from places
where host_id in (
SELECT host_id
from places
group by 1
having count(id) >= 2
)
order by 1select a.cart_id
from
(
SELECT distinct(cart_id)
from cart_products
where name = 'milk'
) a
inner join
(
select distinct(cart_id)
from cart_products
where name = 'yogurt'
) b
on a.cart_id = b.cart_id
order by 1 ascSELECT concat('/home/grep/src/', b.board_id,'/', f.file_id, f.file_name, f.file_ext)
from used_goods_board b
left join used_goods_file f on b.board_id = f.board_id
where b.views = (SELECT b.views
from used_goods_board b
left join used_goods_file f on b.board_id = f.board_id
order by 1 desc limit 1)
order by f.file_id descselect f.flavor
from first_half f
left join
(
select flavor, sum(total_order) as sum_tot
from july
group by 1
) a on f.flavor = a.flavor
group by 1
order by sum(total_order+sum_tot) desc limit 3select a.author_id, a.author_name, b.category, sum(Total_sales) as sales
from
(
SELECT s.book_id, s.sales_date, s.sales, b.price, s.sales * b.price as Total_sales, b.category, b.author_id, b.published_date
from book_sales s
inner join book b on s.book_id = b.book_id
where s.sales_date like '2022-01%'
) b
inner join author a on b.author_id = a.author_id
group by 1, 3
order by 1, 3 descselect month(start_Date), car_id, count(history_id) as RECORDS
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where (month(start_date) between 8 and 10) and
car_id in (SELECT car_id
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where month(start_date) between 8 and 10
group by 1
having count(history_id) >= 5)
group by 1,2
having count(history_id) <>0
order by 1,2 descselect m.member_name, r.review_text, date_format(r.review_date, '%Y-%m-%d')
from rest_review r
inner join member_profile m on r.member_id = m.member_id
where m.member_id in (select member_id
from rest_review
group by 1
having count(review_text) = (SELECT count(review_text)
from rest_review
group by member_id
order by 1 desc limit 1))
order by 3, 2(SELECT date_format(sales_date,'%Y-%m-%d') as sales_date, product_id, user_id, sales_amount
from online_sale
where sales_date like '2022-03%')
union all
(SELECT date_format(sales_date,'%Y-%m-%d') as sales_date, product_id, NULL, sales_amount
from offline_sale
where sales_date like '2022-03%')
order by 1,2,3select m.member_name, r.review_text, date_format(r.review_date, '%Y-%m-%d')
from rest_review r
inner join member_profile m on r.member_id = m.member_id
where m.member_id in (select member_id
from rest_review
group by 1
having count(review_text) = (SELECT count(review_text)
from rest_review
group by member_id
order by 1 desc limit 1))
order by 3, 2SELECT c.member_name
, a.review_text
, date_format(a.review_date, '%Y-%m-%d')
FROM rest_review a
INNER JOIN (SELECT member_id
FROM rest_review
GROUP BY member_id
ORDER BY COUNT(*) DESC LIMIT 1) b
ON a.member_id = b.member_id
LEFT JOIN member_profile c ON a.member_id = c.member_id
ORDER BY a.review_date, a.review_textInner Join : review 테이블 + 가장 많이 review 작성한 멤버 ID 테이블
⇒ review 테이블에 가장 많이 review 작성한 멤버들만 남음
Left Join : review 테이블 + member 테이블
⇒ review 테이블에 member 테이블 합침으로써 name 출력 가능
(SELECT date_format(sales_date,'%Y-%m-%d') as sales_date, product_id, user_id, sales_amount
from online_sale
where sales_date like '2022-03%')
union all
(SELECT date_format(sales_date,'%Y-%m-%d') as sales_date, product_id, NULL, sales_amount
from offline_sale
where sales_date like '2022-03%')
order by 1,2,3스터디를 진행할 강의를 링크해주세요.
강의에서 필수로 사용되는 문법에 대한 개념을 요약해주세요.
Union
기능 : 수직 결합 (Join은 수평 결합)
Ex.) 1월, 2월 매출 데이터 결합할 때
동일 컬럼으로 구성되어 1월 데이터에 2월꺼 붙이면 될 때
컬럼의 이름, 순서, 형식 모두 동일해야 함.
단, 테이블 자체의 컬럼이 모두 동일해야 하는 것은 아님
2월 테이블 원본에 다른 컬럼이 있어도 union 쓸 때만 컬럼 맞추면 됨.


# union/union all 기본구조
# 컬럼 순서가 같고, 그 형식이 같아야 함
select 컬럼1, 컬럼2, 컬럼3..
from 테이블명1
union (all) #수직결합 명시
select 컬럼1, 컬럼2, 컬럼3..
from 테이블명2
Union Vs. Union all
Join
기능 : 수평 결합
두 테이블에서 공통 컬럼을 보유 시 가능
2개의 공통 컬럼으로 확인할 수 있음
# JOIN 기본 구문
# 공통컬럼이 1개인 경우
select 컬럼1, 컬럼2..
from 테이블 a
join # join의 종류는 아래에서 설명할 예정입니다.
select 컬럼1, 컬럼2..
from 테이블 b
on a.공통컬럼=b.공통컬럼
---------------------------------------------------------------------------
# JOIN 기본 구문
# 공통컬럼이 2개 이상인 경우
select 컬럼1, 컬럼2..
from 테이블 as a
join # join의 종류는 아래에서 설명할 예정입니다.
select 컬럼1, 컬럼2..
from 테이블 as b
on a.공통컬럼=b.공통컬럼 and a.공통컬럼2=b.공통컬럼2
(복습)SQL이 내부적으로 인지하고 작동하는 순서
PK Vs. FK

ERD

Mapping Cardinality



Join 종류
