SQL 왕초보가 공부하면서 정리하는 SQL 문법

최용환·2022년 7월 17일
0

<조회 쿼리 select>

  • 모든 컬럼 조회
    select * from users

  • 필요한 컬럼 조회
    select user_id, name from users

<select 문법 순서>

select *  from  테이블명
        where 컬럼 조회 조건
group by 컬럼 그룹 조건
order by 컬럼 순서 조건
  • 쿼리의 실행 순서는 from ▶ where ▶ group by ▶ select ▶ order by

<컬럼 조건 조회 where>

 select * from orders
         where payment_method = "kakaopay"

<like 조건>

 select * from users
         where name = "김%"

<조회 결과 행수 제한 limit>

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

<중복 데이터 제외 distinct>

  select distinct(payment_method) from orders

<몇 개인지 숫자 세기 count>

   select count(*) from orders

<특정 컬럼 기준의 데이터 그룹핑 group by>

   select payment_method, count(*) from orders
           where course_title = "웹개발 종합반"
   group by payment_method

<특정 컬럼 기준으로 정렬 order by>

asc : 오름차순, desc : 내림차순
select * from users
order by created_at desc

A와 B 테이블 모두에 있는 레코드만 조회
select o.payment_method, round(AVG(p.point)) from point_users p
inner join orders o on p.user_id = o.user_id
group by o.payment_method

profile
새롭게AI를 시작한 남자

0개의 댓글