06. GROUP BY
GROUP BY?
์นผ๋ผ์ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฌถ์ ๋ ์ฌ์ฉ
๊ทธ๋ฃนํจ์๊ฐ ์์ด์ผ๋ง ์ฌ์ฉ ๊ฐ๋ฅ
๋๋๊ณ ์ ํ๋ ๊ทธ๋ฃน์ ์นผ๋ผ๋ช ์ SELECT์ ๊ณผ GROUP BY์ ๋ค์ ์ถ๊ฐํ๋ฉด ๋๋ค.
์ง๊ณํจ์์ ํจ๊ป ์ฌ์ฉ๋๋ ์์๋ GROUP BY ์ ์ ์ถ๊ฐํ์ง ์์๋ ๋๋ค.
* '๋ถ์๋ณ ๊ธ์ฌํ๊ท ' ์ฒ๋ผ '~๋ณ' ์ ๊ตฌํ๋ค.
๊ทธ๋ฃนํจ์(=์ง๊ณํจ์)
GROUP BY์ ์ ์ด์ฉํ์ฌ ๊ทธ๋ฃน ๋น ํ๋์ ๊ฒฐ๊ณผ๋ก ๊ทธ๋ฃนํ
HAVING์ ์ ์ฌ์ฉํ์ฌ ์ง๊ณํจ์๋ฅผ ์ด์ฉํ ์กฐ๊ฑด ๋น๊ต ๊ฐ๋ฅ
WHERE ์ ์์๋ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
ROUND(์์,๋ฐ์ฌ๋ฆผํ ์๋ฆฌ ์) : ์์์ ๋ฐ์ฌ๋ฆผ
TRUNCATE(์์,์ ์ญํ ์๋ฆฌ ์) : ์์์ ์๋ ๋จ์ ์ ์ญ
* ์ ์ญํ ์๋ฆฌ ์์ ์์ ์ง์ ๊ฐ๋ฅ
ABS() : ์ ๋๊ฐ
COUNT() : ํ์ ๊ฐ์
SUM() : ์ด ํฉ
MIN() : ์ต์๊ฐ
MAX() : ์ต๋๊ฐ
AVG() : ํ๊ท ๊ฐ
STDDEV : ํ์คํธ์ฐจ
* MIN, MAX ํจ์๋ ๋ชจ๋ ์๋ฃํ์ ์ฌ์ฉ ํ ์ ์๋ค.
HAVING
GROUP BY์ ์์ ์กฐ๊ฑด์ง์ ์ ์ด์ฉ ๊ฐ๋ฅํ ์กฐ๊ฑด๋ฌธ
์ง๊ณํจ์๋ฅผ ๊ฐ์ง๊ณ ์กฐ๊ฑด๋น๊ต๋ฅผ ํ ๋ ์ฌ์ฉ
[ํด์ฆ] GROUP BY ๊ธฐ์ด ๋ฌธ์
1. ๋ค์ GROUP BY ์ง๊ณ์ฟผ๋ฆฌ์ ์๋ฏธ๋ฅผ ์ค๋ช
ํ์ธ์.
2. ์ฟผ๋ฆฌ์ ๋ฌธ์ ์ ์ ์ค๋ช
ํ๊ณ
3. ๋ฌธ์ ๋ฅผ ํด๊ฒฐํด ๋ณด์ธ์.
SELECT ๋ถ์, SUM(๊ธ์ฌ) ๊ธ์ฌ
FROM ์ฌ์
HAVING COUNT(*) > 3
GROUP BY ๋ถ์
ORDER BY SUM(๊ธ์ฌ) DESC
;
1. ์ฌ์์ด 3๋ช ์ด ๋๋ ๋ถ์ ๋ณ, ๊ธ์ฌ ํฉ๊ณ๋ฅผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ๊ตฌํ๊ธฐ ์ํ ์ฟผ๋ฆฌ๋ฌธ
2. ์ง๊ณํจ์ ์ค์ฒฉ์ฌ์ฉ์ผ๋ก erorr ๋ฐ์
SELECT์ ์ SUM(๊ธ์ฌ)์์ (๊ธ์ฌ)๋ ์ฌ์ ํ ์ด๋ธ์ ๊ธ์ฌ ์นผ๋ผ, ORDER BY์ ์ SUM(๊ธ์ฌ)์์์ (๊ธ์ฌ)๋ SELECT ์ ์ SUM(๊ธ์ฌ)์ ๋ํ ๋ณ์นญ์ ์๋ฏธ. ์ฆ ORDER BY SUM(๊ธ์ฌ) >> ORDER BY SUM(SUM(๊ธ์ฌ))
3. ORDER BY์ ์ SUM(์ฌ์.๊ธ์ฌ) DESC๋ก ์์ ํ์ฌ ํ ์ด๋ธ๋ช ๋ช ์
* GROUP BY ์ HAVING ์ ์ ์์๊ฐ ๋ฐ๋์ด๋ ๋์ง๋ง ์์น ๋ฐ๋ฅด๊ธฐ๋ฅผ ๊ถ์ฅ
[ํด์ฆ2] https://www.bullraider.com/quiz/sql-quiz/sql-group-by-order-by-and-having
The Oracle server implicitily sort the ResultSet in ascending order by using Group By clause .
- true
- false
Order by is to sort ResultSet either in ascending or descending order .
- true
- false
Which statements are true about Group By ?
- Group By is used to group set of values based on one or more values
- Group By is used when we use aggregate functions
- Group by does not guaranteed sorts the data in any order
- All of above
How to select Maximum Salary of Employee from each Department ?
- Select deptno, sal from Emp Group By deptno Order By Max(Sal);
- Select deptno, sal from Emp Group By deptno Order By Max(Sal);
- Select dept_id, sal from Emp Group By dept_id Order By Max(Sal);
- Select deptno, max(sal) from Emp Group By deptno;
'Having' clause is used with
- Group By
- Order By
- Both of above
What is the correct format to select Emp_name in Ascending order ?
- Select Emp_name From Emp ;
- Select Emp_name From Emp Group By Emp_name ;
- Select Emp_name From Emp Order By Emp_name ;
- All of above
What is the correct format to select Emp_name in Ascending order ?
- Select Emp_name From Emp ;
- Select Emp_name From Emp Group By Emp_name ;
- Select Emp_name From Emp Order By Emp_name ;
- All of above
How o select number of employee department wise from Emp table ?
- Select deptno, count(*) from Emp Group By deptno;
- Select empno,deptno, count(*) from Emp Group By deptno;
- Select deptno, count(*) from Emp Order By deptno;
- Select empno, count(*) from Emp Order By deptno;
Which of the following is correct to count the total salary in deptno wise where more than two employees exist ?
- SELECT deptno, sum(sal) As totalsal FROM emp GROUP BY deptno HAVING COUNT(empno) = 2
- SELECT deptno, sum(sal) As totalsal FROM emp GROUP BY deptno where COUNT(empno) = 2
- SELECT deptno, sum(sal) As totalsal FROM emp GROUP BY deptno HAVING COUNT(empno) > 2
- SELECT deptno, sum(sal) As totalsal FROM emp GROUP BY deptno where COUNT(empno) > 2
Both 'having' and 'where' clause can be used in aggregate functions.
- true
- false
[์ ๋ต] a a d d a c c a c b
์ฐธ๊ณ ํ ๊ฐ๋ ์ดํด ์์ https://youtu.be/Yvuw0dbd7OQ