원하는 곳에서 데이터를 가져오는 기본 명령어
select * from food_orders;
//food_orders 테이블의 모든 데이터를 조회함
payments 테이블의 데이터 조회하기select * from payments;customers 테이블의 데이터 조회하기select * from customers;원하는 컬럼을 선택하기
* 대신에 조회할 컬럼명을 사용한다.select 컬럼1, 컬럼2
from 테이블;
select order_id, restaurant_name
from food_orders;
컬럼에 별명(alias)을 부여
별명을 부여할 수 있다.방법1 : 컬럼1 as 별명1
방법2 : 컬럼2 별명2
| 구분 | 영문, 언더바 | 특수문자, 한글 |
|---|---|---|
| 방법 | 별명만 적음 | "별명"으로 큰 따옴표 안에 적어줌 |
| 예시 | ord_no | "ord no" "주문번호" |
select order_id as ord_no, restaurant_name "식당이름"
from food_orders;
컬럼을 선택하고 별명 지정하기
select order_id as ord_no, price as "가격", quantity as "수량"
from food_orders;
select name "이름", email "e-mail"
from customers;
필터링의 기초문법, WHERE 절을 알아보자
select *
from 테이블
where 필터링 조건 ex) 20살 이상select *
from customers
where age=21;select *
from customers
where gender='female';select *
from food_orders
where cuisine_type='korean';select *
from payments
where pay_type='card';같음, 큼, 작음 등의 조건을 지정해보기
| 비교연산자 | 의미 | 예시 |
|---|---|---|
| = | 같다 | age=21 gender='female' |
| <> | 같지 않다(다르다) | age<>21 gender<>'female' |
| > | 크다 | age>21 |
| >= | 크거나 같다 | age>=21 |
| < | 작다 | age<21 |
| <= | 작거나 같다 | age<=21 |
다양한 종류의 조건
기본문법 : between a and b
where age between 10 and b
//나이가 10~20 사이의 데이터 조회기본문법 : in(A,B,C)
age in (15, 21, 31)
//나이가 15, 21, 31인 데이터 조회기본문법 : like '시작문자%'
name like '김%'
//이름이 '김'으로 시작하는 데이터 조회기본문법 : like '%포함문자%'
restaurant_name like '%Next%'
//식당이름에 'Next'를 포함하는 데이터 조회기본문법 : like '%끝나는문자'
name like '%임'
//이름이 '임'으로 끝나는 데이터 조회WHERE 절에 비교 연산자 적용 실습
select *
from customers
where age >= 40;
select *
from food_orders
where price < 15000;
WHERE 절에 다양한 조건을 적용하기기
select *
from food_orders
where price between 20000 and 30000;
select *
from food_orders
where restaurant_name like 'B%';