
SELECT *
FROM table1 a
LEFT JOIN table2 b ON a.id = b.id;
NULL)avg(if(rating<>'Not given', rating, null))로 ‘Not given’ 제거coalesce(age, 20) 으로 null → 20으로 대체SELECT name, coalesce(age, 20) AS filled_age
FROM customers;
CASE WHEN age<15 THEN 15 WHEN age>80 THEN 80 ELSE age END 로 정제SELECT restaurant_name,
MAX(IF(hh='15', cnt_order, 0)) AS "15",
...
FROM (
SELECT a.restaurant_name, SUBSTRING(b.time, 1, 2) AS hh, COUNT(*) AS cnt_order
FROM food_orders a
INNER JOIN payments b ON a.order_id=b.order_id
WHERE SUBSTRING(b.time, 1, 2) BETWEEN 15 AND 20
GROUP BY 1, 2
) a
GROUP BY 1
ORDER BY "20" DESC;
SELECT age_group,
MAX(IF(gender='male', order_count, 0)) AS male,
MAX(IF(gender='female', order_count, 0)) AS female
FROM (
SELECT gender,
CASE
WHEN age BETWEEN 10 AND 19 THEN 10
...
END AS age_group,
COUNT(*) AS order_count
FROM food_orders a
INNER JOIN customers b ON a.customer_id = b.customer_id
WHERE age BETWEEN 10 AND 59
GROUP BY 1, 2
) t
GROUP BY 1
ORDER BY age_group;
SELECT cuisine_type, restaurant_name, order_count,
RANK() OVER (PARTITION BY cuisine_type ORDER BY order_count DESC) AS rn
FROM (
SELECT cuisine_type, restaurant_name, COUNT(*) AS order_count
FROM food_orders
GROUP BY 1, 2
) a
WHERE rn <= 3
ORDER BY cuisine_type, rn;
SELECT cuisine_type, restaurant_name, order_count,
SUM(order_count) OVER (PARTITION BY cuisine_type) AS total,
SUM(order_count) OVER (PARTITION BY cuisine_type ORDER BY order_count) AS cum_sum
FROM (
SELECT cuisine_type, restaurant_name, COUNT(*) AS order_count
FROM food_orders
GROUP BY 1, 2
) a;
SELECT DATE(date) AS date_type,
DATE_FORMAT(DATE(date), '%Y') AS "년",
DATE_FORMAT(DATE(date), '%m') AS "월",
DATE_FORMAT(DATE(date), '%d') AS "일",
DATE_FORMAT(DATE(date), '%w') AS "요일"
FROM payments;
SELECT DATE_FORMAT(DATE(date), '%Y') AS y,
DATE_FORMAT(DATE(date), '%m') AS m,
COUNT(*) AS order_count
FROM food_orders a
INNER JOIN payments b ON a.order_id = b.order_id
WHERE DATE_FORMAT(DATE(date), '%m') = '03'
GROUP BY 1, 2
ORDER BY 1;
AVG, JOIN 결과가 왜곡됨Pivot, RANK, 누적합이 가능하다는 점COALESCE, IF, CASE, DATE_FORMAT이 실무에서 매우 유용함ROW_NUMBER, LEAD, LAG)월별, 주별, 요일별 트렌드 분석 연습
select cuisine_type,
max(if(age=10, order_count, 0)) "10대",
max(if(age=20, order_count, 0)) "20대",
max(if(age=30, order_count, 0)) "30대",
max(if(age=40, order_count, 0)) "40대",
max(if(age=50, order_count, 0)) "50대"
from
(
select a.cuisine_type,
case when age between 10 and 19 then 10
when age between 20 and 29 then 20
when age between 30 and 39 then 30
when age between 40 and 49 then 40
when age between 50 and 59 then 50 end age,
count(1) order_count
from food_orders a inner join customers b on a.customer_id=b.customer_id
where age between 10 and 59
group by 1, 2
) t
group by 1