1.비교연산자 사용하기:
film 테이블에서 rental_rate가 2.99 이상인 모든 영화의
title과 rental_rate를 조회하세요.
select title,rental_rate
from film
where rental_rate >= 2.99;
SELECT title, rental_rate
FROM film
WHERE rental_rate >= 2.99;
2.불일치 확인하기:
customer 테이블에서 email이 gmail.com으로 끝나지 않는 고객의 first_name, last_name, email을 조회하세요.
SELECT first_name, last_name, email
FROM customer
WHERE email !~* 'gmail\.com$';
정규표현식
!~* 연산자는 특정 패턴을 포함하지 않는 경우를 나타냄
$ 정규 표현식에서 문자열의 끝을 나타맴
select first_name,last_name,email
from customer
where email not like '%gmail.com%';
/*
정답에 가깝지만 정답이 아님
%는 SQL에서 사용되는 와일드카드 문자
와일드카드 문자는 문자열 검색 시 특정 패턴을 나타내는 데 사용됨
정규 표현식에서는 %와 같은 와일드카드 문자를 사용하지 않음
*/
-- 정답
SELECT first_name, last_name, email
FROM customer
WHERE email NOT LIKE '%gmail.com';
3.AND 사용하기:
actor 테이블에서
first_name이 'PENELOPE'이고
last_name이 'GUINESS'인 배우의 정보를 조회하세요.
select *
from actor
where (first_name = 'Penelope') and (last_name = 'Guiness');
--쿼리의 가독성을 높이기 위해 괄호를 추가
-- 정답
SELECT *
FROM actor
WHERE first_name = 'PENELOPE' AND last_name = 'GUINESS';
4.OR 사용하기:
film 테이블에서
length가 50 이하거나
150 이상인 영화의
title, length를 조회하세요.
select title,length
from film
where (length <= 50) or (length >= 150);
-- 정답
SELECT title, length
FROM film
WHERE length <= 50 OR length >= 150;
5.IN 사용하기:
customer 테이블에서
store_id가 1 또는 2인 고객의
first_name, last_name를 조회하세요.
select first_name,last_name,store_id
from customer
where store_id in (1,2);
-- WHERE을 사용
select first_name,last_name,store_id
from customer
where (store_id = 1) or (store_id = 2) ;
select first_name,last_name,store_id
from customer
where (store_id = '1') or (store_id = '2') ;
-- PostgreSQL에서는 문자열과 숫자 사이에 자동 형변환이 발생하기 때문에 결과가 같음
-- 정답
SELECT first_name, last_name
FROM customer
WHERE store_id IN (1, 2);
6.BETWEEN 사용하기:
payment 테이블에서
amount가 8과 10 사이인 결제 내역의
customer_id, amount를 조회하세요.
select customer_id, amount
from payment
where amount between 8 and 10;
-- 정답
SELECT customer_id, amount
FROM payment
WHERE amount BETWEEN 8 AND 10;
7.복합 조건 사용하기:
film 테이블에서
영화의 길이(length)가 100분을 초과하고,
대여 비용(rental_rate)이 2.99 이하인 영화의
title, length, rental_rate를 조회하세요.
추가로, 이 영화들 중 rating이 'G' 또는 'PG'인 영화만 결과로 나타내세요.
select title, length, rental_rate
from film
where (length > 100) and (rental_rate <= 2.99) and (rating = 'G' or rating = 'PG');
-- IN 사용
select title, length, rental_rate
from film
where (length > 100) and (rental_rate <= 2.99) and rating in ('G','PG');
-- 정답
SELECT title, length, rental_rate
FROM film
WHERE length > 100 AND rental_rate <= 2.99 AND rating IN ('G', 'PG');
8.특정 연도의 영화만 조회하기:
film 테이블에서
2006년에 출시된(release_year가 2006) 영화 중
rental_duration이 6일 이상인 영화의
title, release_year, rental_duration을 조회하세요.
select title,release_year, rental_duration
FROM film f
where release_year = 2006 and rental_duration >= 6 ;
-- 정답
SELECT title, release_year, rental_duration
FROM film
WHERE release_year = 2006 AND rental_duration >= 6;
9.대여료와 대여 기간으로 필터링하기:
film 테이블에서 대여료(rental_rate)가 2.99 이상이며,
대여 기간(rental_duration)이 3일 이하인 영화의
title, rental_rate, rental_duration을 조회하세요.
select title, rental_rate, rental_duration
FROM film f
where rental_rate >= 2.99 and rental_duration <= 3 ;
-- 정답
SELECT title, rental_rate, rental_duration
FROM film
WHERE rental_rate >= 2.99 AND rental_duration <= 3;
10.특정 문자열이 포함된 영화 조회하기:
film 테이블에서 설명(description)에
'Action'이라는 단어가 포함된 영화의
title, description을 조회하세요.
select title,description
from film
where description like '%Action%';
-- A가 소문자이면 테이블에 없기 때문에 찾지 못함
-- 정답
SELECT title, description
FROM film
WHERE lower(description) LIKE '%action%';
11.여러 조건을 사용한 고객 조회:
customer 테이블에서
first_name이 'Mary'이거나
last_name이 'Smith'인 고객의
first_name, last_name, email을 조회하세요.
또한, 이메일이 NULL이 아닌 고객만 결과에 포함하세요.
select first_name,last_name,email
from customer
where (first_name = 'Mary' or last_name = 'smith') and email is not null;
-- 정답
SELECT first_name, last_name, email
FROM customer
WHERE (first_name = 'Mary' OR last_name = 'Smith') AND email IS NOT NULL;
12. 지정된 대여료 범위 밖의 영화 조회하기:
film 테이블에서
대여료(rental_rate)가 0.99 미만이거나 4.99 초과인
모든 영화의 title, rental_rate를 조회하세요.
select title,rental_rate
from film
where rental_rate < 0.99 or 4.99 < rental_rate ;
-- 정답
SELECT title, rental_rate
FROM film
WHERE rental_rate < 0.99 OR rental_rate > 4.99;
13.가장 최근 영화 5개 조회하기:
film 테이블에서
가장 최근에 출시된 5개의 영화의
title과 release_year를 조회하세요.(LIMIT 사용)
select title,release_year
from film
order by release_year desc
limit 5;
-- 정답
SELECT title, release_year
FROM film
ORDER BY release_year DESC
LIMIT 5;
14.가장 저렴한 대여료의 영화 3개 조회하기:
film 테이블에서
대여료(rental_rate)가 가장 낮은 3개의 영화의
title과 rental_rate를 조회하세요.(LIMIT 사용)
select title,rental_rate
from film
order by rental_rate asc
limit 3;
-- 정답
SELECT title, rental_rate
FROM film
ORDER BY rental_rate ASC
LIMIT 3;
15.고객 목록에서 처음 등록된 10명 조회하기:
customer 테이블에서
가장 처음 등록된 10명의 고객의
first_name, last_name, create_date를 조회하세요.(LIMIT 사용)
select first_name, last_name, create_date
from customer
order by create_date asc
limit 10;
-- 정답
SELECT first_name, last_name, create_date
FROM customer
ORDER BY create_date ASC
LIMIT 10;
16.첫 번째 페이지의 영화 5개 조회하기:
film 테이블에서
첫 번째 페이지의 영화 5개의
title과 release_year를 조회하세요.
(페이지 크기를 5로 가정)(FETCH 사용)
select title,release_year
from film
fetch first 5 rows only;
-- 정답
SELECT title, release_year
FROM film
ORDER BY release_year DESC
FETCH FIRST 5 ROWS ONLY;
17.두 번째 페이지의 고객 목록 조회하기:
customer 테이블에서
두 번째 페이지의 고객 10명의 first_name, last_name를 조회하세요. (페이지 크기를 10으로 가정)(FETCH 사용)
select first_name,last_name
from customer
offset 10 row
fetch first 10 rows only;
--질문에 맞는 답
select first_name,last_name
from customer
offset 11 row
fetch first 10 rows only;
--사진에 맞는 답
-- 정답
SELECT first_name, last_name FROM customer
ORDER BY create_date ASC
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY;
18.세 번째 페이지의 대여 기록 조회하기:
rental 테이블에서
세 번째 페이지의
대여 기록 5개를 조회하세요.
(페이지 크기를 5로 가정)(FETCH 사용)
select rental_id,rental_date
from rental
offset 10 rows
fetch first 5 rows only;
-- 정답
SELECT rental_id, rental_date FROM rental
ORDER BY rental_date DESC
OFFSET 10 ROWS
FETCH NEXT 5 ROWS ONLY;
정규식
문자열을 검색하고 편집하기 위한 유연하고 강력한 패턴 매칭 도구
이를 사용하면 특정한 규칙에 따라 문자열을 찾거나 변형할 수 있음
(예를 들어, 이메일 주소나 전화번호와 같은 특정한 형식을 갖는 문자열을 찾거나,
특정한 단어나 구절을 바꾸는 등 다양한 작업에 사용됨
정규식은 간결한 표현으로 복잡한 문자열 패턴을 다룰 수 있어서
많은 프로그래밍 언어와 텍스트 편집기에서 널리 사용됨)