select category,count(category)
from(SELECT left(product_code,2) as 'category'
from product) as sub
group by category
SUBSTRING(문자열, 시작위치, 길이)
select substring('hello world',1,5)
-> helloLEFT(문자열, 길이)
select left('hello world',5)
-> helloRIGHT(문자열, 길이)
select left('hello world',5)
-> world특정 칼럼들의 데이터를 기준으로 그룹을 나눠주는 기능
주로 집계함수와 함께 사용하여 집계데이터(평균, 최대, 최소, 개수 등)를 구하는 데 사용
SELECT substring(product_code,1,2) as 'category'
from product
group by category
SELECT left(product_code,2) as 'category',count(PRODUCT_CODE) as products
from product
group by category
SELECT substring(product_code,1,2) as 'category',count(PRODUCT_CODE) as products
from product
group by category
SELECT left(product_code,2) as CATEGORY,count(*) as PRODUCTS
from product
group by CATEGORY
order by 1
Every derived table must have its own alias
SELECT substring(product_code,1,2) as 'category',count(category) as products
from product
group by category
SELECT 문에서 정의된 별칭은 GROUP BY 또는 HAVING에서만 사용할 수 있고, COUNT() 같은 집계 함수에서는 사용할 수 없음!