SELECT Country FROM Customers
GROUP BY Country;
💡 여러 컬럼을 기준으로 그룹화할 수도 있다.
SELECT 컬럼 FROM 테이블 GROUP BY 그룹화할 컬럼;
SELECT
Country, City,
CONCAT_WS(', ', City, Country)
FROM Customers
GROUP BY Country, City;
select count(*), OrderDate
from Orders
GROUP By OrderDate;
⚠️ ORDER BY 와는 함께 사용될 수 없습니다.
SELECT
Country, COUNT(*)
FROM Suppliers
GROUP BY Country
WITH ROLLUP;
SELECT
Country, COUNT(*) AS Count
FROM Suppliers
GROUP BY Country
HAVING Count >= 3;
SELECT
COUNT(*) AS Count, OrderDate
FROM Orders
WHERE OrderDate > DATE('1996-12-31')
GROUP BY OrderDate
HAVING Count > 2;
SELECT DISTINCT CategoryID
FROM Products;
-- 위의 GROUP BY를 사용한 쿼리와 결과 비교
💡 GROUP BY와 DISTINCT 함께 활용하기
SELECT
Country,
COUNT(DISTINCT CITY)
FROM Customers
GROUP BY Country;