2025.04.23
select * 혹은 컬럼
from 테이블명
* 별명 지을 때 유의사항
영문, 언더바 => 별명만 ex) ord_no
한글, 특수문자 => 큰 따음표안에 별명 ex) "주문번호", "e-mail"
select *
from 테이블
where 필터링 조건 (문자는 '' 안에)
*반드시 from 뒤에 작성*
age>20 or gender=’female’
→ 나이가 20세 이상이거나, 여성
select food_preparation_time + delivery_time as total_time
from food_orders
=> 음식 준비 시간과 배달 시간을 더하여 총 시간을 알려줘
select avg(price)
from food_orders
=> 가격의 평균을 구해줘
select count(distinct pay_type) count_of_pay_type
from payments
=> 지불방법이 몇개인지 알려줘
지불방법은 cash, card 두개라 2로 나옴
distinct 말고 그냥 데이터 갯수를 세면 모든 갯수를 더한 값이 나옴
# distinct는 중복제거하는 명령어임
select distinct 중복제거 컬럼
form 테이블
select 카테고리컬럼(원하는컬럼 아무거나),
sum(계산 컬럼),
from 테이블
group by 카테고리컬럼(원하는컬럼 아무거나)
select 카테고리컬럼,
sum(컬럼),
from 테이블
group by 카테고리컬럼
order by 정렬을 원하는 컬럼 (카테고리컬럼, sum(컬럼) 둘 다 가능)
# 기본적으로 오름차순, order by 컬럼 desc로 쓰면 내림차순
select cuisine_type,
sum(price) sum_of_price
from food_orders
group by cuisine_type
order by sum(price)
=> 음식별로 나누고 금액의 합을 기준으로 오름차순 정렬