계속 업데이트 할 예정
-- 조건
CASE WHEN ~ THEN ~
WHEN ~ THEN ~
ELSE ~
END
--- NULL은 = 연산자 안됨
WHERE age IS NULL
WHERE age IS NOT NULL

SELECT user_id, product_id
FROM online_sale
GROUP BY user_id, product_id
HAVING COUNT(*) >= 2
ORDER BY user_id ASC, product_id desc

SELECT s1.name
FROM friends f1
JOIN packages p1 ON f1.id = p1.id
JOIN friends f2 ON f1.friend_id = f2.id
JOIN packages p2 on f2.id = p2.id
join students s1 on f1.id = s1.id
WHERE p1.salary < p2.salary
ORDER BY p2.salary
SELECT s.name
FROM students s
JOIN (friends f, packages ps, packages pf)
ON s.id = f.id
AND s.id = ps.id
AND f.friend_id = pf.id
WHERE ps.salary < pf.salary
ORDER BY pf.salary
FIRST_HALF (FLAVOR PK, SHIPMENT_ID FK)

JULY (SHIPMENT_ID PK, FLAVOR FK)

7월에 주문량 폭주로 동일한 맛 아이스크림이 여러 번 출하됐다.
f.total_order + SUM(j.total_order) f.total_order까지 SUM하면 중복 ROW 때문에 값 뻥튀기https://school.programmers.co.kr/learn/courses/30/lessons/157339
SELECT a.car_id, a.car_type,
ROUND(
a.daily_fee * 30 * (1 - (p.discount_rate / 100)),
0
) AS FEE
FROM car_rental_company_car a
LEFT JOIN car_rental_company_discount_plan p
ON a.car_type = p.car_type
WHERE
a.car_type IN ('세단', 'SUV') AND
p.duration_type = '30일 이상' AND
a.daily_fee * 30 * (1 - (p.discount_rate / 100)) BETWEEN 500000 AND 2000000 AND
a.car_id NOT IN (
SELECT car_id
FROM car_rental_company_rental_history
WHERE '2022-11-01' <= end_date AND start_date < '2022-12-01'
)
ORDER BY FEE DESC, a.car_type, a.car_id DESC
select distinct(city) from station where city LIKE "%a" or city LIKE "%e" or city LIKE "%i" or city LIKE "%o" or city LIKE "%u"
select distinct(city) from station where city REGEXP ('a$|e$|i$|o$|u$')
select distinct(city) from station where city REGEXP '[aeiou]$'
select distinct(city) from station where city REGEXP '^[aeiou]' and city REGEXP '[aeiou]$'
.: any character*: 일반 LIKE 쓰듯이 쓰면 안됨. 앞에 Matching pattern 필요.select distinct(city) from station where city REGEXP '^[aeiou].*[aeiou]$'
| 는 OR 이므로, 이렇게 할 수 없음select distinct(city) from station where city REGEXP '^[aeiou]|[aeiou]$'
select distinct(city) from station where city REGEXP '^[aeiou][A-z]*[aeiou]$'
select distinct(city) from station where city REGEXP '^[^aeiou]'
SELECT round(13.12345, 4); -- 숫자 없으면, 정수로
SELECT truncate(13.12345, 4); -- 숫자 아래로 삭제 (floor)
SELECT floor(13.12345); -- 정수로만 가능
SELECT ceil(13.12345); -- 정수로만 가능
WHERE date1 BETWEEN '2022-09-01' and '2022-09-26' -- 0시 기준이므로 26일 비포함
WHERE date1 BETWEEN '2022-09-01 00:00:00' and '2022-09-26 23:59:59'
DATEDIFF(date1, date2)
DATE_ADD(date1, INTERVAL 100 DAY); -- SECOND,MINUTE,HOUR
DATE_ADD(date1, INTERVAL 1 MONTH); -- DAY,MONTH,YEAR
DATE_FORMAT(날짜, 형식)
| 기호 | 역할 | 기호 | 역할 |
|---|---|---|---|
| %Y | 4자리 연도 | %y | 2자리 연도 |
| %m | 두자리 숫자 월 | %c | 한자리 숫자 월 |
| %M | 긴 영어 월 | %b | 짧은 영어 월 |
| %d | 두자리 일 | %e | 한자리 일 |
| %W | 긴 영어 요일 | %a | 짧은 영어 요일 |
| %I | 12시간제 시 | %H | 24시간제 시 |
| %i | 분 | %S | 초 |
| %T | hh:mm:ss | %r | hh:mm:ss AM/PM |
| %p | AM/PM |