❓ Question ❗ Answer SELECT p.member_name, r.review_text, DATE_FORMAT(r.review_date,'%Y-%m-%d') FROM member_profile p INNER JOIN rest_review r ON p.member_id = r.member_id WHERE p.member_id IN (SELECT member_id FROM rest_review GROUP BY member_id HAVING COUNT(member_id) = (SELECT MAX(cnt) FROM (SELECT COUNT(member_id) cnt FROM rest_review GROUP BY member_id) a)) ORDER BY review_date, review_text;..
❓ Question ❗ Answer SELECT MONTH(start_date) month, car_id, COUNT(car_id) RECORDS FROM car_rental_company_rental_history WHERE car_id IN (SELECT car_id FROM car_rental_company_rental_history WHERE start_date BETWEEN '2022-08-01' AND '2022-10-31' GROUP BY car_id HAVING COUNT(car_id) >= 5) AND start_date BETWEEN '2022-08-01' AND '2022-10-31' GROUP BY month, car_id HAVING RECORDS IS NOT NULL ORDER ..
❓ Question ❗ Answer SELECT CONCAT('/home/grep/src/',board.board_id,'/',file.file_id,file.file_name,file.file_ext) FILE_PATH FROM used_goods_board board INNER JOIN used_goods_file file ON board.board_id = file.board_id WHERE board.views = (SELECT MAX(views) FROM used_goods_board) ORDER BY file.file_id DESC; 📌 Discussion - INNER JOIN을 활용하여 같은 게시물 ID를 사용하는 모든 파일 레코드 호출 - sub-query를 scalar 형태로 사용하여 ..
❓ Question ❗ Answer SELECT f.flavor FROM first_half f INNER JOIN july j ON f.flavor = j.flavor GROUP BY f.flavor ORDER BY SUM(f.total_order+j.total_order) DESC LIMIT 3;📌 Discussion flavor로 join하여 shipment_id가 다르거나, shipment_id가 같더라도 테이블이 다르면 서로 다른 레코드로 취급함 group by로 같은 flavor끼리 묶어서 위 조건에 해당되는 서로 다른 레코드를 합계
❓ Question ❗ Answer # GROUP_CONCAT 활용 SELECT cart_id FROM cart_products GROUP BY cart_id HAVING GROUP_CONCAT(name) LIKE '%Milk%' AND GROUP_CONCAT(name) LIKE '%Yogurt%' ORDER BY cart_id; # sub query 활용 SELECT DISTINCT cart_id FROM cart_products WHERE name = 'Milk' AND cart_id in (SELECT cart_id FROM cart_products WHERE name = 'Yogurt') ORDER BY cart_id; # IN, DISTN..
❓ Question ❗ Answer SELECT * FROM places WHERE host_id IN (SELECT host_id FROM places GROUP BY host_id HAVING COUNT(*)>=2) ORDER BY id;📌 Discussion sub query 활용 하여 조건에 맞는 host_id에 해당하는 데이터만 출력