From 50b05109f1ea341116e3c888804b0a5beac1698c Mon Sep 17 00:00:00 2001 From: kmd192kmd Date: 2026年4月11日 21:19:39 +0900 Subject: [PATCH 1/6] =?UTF-8?q?1.select,=202.where=20=EC=97=B0=EC=8A=B5?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice/Script-1-select.sql | 59 ++++++++++++++++++++++++++++++++++ practice/Script-2-where.sql | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 practice/Script-1-select.sql create mode 100644 practice/Script-2-where.sql diff --git a/practice/Script-1-select.sql b/practice/Script-1-select.sql new file mode 100644 index 0000000..78ee74e --- /dev/null +++ b/practice/Script-1-select.sql @@ -0,0 +1,59 @@ +--01.SELECT기초 + +select * from products; +select name, price, stock_qty from products; +select + name as product_name, price as unit_price, stock_qty as in_stock +FROM products; +select name, price * 1.1 as price_with_tax from products; +select distinct grade from customers; +select distinct gender from customers; +select distinct is_active as status +from customers +order by is_active; + +--1번 +select name as full_name, email as email_address, grade as mebership_tier from customers; + +--2번 +select DISTINCT method from payments; + +--3번 +select name, price, stock_qty, price * stock_qty as inventory_value from products; + +--4번 +select * from categories; + +--5번 +select department, role, name from staff; + +--6번 +select DISTINCT status from orders; + +--7번 +select name, price, price * 0.9 as discounted_price from products; + +--8번 +select name, price, 'KRW' as currency FROM products; + +--9번 +select distinct contact_name, company_name from suppliers; + +--10번 +select name, price, cost_price, price - cost_price as margin, (price - cost_price) / price * 100 as margin_pct from products; + + + + + + + + + + + + + + + + diff --git a/practice/Script-2-where.sql b/practice/Script-2-where.sql new file mode 100644 index 0000000..600b718 --- /dev/null +++ b/practice/Script-2-where.sql @@ -0,0 +1,61 @@ +--02.WHERE로 데이터 필터링 + +select name, price from products where price> 500; +select name, price, stock_qty from products where is_active = 1; +select name, price from products where is_active = 1 and price>= 100 and price <= 500; +select name, email, grade from customers where grade = 'VIP' or grade = 'GOLD'; +select name, grade from customers where grade in ('GOLD', 'VIP'); +select name, price from products where price BETWEEN 50 and 200; +select order_number, ordered_at, total_amount from orders where ordered_at between '2024-01-01' and '2024-03-31 23:59:59'; +select name, price from products where name like '%Gaming%'; +select name, email from customers where email like '%@testmail.kr'; +select name, email from customers where birth_date is null; +select order_number, notes from orders where notes is not null; + +--1번 +select name, grade, point_balance from customers where gender = 'F' and grade in ('SILVER', 'GOLD'); + +--2번 +select name, price from products where is_active = 1 and price between 200 and 800 order by price desc; + +--3번 +select name, created_at from customers where gender is null and last_login_at is null; + +--4번 +select name, price from products where price>= 1000; + +--5번 +select name, stock_qty from products where stock_qty != 0; + +--6번 +select name, point_balance from customers where grade = 'GOLD' and point_balance between 500 and 2000; + +--7번 +select order_number, status from orders where status in('pending', 'processing'); + +--8번 +select name, price from products where name like '%Keyboard'; + +--9번 +select name, department from staff where is_active = 1 and department 'Sales'; + +--10번 +select name, grade, point_balance, is_active from customers where (grade = 'VIP' and is_active = 0) or point_balance>= 5000; + + + + + + + + + + + + + + + + + + From f7e9fc475125168dec89f23f328bb2d76b7468e6 Mon Sep 17 00:00:00 2001 From: kmd192kmd Date: 2026年4月12日 12:33:12 +0900 Subject: [PATCH 2/6] =?UTF-8?q?3.order&pagination,=204.aggregateFunction?= =?UTF-8?q?=20=EB=AC=B8=EC=A0=9C=ED=92=80=EC=9D=B4=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice/Script-3-order&pagination.sql | 48 +++++++++++++++++ practice/Script-4-aggregateFunctions.sql | 65 ++++++++++++++++++++++++ serve.bat | 2 +- 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 practice/Script-3-order&pagination.sql create mode 100644 practice/Script-4-aggregateFunctions.sql diff --git a/practice/Script-3-order&pagination.sql b/practice/Script-3-order&pagination.sql new file mode 100644 index 0000000..d816ba1 --- /dev/null +++ b/practice/Script-3-order&pagination.sql @@ -0,0 +1,48 @@ +--03.정렬과 페이지네이션 +select name, price from products where is_active = 1 order by price asc; +select name, price from products where is_active = 1 order by price desc; +select name, grade, point_balance from customers where is_active = 1 order by grade asc, name asc; +select order_number, ordered_at, total_amount from orders order by ordered_at desc, total_amount desc; +select name, price from products where is_active = 1 order by price desc limit 5; +select name, price from products where is_active = 1 order by name asc limit 10 offset 0; +select name, price from products where is_active = 1 order by name asc limit 10 offset 10; +select name, price from products where is_active = 1 order by name asc limit 10 offset 20; +select name, birth_date from customers order by birth_date asc limit 5; + +--1번 +select order_number, ordered_at, status, total_amount from orders order by ordered_at desc limit 10; + +--2번 +select name, stock_qty, price from products order by stock_qty asc, price desc limit 20; + +--3번 +select name, price, stock_qty from products where is_active = 1 order by name asc limit 10 offset 20; + +--4번 +select name, grade, point_balance from customers order by point_balance desc limit 5; + +--5번 +select name, price from products order by price asc, name asc; + +--6번 +select name, price, cost_price from products order by price - cost_price desc; + +--7번 +select product_id, rating, created_at from reviews order by created_at desc limit 5 offset 5; + +--8번 +select name, department, hired_at from staff order by department asc, hired_at asc; + +--9번 +select name, birth_date from customers order by birth_date is null asc, birth_date asc; + +--10번 +select order_number, total_amount, ordered_at from orders order by total_amount desc, ordered_at desc limit 15; + + + + + + + + diff --git a/practice/Script-4-aggregateFunctions.sql b/practice/Script-4-aggregateFunctions.sql new file mode 100644 index 0000000..d400319 --- /dev/null +++ b/practice/Script-4-aggregateFunctions.sql @@ -0,0 +1,65 @@ +--04.집계함수 +select count(*) as total_customers from customers; +select count(*) as total_customers, count(birth_date) as with_birth_date, count(*) - count(birth_date) as missing_birth_date from customers; +select sum(total_amount) as total_revenue from orders where status in ('delivered', 'confirmed'); +select sum(point_balance) as total_points_outstanding from customers where is_active = 1; +select avg(price) as avg_price, avg(stock_qty) as avg_stock from products where is_active = 1; +select avg(total_amount) as avg_order_value from orders where status not in ('cancelled', 'returned'); +select min(price) as cheapest, max(price) as most_expensive from products where is_active = 1; +select min(ordered_at) as first_order, max(ordered_at) as lastest_order from orders; +select count(*) as total_reviews, avg(rating) as avg_rating, min(rating) as lowest_rating, max(rating) as highest_rating, + sum(case when rating = 5 then 1 else 0 end) as five_star_count from reviews; + +--1번 +select round(avg(rating), 2) as avg_rating FROM reviews; + +--2번 +select sum(total_amount) as total_revenue from orders where status in ('delivered', 'confirmed'); + +--3번 +select count(*) as total_customers, count(birth_date) as with_birth_date from customers; + +--4번 +select count(*) as active_product_count, sum(price * stock_qty) as total_inventory_value from products where is_active = 1; + +--5번 +select avg(total_amount) as avg_order, min(total_amount) as min_order, max(total_amount) as max_order from orders where status not in ('cancelled', 'returned', 'returned_requested'); + +--6번 +select round(avg(price), 0) as avg_price, round(avg(cost_price), 0) as avg_cost, round(avg((price - cost_price) / price * 100), 1) as avg_margin_pct from products where is_active = 1; +PRAGMA table_info(products); + +--7번 +select min(price) as min_price, max(price) as max_price, max(price) - min(price) as price_range from products where is_active = 1; + +--8번 +select count(*) as total_items, sum(quantity) as total_qty, round(avg(unit_price), 2) as avg_unit_price, max(quantity) as max_qty from order_items; + +--9번 +select count(*) as total_items, sum(amount) as total_amount, round(avg(amount), 0) as avg_amount, min(amount) as min_amount, max(amount) as max_amount +from payments +where status = 'completed'; + +--10번 +select + count(case when notes is not null then 1 end) as orders_with_notes, + count(*) as total_orders, + round(100.0 * count(case when notes is not null then 1 end) / count(*), 1) as pct_with_notes +from orders; + + + + + + + + + + + + + + + + + diff --git a/serve.bat b/serve.bat index 695eb74..e5bc419 100644 --- a/serve.bat +++ b/serve.bat @@ -25,5 +25,5 @@ if "%LANG%"=="all" ( python -m http.server 8001 ) else ( cd /d "%~dp0docs" - mkdocs serve -f mkdocs-%LANG%.yml -a localhost:8001 + python -m mkdocs serve -f mkdocs-%LANG%.yml -a localhost:8001 ) From d0c3caf94eee2e1052099aa7ee749198422b7aec Mon Sep 17 00:00:00 2001 From: kmd192kmd Date: 2026年4月19日 10:31:31 +0900 Subject: [PATCH 3/6] =?UTF-8?q?05.groupBy=20=EC=97=B0=EC=8A=B5=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice/Script-5-groupBy.sql | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 practice/Script-5-groupBy.sql diff --git a/practice/Script-5-groupBy.sql b/practice/Script-5-groupBy.sql new file mode 100644 index 0000000..f6209b7 --- /dev/null +++ b/practice/Script-5-groupBy.sql @@ -0,0 +1,58 @@ +--05.GROUP BY와 HAVING +select grade, count(*) as customer_count from customers group by grade; +select status, count(*) as order_count, SUM(total_amount) as total_revenue from orders group by status order by total_revenue desc; +select grade, gender, count(*) as cnt from customers where gender is not null group by grade, gender order by grade, gender; +select substr(ordered_at, 1, 7) as year_month, count(*) as order_count, sum(total_amount) as monthly_revenue from orders + where ordered_at like '2024%' group by substr(ordered_at, 1, 7) order by year_month; +select grade, count(*) as customer_count from customers group by grade having count(*)> 500; +select category_id, count(*) as product_count, avg(price) as avg_price from products where is_active = 1 + group by category_id having count(*)>= 10 and avg(price)> 100 order by avg_price desc; +select grade, avg(point_balance) as avg_points from customers where is_active = 1 group by grade having avg(point_balance)> 500; + +--1번 +select status, count(*) as total_count from orders group by status having total_count> 500 order by total_count desc; + +--2번 +select method, sum(amount) as total_amount, count(*) as total_count from payments where status = 'completed' group by method order by total_amount desc; + +--3번 +select grade, avg(point_balance) as avg_points from customers group by grade order by avg_points desc; + +--4번 +select grade, gender, count(*) as customer_count from customers group by grade, gender order by grade, gender; + +--5번 +select rating, count(*) as review_count from reviews group by rating having count(*)>= 100 order by rating; + +--6번 +select status, count(*) as order_count, round(avg(total_amount)) as avg_amount from orders + where status not in ('cancelled', 'returned') group by status having avg_amount> 300 order by avg_amount; + +--7번 +select substr(ordered_at, 1, 7) as year_month, sum(total_amount) as monthly_revenue from orders where ordered_at>= '2023-01-01' and ordered_at < '2025-01-01' and status not in ('cancelled', 'returned') + group by substr(ordered_at, 1, 7) having sum(total_amount)> 500000 order by year_month; + +--8번 +select method, count(distinct order_id) as unique_orders from payments group by method order by count(distinct order_id) desc; + +--9번 +select substr(ordered_at, 1, 4) as order_year, count(*) as order_count, sum(total_amount) as yearly_revenue from orders where status not in ('cancelled', 'returned') group by substr(ordered_at, 1, 4); + +--10번 +select category_id, count(*) as product_count, round(avg(price)) as avg_price, sum(stock_qty) as total_stock from products + group by category_id having avg_price>= 50 and product_count>= 5 order BY total_stock desc; + + + + + + + + + + + + + + + From 2a7619aeb38dbc7e2cf5e4ddbe136e2c243fa537 Mon Sep 17 00:00:00 2001 From: kmd192kmd Date: 2026年4月19日 23:06:23 +0900 Subject: [PATCH 4/6] =?UTF-8?q?06.null=20=EC=97=B0=EC=8A=B5=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice/Script-6-null.sql | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 practice/Script-6-null.sql diff --git a/practice/Script-6-null.sql b/practice/Script-6-null.sql new file mode 100644 index 0000000..87c2ea7 --- /dev/null +++ b/practice/Script-6-null.sql @@ -0,0 +1,58 @@ +--06.NULL +select name from customers where birth_date = null; +select name from customers where birth_date is null; +select name, gender from customers where gender is not null limit 5; +select order_number, total_amount from orders where notes is null limit 5; +select order_number, status from orders where staff_id is null and status in ('return_requested', 'returned', 'complaints') limit 5; +select name, coalesce(gender, '미입력') as gender_display from customers limit 8; +select order_number, coalesce(notes, '특이사항 없음') as delivery_note from orders limit 5; +select grade, count(*) as total, count(case when is_active = 0 then 1 end) as inactive, + round(100.0 * count(case when is_active = 0 then 1 end)/nullif(count(*),0),1) as pct_inactive from customers group by grade; +select count(*) as all_customers, count(birth_date) as customers_with_dob, avg(cast(substr(birth_date, 1, 4) as INTEGER)) as avg_birth_year from customers; +select 1+NULL, null*100, 'hello' || NULL; +select name, birth_date, coalesce(cast((julianday('now')-julianday(birth_date))/365.25 as INTEGER), -1) as age_years from customers limit 5; + +--1번 +SELECT name, department, role from staff where manager_id is null; + +--2번 +select name, coalesce(email, '연락처 없음') as email from customers where phone is null; + +--3번 +select name, price, stock_qty, price / nullif(stock_qty, 0) as price_per_unit FROM products limit 5; + +--4번 +select name, coalesce(email, '없음') as email, COALESCE(created_at, '알 수 없음') as created_at from customers where last_login_at is null limit 10; + +--5번 +select order_number, status, coalesce(notes, '-') as notes from orders where staff_id is null order by ordered_at desc limit 20; + +--6번 +select grade, count(*) as customer_count, coalesce(gender, 'Unknown') as gender_status from customers group by grade, coalesce(gender, 'Unknown') order by grade, gender_status; + +--7번 +select count(case when cancelled_at is null then 1 end) as not_cancelled, count(case when cancelled_at is not null then 1 end) as cancelled from orders; + +--8번 +select count(*) as total_products, count(case when weight_grams is null then 1 END) as missing_weight, round(100.0 * count(case when weight_grams is null then 1 END)/count(*), 1) from products; + +--9번 +select case when content is null then 'No Content' else 'Has Content' end as content_status, count(*) as review_count, avg(rating) as avg_rating from reviews + group by case when content is null then 'No Content' else 'Has Content' end; + +--10번 +select count(*) as total_customers, count(*) - count(birth_date) as missing_birth_date, count(*) - count(gender) as missing_gender, + sum(case when last_login_at is null then 1 else 0 end) as never_logged_in from customers; + + + + + + + + + + + + + From d3141c3b3f586b2fcb66ae7ce54f6aa04c8b3d6a Mon Sep 17 00:00:00 2001 From: kmd192kmd Date: 2026年5月17日 22:49:52 +0900 Subject: [PATCH 5/6] =?UTF-8?q?7-innerJoin,=208-leftJoin=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice/Script-7-innerJoin.sql | 192 ++++++++++++++++++++++++++ practice/Script-8-leftJoin.sql | 233 ++++++++++++++++++++++++++++++++ 2 files changed, 425 insertions(+) create mode 100644 practice/Script-7-innerJoin.sql create mode 100644 practice/Script-8-leftJoin.sql diff --git a/practice/Script-7-innerJoin.sql b/practice/Script-7-innerJoin.sql new file mode 100644 index 0000000..57558d0 --- /dev/null +++ b/practice/Script-7-innerJoin.sql @@ -0,0 +1,192 @@ +--07.INNER JOIN +select + o.order_number, + c.name as customer_name, + o.status, + o.total_amount +from orders as o +inner join customers as c on o.customer_id = c.id +order by o.ordered_at DESC +limit 5; + +select count(*) as orders_without_customer +from orders o +left join customers c on o.customer_id = c.id +where c.id is null; + +SELECT + oi.id as item_id, + o.order_number, + p.name as product_name, + cat.name as category, + oi.quantity, + oi.unit_price +FROM order_items as oi +inner JOIN orders as o on oi.order_id = o.id +inner join products as p on oi.product_id = p.id +inner join categories as cat on p.category_id = cat.id +order by o.ordered_at desc +limit 6; + +SELECT + cat.name as category, + count(distinct o.id) as order_count, + sum(oi.quantity) as units_sold, + sum(oi.quantity * oi.unit_price) as gross_revenue +from order_items as oi +inner join orders as o on oi.order_id = o.id +inner join products as p on oi.product_id = p.id +inner join categories as cat on p.category_id = cat.id +where o.status in ('delivered', 'confirmed') +group by cat.name +order by gross_revenue desc +limit 8; + +SELECT + c.name as customer_name, + o.order_number, + o.total_amount, + o.ordered_at +from orders as o +inner join customers as c on o.customer_id = c.id +where c.grade = 'VIP' + and o.total_amount> 1000 + and o.ordered_at like '2024%' +order by o.total_amount desc; + +select * from reviews; +select * from customers; +select * from products; +select * from orders; +select * from payments; +select * from shipping; +select * from order_items; +select * from suppliers; + +--1번 +select + r.id as review_id, + c.name as customer_name, + p.name as product_name, + r.rating, + r.created_at +from reviews r +inner join customers c on r.customer_id = c.id +inner join products p on r.product_id = p.id +order by r.rating desc, r.created_at desc +limit 10; + +--2번 +select + p.method, + count(distinct o.customer_id) as unique_customers +from payments p +inner join orders o on p.order_id = o.id +where p.status = 'completed' +group by p.method +order by unique_customers desc; + +--3번 +select + c.name as customer_name, + c.grade, + count(o.id) as order_count, + sum(o.total_amount) as total_spent +from customers c +inner join orders o on c.id = o.customer_id +where o.status not in ('cancelled', 'returned', 'return_requested') +group by c.id, c.name, c.grade +order by total_spent desc +limit 5; + +--4번 +SELECT + o.order_number as order_name, + c.name as customer_name, + o.status +from orders o +inner join customers c on o.customer_id = c.id +where o.status = 'shipped' +order by ordered_at desc +limit 10; + +--5번 +select + o.order_number, + s.carrier, + s.tracking_number, + s.delivered_at +from shipping s +inner join orders o on s.order_id = o.id +where s.status = 'delivered' +order BY delivered_at desc +limit 10; + +--6번 +select + p.name as product_name, + sup.company_name as supplier_name, + oi.quantity, + oi.unit_price +from order_items oi +inner join products p on oi.product_id = p.id +inner join suppliers sup on p.supplier_id = sup.id +order by oi.unit_price desc; + +--7번 +select + sup.company_name, + count(distinct oi.order_id) as order_count, + sum(oi.quantity) as total_qty +from order_items oi +inner join products p on oi.product_id = p.id +inner join suppliers sup on p.supplier_id = sup.id +group by sup.id, sup.company_name +order by total_qty desc; + +--8번 +select + s.name as staff_name, + s.department, + count(o.id) as order_count, + sum(o.total_amount) as total_revenue +from staff s +inner join orders o on s.id = o.staff_id +where o.status not in ('cancelled', 'returned', 'returned_requested') +group by s.id, s.name, s.department +order by total_revenue desc +limit 10; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/practice/Script-8-leftJoin.sql b/practice/Script-8-leftJoin.sql new file mode 100644 index 0000000..5f66e90 --- /dev/null +++ b/practice/Script-8-leftJoin.sql @@ -0,0 +1,233 @@ +--08.left join +SELECT + p.name as product_name, + p.price, + r.rating, + r.created_at as reviewed_at +from products p +left join reviews r on p.id = r.product_id +order by p.name +limit 8; + +SELECT + p.id, + p.name, + p.price +from products p +left join reviews r on p.id = r.product_id +where r.id is null +order by p.name; + +SELECT + c.id, + c.name, + c.email, + c.created_at +from customers c +left join orders o on c.id = o.customer_id +where o.id is null +order by c.created_at desc +limit 10; + +select + p.name as product_name, + p.price, + count(r.id) as review_count, + round(avg(r.rating), 2) as avg_rating +from products p +left join reviews r on p.id = r.product_id +where p.is_active = 1 +group by p.id, p.name, p.price +order by review_count desc +limit 10; + +SELECT + c.name, + c.grade, + count(o.id) as order_count, + coalesce(sum(o.total_amount), 0) as lifetime_value +from customers c +left join orders o on c.id = o.customer_id + and o.status not in ('cancelled', 'returned') +group by c.id, c.name, c.grade +order by lifetime_value desc +limit 8; + +SELECT + o.order_number, + o.status, + o.total_amount, + s.carrier, + s.tracking_number, + p.method as payment_method +from orders o +left join shipping s on s.order_id = o.id +left join payments p on p.order_id = o.id +where o.ordered_at like '2024-12%' +limit 5; + +select * from shipping; + +SELECT + c.name, + c.email, + o.order_number, + o.total_amount +from orders o +right join customers c on c.id = o.customer_id +order by c.name +limit 10; + +SELECT + c.name, + c.email, + o.order_number, + o.total_amount +from customers c +left join orders o on c.id = o.customer_id +order by c.name +limit 10; + +select + c.name, + c.email, + o.order_number, + o.total_amount +from customers c +full outer join orders o on c.id = o.customer_id +order by c.name +limit 5; + +--1번 +select + COALESCE (c.name, '(알 수 없음)') as customer_name, + COALESCE (o.order_number, '(주문 없음)') as order_number, + o.total_amount +from customers c +full outer join orders o on c.id = o.customer_id +order by customer_name +limit 15; + +--2번 +select + count(c.id) as no_review_customers +from customers c +left join reviews r on r.customer_id = c.id +where r.id is null; + +select * from inventory_transactions; + +--3번 +select + p.id as product_id, + p.name, + p.stock_qty +from products p +left join inventory_transactions i on p.id = i.product_id +where p.is_active = 1 and i.id is null; + +--4번 +select + ca.name as category_name, + coalesce(count(p.id), 0) as product_count +from categories ca +left join products p on ca.id = p.category_id +group by ca.id, ca.name +order by product_count desc, category_name asc; + +--5번 +select + c.name as customer_name, + count(o.id) as order_count +from orders o +right join customers c on c.id = o.customer_id +group by c.id, c.name +order by order_count desc +limit 10; + +--6번 +select + sup.company_name, + count(p.id) as product_count, + coalesce(sum(p.stock_qty), 0) as total_stock +from suppliers sup +left join products p on sup.id = p.supplier_id + and p.is_active = 1 +group by sup.id, sup.company_name +order by total_stock desc; + +--7번 +select + p.name as product_name, + p.price, + coalesce(sum(oi.quantity), 0) as units_sold, + count(distinct oi.order_id) as order_appearances +from products p +left join order_items oi on p.id = oi.product_id +group by p.id, p.name, p.price +order by units_sold desc +limit 20; + +--8번 +select + o.order_number, + o.total_amount, + coalesce(pay.method, '미결제') as payments_method, + coalesce(s.carrier, '미배송') as shipping_carrier +from orders o +left join payments pay on pay.order_id = o.id +left join shipping s on s.order_id = o.id +order by o.total_amount desc +limit 10; + +--9번 +select + c.name as customer_name, + c.email as customer_email, + coalesce(o.status, '주문 없음') as last_order_status +from customers c +left join orders o on c.id = o.customer_id + and o.ordered_at = ( + select max(o2.ordered_at) + from orders o2 + where o2.customer_id = c.id + ) +order by c.name asc +limit 15; + +--10번 +SELECT + c.name AS customer_name, + c.email, + COUNT(w.id) AS wishlist_items +FROM customers AS c +LEFT JOIN orders AS o ON c.id = o.customer_id +INNER JOIN wishlists AS w ON c.id = w.customer_id +WHERE o.id IS NULL +GROUP BY c.id, c.name, c.email +ORDER BY wishlist_items DESC; + + + + + + + + + + + + + + + + + + + + + + + + + From 02512c2e0bde9e3d207e9430d6055390d53e81c1 Mon Sep 17 00:00:00 2001 From: kmd192kmd Date: 2026年5月31日 21:36:44 +0900 Subject: [PATCH 6/6] =?UTF-8?q?9=EB=B2=88=20subquery=20=EC=97=B0=EC=8A=B5?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice/Script-9-subquery.sql | 184 +++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 practice/Script-9-subquery.sql diff --git a/practice/Script-9-subquery.sql b/practice/Script-9-subquery.sql new file mode 100644 index 0000000..117ee13 --- /dev/null +++ b/practice/Script-9-subquery.sql @@ -0,0 +1,184 @@ +--09. 서브쿼리 +select name, price +from products +where price> (select avg(price) from products where is_active = 1) and is_active = 1 order by price asc; + +select name, created_at +from customers +where created_at < (select MIN(ordered_at) from orders) limit 5; + +select name, email, grade +from customers +where id IN ( + select distinct customer_id from reviews where rating = 1) order by name; + +SELECT name, price, stock_qty +from products +where id IN ( + select distinct product_id from cart_items) and is_active = 1 order by name; + +select name, price from products +where id NOT IN ( + select distinct product_id from order_items) and is_active = 1; + +select grade, round(avg(avg_order), 2) as avg_order_value +from ( + select c.grade, o.customer_id, avg(o.total_amount) as avg_order + from orders as o + inner join customers as c on o.customer_id = c.id + where o.status not in ('cancelled', 'returned') + group by c.grade, o.customer_id) as customer_avgs +group by grade order by avg_order_value desc; + +SELECT + monthly.year_month, + monthly.revenue, + monthly.order_count +from ( + select + substr(ordered_at, 1, 7) as year_month, + sum(total_amount) as revenue, + count(*) as order_count + from orders + where status not in ('cancelled', 'returned') + group by substr(ordered_at, 1, 7) + ) as monthly +order by revenue desc limit 3; + +select + c.name, + c.grade, + ( + select max(ordered_at) + from orders + where customer_id = c.id + ) as last_order_date +from customers as c +where c.is_active = 1 +order by last_order_date desc limit 8; + +--1번 +select + order_number, + total_amount, + status +from orders +where id not in (select order_id from payments where status = 'completed') +order by total_amount desc +limit 10; + +--2번 +SELECT + order_number, + total_amount +from orders o +where total_amount> (select avg(total_amount) from orders) +order by total_amount desc +limit 10; + +--3번 +select + name as product_name, + price, + (select count(*) from reviews where product_id = p.id) as review_count +from products p +where is_active = 1 +order by review_count desc +limit 10; + +--4번 +select + p.name as product_name, + p.price, + p.category_id +from products p +where p.price> ( + select avg(price) + from products as p2 + where p2.category_id = p.category_id + and p2.is_active = 1 + ) + and p.is_active = 1 +order by p.category_id, p.price desc; + +--5번 +select + name as product_name, + price +from products p +where p.id in (select distinct product_id from wishlists w) + and p.id not in (select distinct product_id from order_items) +order by price desc; + +--6번 +select + c.name as category_name, + price_stats.avg_price +from (select round(avg(price), 2) as avg_price, category_id from products where is_active = 1 group by category_id) as price_stats +inner join categories c on price_stats.category_id = c.id +order by avg_price desc; + +--7번 +select + name as product_name, + price +from products p +where p.id in (select distinct product_id from order_items oi inner join orders o on o.id = oi.order_id inner join customers c on c.id = o.customer_id where c.grade = 'VIP') +order by price desc; + +--8번 +select + c.name, + c.grade, + order_stats.order_count, + order_stats.total_spent +from ( + select + sum(total_amount) as total_spent, + count(*) as order_count, + customer_id + FROM orders + where status in ('delivered', 'confirmed') + group by customer_id + ) as order_stats +inner join customers c on c.id = order_stats.customer_id +order by order_stats.order_count desc +limit 10; + +--9번 +select + customer_id, + order_count +from ( + select + count(*) as order_count, + customer_id + from orders o + group by customer_id + ) c_orders +where order_count> ( + select avg(cnt) + from ( + select count(*) as cnt + from orders o + group by customer_id + ) as avg_calc + ) +order by order_count desc +limit 10; + + + + + + + + + + + + + + + +

AltStyle によって変換されたページ (->オリジナル) /