Skip to content

Latest commit

 

History

History
143 lines (104 loc) · 2.51 KB

내용정리.md

File metadata and controls

143 lines (104 loc) · 2.51 KB

기본적인 조회 SQL

SELECT - FROM - WHERE

주어진 테이블 안에서 조건에 따라 특성을 추출할 때 사용

SELECT 특성
FROM 테이블명
WHERE 조건

--- 예시 ---

# actor 테이블에서 이름이 'Nick'인 배우 찾기
SELECT *
FROM actor
WHERE first_name = 'Nick'

SELECT DISTINCT

유니크한 값들만 받고 싶을 때 사용

SELECT DISTINCT 특성
FROM 테이블_이름;

--예시
select distinct a.first_name 
from actor as a

LIKE

문자열에서 특정 값과 비슷한 값들을 필터할 때에는 LIKE 와 '%' 혹은 '*' 를 사용

SELECT 특성_1, 특성_2
FROM 테이블_이름
WHERE 특성_2 LIKE "%특정 문자열%";

-- 예시
SELECT customers.FirstName, customers.LastName
FROM customers
WHERE customers.LastName LIKE '%han%';

ORDER BY

내림차순, 또는 오름차순 정렬

SELECT 특성
FROM 테이블_이름
ORDER BY 특성_1;

--예시
#(오름차순)
select *
from rental as r
order by r.rental_date

#(내림차순)
select *
from rental as r
order by r.rental_date desc 

LIMIT

돌려받는 데이터 결과 갯수 정하기

SELECT 특성
FROM 테이블_이름
LIMIT 갯수

--예시
# Rental 테이블에서 대여일 기준으로 오름차순 정렬하고, 50개만 출력
select *
from rental as r
order by r.rental_date 
limit 50

FETCH

원하는 행의 갯수만큼의 데이터를 출력하는 옵션

참고 : https://docs.microsoft.com/ko-kr/sql/t-sql/language-elements/fetch-transact-sql?view=sql-server-ver15

--예시
select r.customer_id, r.rental_date 
from rental as r
fetch next 5 rows only # (NEXT 옵션, 이후 5행 출력)

IN

리스트의 값들과 일치하는 데이터 필터링

SELECT 특성_1, 특성_2
FROM 테이블_이름
WHERE 특성_2 IN ("특정값_1", "특정값_2")

--예시
select ad.address, ad.district, ad.phone 
from address as ad
where ad.district in ('Alberta','Attika')

BETWEEN

조건 사이의 값 필터링(A와 B 사이의 값 나열)

SELECT 특성
FROM 테이블_이름
WHERE 특성_1 BETWEEN A AND B

--예시
select r.customer_id, r.rental_date 
from rental as r
where r.rental_date between '2005-05-24 22:54:33' and '2005-05-24 23:04:41'

IS NULL

필드값이 비어 있는 경우 조회

SELECT 특성
FROM 테이블_이름
WHERE 특성_1 IS NULL

--예시
select *
from address as a 
where a.address2 is null