데이터 활용 능력 키우기 💪

SQL select 문법 정리

who_knew 2021. 12. 25. 19:36

1. 데이터베이스에 존재하는 테이블 모두 보기

show tables;

 

2. 특정 테이블(ex.orders)의 데이터 보기

select * from orders;

 

3. 특정 테이블의 특정 데이터열만 뽑아보기

select created_at, course_title, payment_method, email from orders;

 

4. where절: 특정 조건의 테이블 보기

select * from orders
where payment_method = "kakaopay";

 - 같지 않음

select * from orders
where course_title != "웹개발 종합반";

 

- 범위 조건

select * from orders
where created_at between "2020-07-13" and "2020-07-15";

- 포함 조건

select * from checkins 
where week in (1, 3);

- like문

  • where email like 'a%': email 필드값이 a로 시작하는 모든 데이터
  • where email like '%a' email 필드값이 a로 끝나는 모든 데이터
  • where email like '%co%' email 필드값에 co를 포함하는 모든 데이터
  • where email like 'a%o' email 필드값이 a로 시작하고 o로 끝나는 모든 데이터

 

5. 일부 데이터만 가져오는 Limit

select * from orders 
where payment_method = "kakaopay"
limit 5;

 

6. 중복 데이터는 제외하고 가져오는 Distinct

select distinct(payment_method) from orders;

 

7. 개수를 세어주는 Count

select count(*) from orders

 

8. 범주의 통계를 내어주는 Group by

select name, count(*) from users
group by name;
  • min: 최소값
  • max: 최대값
  • avg: 평균
  • sum: 합계

 

9. 정렬을 해주는 Order by

select * from checkins
order by likes desc;

 

10. 여러 테이블을 연결해주는 Join

select * from orders o
inner join users u
on o.user_id = u.user_id;

inner join

 

left join

 

on: 두 테이블의 공통 부분을 '='으로 연결

 

 

11. 두 결과물을 합칠 수 있는 Union All

(
	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
  order 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
  order by c2.course_id, c2.week
)

 

12. 하나의 쿼리 안에 또다른 쿼리를 존재시키는 Subquery

select checkin_id, course_id, user_id, likes, 
(select avg(c2.likes) from checkins c2
where c.course_id = c2.course_id) 
from checkins c;

 

 

13. 경우에 따라 원하는 값을 새 필드에 출력해주는 Case

select pu.point_user_id, pu.point,
case 
when pu.point > 10000 then '1만 이상'
when pu.point > 5000 then '5천 이상'
else '5천 미만'
END as lv
from point_users pu

 

14. with로 테이블 간단하게 정의해주기

with table1 as (
	select course_id, count(distinct(user_id)) as cnt_checkins from checkins
	group by course_id
), table2 as (
	select course_id, count(*) as cnt_total from orders
	group by course_id
)

select c.title,
       a.cnt_checkins,
       b.cnt_total,
       (a.cnt_checkins/b.cnt_total) as ratio
from table1 a inner join table2 b on a.course_id = b.course_id
inner join courses c on a.course_id = c.course_id

 

15.  문자열 데이터를 원하는 형태로 정리해주는 SUBSTRING_INDEX, SUBSTRING

select user_id, email, SUBSTRING_INDEX(email, '@', 1) from users
select user_id, email, SUBSTRING_INDEX(email, '@', -1) from users
select substring(created_at,1,10) as date, count(*) as cnt_date from orders
group by date