[스파르타]엑셀보다 쉬운 SQL 문법 정리

김지현·2023년 8월 21일
0

SQL 공부

목록 보기
1/1
post-thumbnail

목차

1주차: Select, Where
데이터를 불러오고 (Select), 조건에 맞게 필터링 하는 것 (Where)!

2주차 : Group by, Order by
데이터를 범주에 따라 묶어서 통계치를 구하고 (Group by), 정렬하는 것 (Order by)!

3주차: Join
여러 데이터를 합쳐서 분석하기 (Join), 더 쉽고 깔끔하게 원하는 데이터를 얻기 (Subquery)!

4주차: Subquery, 그 외
데이터 분석을 위해서 데이터를 원하는 형태로 정리하기!

1주차 (where,select과 함께)

  • 같지 않음 : != '웹개발 종합반'
  • 범위 : created_at between '2020-07-20' and '2020-07-22'
  • 포함 : week in (1,3)
  • 패턴 : email like '%daum.net'
  • 일부데이터만 : limit 5 (5개만 출력)
  • 중복 데이터 제거 : distinct(payment_method)
  • 숫자세기 : count(*)

2주차 (Group by, Order by와 함께)

  • 범주별 통계 : Group by
    select name, count() from users
    group by name
    -> 반올림 : round(,2-뒤에 소숫점)
    -> 최댓값, 최솟값 : min(
    ), max(*)
    -> 평균 : avg(likes)
    -> 합계 : sum(likes)
  • 순서 정리 : Order by
    order by count(*)
    -> 내림차순 : desc
  • 별칭 : as

3주차 (Join - 테이블 연결)

  • Left join : Null 가능(모든 이용자 뭐를 안가질 때)
    -> is NULL / is not NULL
    select name, count(*) from users u
    left join point_users pu on u.user_id = pu.user_id
    where pu.point_user_id is NULL
    group by name
  • Inner join : 공통영역으로 연결
    select * from enrolleds e
    inner join courses c
    on e.course_id = c.course_id
  • union all : 뽑은 거 끼리 붙이고 싶을 때
    -> 이러면 order by 안먹힘
    (
    select '7월' as month, c.title, c2.week, count() as cnt from checkins c2
    inner join courses c on c2.course_id = c.course_id
    inner join orders o on o.user_id = c2.user_id
    where o.created_at < '2020-08-01'
    group by c2.course_id, c2.week
    )
    union all
    (

    select '8월' as month, c.title, c2.week, count(
    ) as cnt from checkins c2
    inner join courses c on c2.course_id = c.course_id
    inner join orders o on o.user_id = c2.user_id
    where o.created_at >= '2020-08-01'
    group by c2.course_id, c2.week
    )

4주차 (Subquery 사용해 연결)

  • where 필드명 in (subquery)
    select * from users u
    where u.user_id in (select o.user_id from orders o
    where o.payment_method = 'kakaopay')
  • select 필드명, 필드명, (subquery) from ..
    select c.checkin_id, c.user_id, c.likes,
    (select avg(likes) from checkins c2
    where c2.user_id = c.user_id) as avg_like_user
    from checkins c;
  • from 에 들어가는 Subquery : 가장 많이 씀
    select pu.user_id, a.avg_like, pu.point from point_users pu
    inner join (
    select user_id, round(avg(likes),1) as avg_like from checkins
    group by user_id
    ) a on pu.user_id = a.user_id
    * SUBSTRING_INDEX : 문자열 쪼개기
    select user_id, email, SUBSTRING_INDEX(email, '@', 1 또는 -1) from users
    -> 이메일에서 아이디(1) 또는 도메인(-1)만 가져오기
    * SUBSTRING(문자열, 출력을 하고싶은 첫 글자의 위치, 몇개의 글자를 출력하고 싶은지)
    select order_no, created_at, substring(created_at,1,10) as date from orders
    * case : 원하는 값을 새 필드에 출력
    select pu.point_user_id, pu.point,
    case
    when
    pu.point > 10000 then '잘 하고 있어요!'
    else '조금 더 달려주세요!' END as 'msg'
    from point_users pu;
  • with 절 사용 : 쿼리 윗쪽에 쓰기
    ->table1,table2...
    with table1 as (
    select pu.point_user_id, pu.point,
    case
    when pu.point >= 10000 then '1만 이상'
    when pu.point >= 5000 then '5천 이상'
    else '5천 미만'
    END as level
    from point_users pu
    )
    select level, count(*) as cnt from table1
    group by level
profile
코린이의 개발 성장일기:)

0개의 댓글