[241008] SQL 스터디

JunichiK·2024년 10월 8일

SQL 스터디

목록 보기
13/21

코드 카타

문제 & 제출 답안

오답노트

  • join 두 번 사용
    • 내가 쓴 답
      select 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
    • Join 두 번
      SELECT 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_text
      1. Inner Join : review 테이블 + 가장 많이 review 작성한 멤버 ID 테이블

        ⇒ review 테이블에 가장 많이 review 작성한 멤버들만 남음

      2. Left Join : review 테이블 + member 테이블

        ⇒ review 테이블에 member 테이블 합침으로써 name 출력 가능

  • Union : 없는 컬럼에 Null 값 넣기
    (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
    • User_id 컬럼
      • Online_Sale 테이블 O
      • Offline_sale 테이블 X
      • 이 상황에서 Union 했을 때 Offline_sale 테이블에 user_id 를 Null 값으로 표기하고 싶다면 Null 로 기입하면 됨.
        • ‘NULL’ 로 표기할 경우 텍스트로 NULL 이라는 값이 채워짐.
      • 단, Offline_sale 테이블을 Union 위에 쓰면 컬럼명이 Null 로 표기됨

강의 스터디

수강할 강의

스터디를 진행할 강의를 링크해주세요.

[SQL] 예제로 익히는 SQL - 4회차

강의에서 필수 사용되는 문법 요약

강의에서 필수로 사용되는 문법에 대한 개념을 요약해주세요.

  1. 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

      • Union : 중복이 있으면 제거
      • Union all : 중복 제거 없이 출력
  2. 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이 내부적으로 인지하고 작동하는 순서

      • FROM → ON → JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY
    • PK Vs. FK

    • ERD

      • Mapping Cardinality

        • One Vs. One and Only one
        • 예시
          A student, Alice, can only have one dorm room at at time. A dorm room can only house one student at a time (for the sake of this example).
          Next year, Alice will be assigned a new dorm room, and at that point her dorm room from this year will be assigned to a new student.
          Alice can have one and only one login (e.g. a11235) and that login can only be assigned to Alice. When Alice graduates, no one else can be assigned the login a11235.
    • Join 종류

profile
represent ojeong-dong

0개의 댓글