SELECT * FROM sakila.customer WHERE first_name LIKE 'J%';
SELECT * FROM sakila.customer WHERE active=1 AND email IS NOT NULL;
SELECT * FROM sakila.customer WHERE create_date > '2006-03-01';
SELECT * FROM sakila.customer WHERE first_name LIKE '%Lee%';
SELECT * FROM sakila.customer WHERE email LIKE '%@example.com%';
SELECT * FROM sakila.customer WHERE first_name LIKE '_____';
SELECT * FROM sakila.customer WHERE customer_id IN (1, 3, 4);
SELECT * FROM sakila.customer WHERE customer_id=1 OR customer_id=3 OR customer_id=4;
SELECT * FROM sakila.customer WHERE first_name IN ('John', 'Lisa', 'Emma');
SELECT * FROM sakila.city WHERE country_id IN (103, 86) AND city IN ("Cheju", "Sunnyvale", "Dalloas");
SELECT * FROM sakila.customer WHERE store_id IN (1, 2)
AND first_name IN ('Mary', 'John')
AND last_update IS NOT NULL
AND last_update > '2006-01-01'
ORDER BY last_update DESC;
SELECT * FROM sakila.customer ORDER BY last_name ASC;
SELECT * FROM sakila.customer ORDER BY first_name DESC;
SELECT * FROM sakila.customer ORDER BY last_name ASC, first_name ASC;
SELECT * FROM sakila.customer ORDER BY store_id ASC, last_update DESC;
SELECT * FROM sakila.customer WHERE first_name="Mary" ORDER BY last_name ASC;
SELECT * FROM sakila.customer ORDER BY active DESC, first_name ASC;
SELECT * FROM sakila.customer WHERE first_name LIKE '%a%' ORDER BY create_date ASC;
SELECT customer_id, first_name, last_name, length(first_name) AS name_length FROM sakila.customer ORDER BY name_length ASC;
SELECT customer_id, first_name, last_name FROM sakila.customer WHERE first_name LIKE 'M%' AND last_name LIKE '%e%' ORDER BY first_name ASC;
SELECT
sum(active) as active_customers,
count(*) - sum(active) as inactive_customers,
round(sum(active) / count(*) *100, 2) as active_percentage,
sum(active) / count(*) as average_active_status
FROM sakila.customer;
SELECT customer_id, first_name, last_name, length(first_name) as name_length
FROM sakila.customer
WHERE length(first_name) >= 6
ORDER BY name_length DESC;
SELECT active, count(*) as total_customers
FROM sakila.customer
GROUP BY active;
SELECT
LEFT(last_name, 1) as first_letter,
count(*) as customer_count
FROM sakila.customer
GROUP BY LEFT(last_name, 1)
ORDER BY first_name ASC;
SELECT length(first_name) as name_length, count(*) as customer_count
FROM sakila.customer
GROUP BY length(first_name)
ORDER BY name_length ASC;
SELECT active, avg(length(first_name)) as avg_name_length
FROM sakila.customer
GROUP BY active;
SELECT
LEFT(last_name, 1) as first_letter,
count(*) as total_customers,
sum(active) as active_customers,
round(sum(active) * 100.0 / count(*), 2) as active_rate
FROM sakila.customer
GROUP BY LEFT(last_name, 1)
ORDER BY active_rate desc;
SELECT
active, max(length(first_name)) as max_name_length,
min(length(first_name)) as min_name_length,
avg(length(first_name)) as avg_name_length
FROM sakila.customer
GROUP BY active;
SELECT store_id, count(*) as customer_count
FROM sakila.customer
GROUP BY store_id HAVING count(*) >= 2;
SELECT store_id, count(*) as customer_count
FROM sakila.customer
WHERE store_id <= 2 IS NOT NULL
GROUP BY store_id;
SELECT
address_id,
count(*) as customer_count
FROM sakila.customer
GROUP BY address_id
HAVING address_id >= 5;
SELECT
store_id,
count(customer_id) as active_customer_count,
avg(address_id) as avg_address_id
FROM sakila.customer
WHERE active=1
GROUP BY store_id
HAVING count(customer_id) >= 2 AND avg(address_id) > 5;
SELECT
store_id,
count(customer_id) as total_customers,
max(customer_id) as max_customer_id
FROM sakila.customer
GROUP BY store_id
HAVING count(customer_id) >= 2 AND max(customer_id) > 5;
SELECT
store_id,
count(customer_id) as total_active_customers,
avg(address_id) as avg_address_id,
max(last_update) as last_update
FROM sakila.customer
WHERE active=1 AND last_update > '2006-02-15'
GROUP BY store_id
HAVING count(customer_id) >= 2 AND avg(address_id) > 5
ORDER BY total_active_customers DESC;
SELECT
c.store_id,
count(c.customer_id) as total_active_customers,
avg(c.address_id) as avg_address_id,
max(c.last_update) as last_update
FROM sakila.customer c
WHERE c.active=1 AND c.last_update > '2006-02-15'
GROUP BY c.store_id
HAVING count(c.customer_id) >= 2 AND avg(c.address_id) > 5
ORDER BY total_active_customers DESC;
SELECT DISTINCT
customer_id,
first_name,
last_name,
email,
active,
create_date
FROM sakila.customer
WHERE create_date > '2006-01-01' AND active=1
GROUP BY customer_id
HAVING customer_id % 2 = 0 AND avg(customer_id) > 3
ORDER BY create_date DESC;
SELECT
c.customer_id,
c.first_name,
c.last_name,
c.store_id,
s.manager_staff_id,
s.address_id
FROM sakila.customer c
LEFT JOIN sakila.store s
ON c.store_id = s.store_id
ORDER BY c.customer_id;
SELECT
s.store_id,
s.manager_staff_id,
c.customer_id,
c.first_name,
c.last_name,
c.store_id as customer_store_id
FROM sakila.store s
RIGHT JOIN sakila.customer c
ON s.store_id = c.store_id
ORDER BY c.customer_id;
SELECT
c.customer_id,
c.first_name,
c.last_name,
c.store_id as customer_store_id,
s.store_id as store_store_id,
s.manager_staff_id
FROM sakila.customer c
INNER JOIN sakila.store s
ON c.store_id = s.store_id
ORDER BY c.customer_id;
SELECT
c.customer_id,
c.first_name,
c.last_name,
c.store_id,
count(i.film_id) as number_of_rental
FROM sakila.customer c
INNER JOIN sakila.rental r
ON c.customer_id = r.customer_id
INNER JOIN sakila.inventory i
ON r.inventory_id = i.inventory_id
GROUP BY c.customer_id
HAVING count(i.film_id) >= 5
ORDER BY number_of_rental DESC;
정리
| 문법/연산자 | 설명 |
|---|
SELECT | 데이터를 조회하는 기본 문법. |
WHERE | 조건을 만족하는 데이터만 필터링. |
LIKE | 문자열 패턴을 검색. |
% | 0개 이상의 임의 문자와 일치. |
_ | 단일 문자와 일치. |
IN | 여러 값 중 하나와 일치하는 조건을 검색. |
AND | 두 조건을 모두 만족하는 데이터를 필터링. |
OR | 둘 중 하나 이상의 조건을 만족하는 데이터를 필터링. |
NOT | 조건을 부정. |
ORDER BY | 데이터를 정렬. |
ASC | 오름차순 정렬 (기본값). |
DESC | 내림차순 정렬. |
GROUP BY | 데이터를 그룹화하여 집계. |
HAVING | 그룹화된 데이터에 조건을 적용. |
DISTINCT | 중복된 데이터를 제거하고 고유한 결과만 반환. |
IS NULL | 값이 NULL인지 확인. |
IS NOT NULL | 값이 NULL이 아닌지 확인. |
JOIN | 여러 테이블을 연결. |
LEFT JOIN | 왼쪽 테이블의 모든 데이터와 오른쪽 테이블의 일치하는 데이터 결합. |
RIGHT JOIN | 오른쪽 테이블의 모든 데이터와 왼쪽 테이블의 일치하는 데이터 결합. |
INNER JOIN | 두 테이블 모두에서 일치하는 데이터만 결합. |
COUNT | 데이터의 개수를 계산. |
SUM | 데이터의 합계를 계산. |
AVG | 데이터의 평균을 계산. |
MAX | 데이터의 최대값을 반환. |
MIN | 데이터의 최소값을 반환. |
ROUND | 숫자를 반올림. |
LEFT | 문자열의 왼쪽에서 지정된 수만큼 문자 반환. |
LENGTH | 문자열의 길이를 반환. |
% (모듈로 연산) | 나머지 연산. |