본문 바로가기

having4

[leetcode / MySQL] 585. Investments in 2016 문제 💡 Write a solution to report the sum of all total investment values in 2016 tiv_2016, for all policyholders who: have the same tiv_2015 value as one or more other policyholders, and are not located in the same city as any other policyholder (i.e., the (lat, lon) attribute pairs must be unique). Round tiv_2016 to two decimal places. 테이블 형태 풀이 SELECT ROUND(SUM(tiv_2016),2) AS tiv_2016 FROM Insu.. 2024. 2. 22.
[leetcode / MySQL] 570. Managers with at Least 5 Direct Reports 문제 💡 Write a solution to find managers with at least five direct reports. Return the result table in any order. 테이블 형태 풀이 SELECT e2.name FROM Employee e1 INNER JOIN Employee e2 ON e1.managerId=e2.id GROUP BY e1.managerId HAVING COUNT(DISTINCT e1.id) >= 5 해설 SELECT e2.name FROM Employee e1 INNER JOIN Employee e2 ON e1.managerId=e2.id GROUP BY e1.managerId HAVING COUNT(DISTINCT e1.id) >= 5 최소 5명 이.. 2024. 1. 31.
[프로그래머스 / MySQL] 동명 동물 수 찾기 문제 💡 동물 보호소에 들어온 동물 이름 중 두 번 이상 쓰인 이름과 해당 이름이 쓰인 횟수를 조회하는 SQL문을 작성해주세요. 이때 결과는 이름이 없는 동물은 집계에서 제외하며, 결과는 이름 순으로 조회해주세요. 테이블 형태 풀이 SELECT NAME, COUNT(*) AS COUNT FROM ANIMAL_INS WHERE NAME IS NOT NULL GROUP BY NAME HAVING COUNT >= 2 ORDER BY NAME 해설 FROM ANIMAL_INS WHERE NAME IS NOT NULL 두 번 이상 쓰인 이름과 사용 횟수를 출력하는 문제다 이름이 NULL 인 값은 집계에서 제외해야 하므로 WHERE 절에서 IS NOT NULL 조건을 설정했다(중요) SELECT NAME, COUN.. 2024. 1. 31.
[leetcode / MySQL] 1045. Customers Who Bought All Products 문제 💡 Write a solution to report the customer ids from the Customer table that bought all the products in the Product table. Return the result table in any order. 테이블 형태 풀이 SELECT customer_id FROM Customer GROUP BY customer_id HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(*) FROM Product) 해설 SELECT customer_id FROM Customer GROUP BY customer_id HAVING COUNT(DISTINCT product_key) = (SELECT CO.. 2024. 1. 30.