2024. 4. 19. 17:21ㆍComputerScience/Database
#1
SELECT MAX(salary) "Maximum", MIN(salary) "Minimum", SUM(salary) "Sum", AVG(salary) "Average"
FROM employees
#2
SELECT job_id, MAX(salary) "Maximum", MIN(salary) "Minimum", SUM(salary) "Sum", AVG(salary) "Average"
FROM employees
GROUP BY job_id
#3
SELECT job_id, COUNT(employee_id)
FROM employees
GROUP BY job_id
-- COUNT(*) : 각 그룹의 투플 수를 count
#4
SELECT COUNT(COUNT(MANAGER_ID)) "Number of managers"
FROM employees
Group by MANAGER_ID
-- COUNT(MANAGER_ID) 하면 널 값이 제외됨
* distinct 키워드
SELECT COUNT(distinct manager_id) "Number of managers"
FROM employees
#5
SELECT (MAX(salary) - MIN(salary)) "Differences"
FROM employees
#6
SELECT MANAGER_ID, MIN(salary)
FROM employees
WHERE MANAGER_ID is not null
GROUP BY MANAGER_ID
HAVING MIN(salary) >= 6000
-- where 절에 조건을 주는 것이 더 좋다
-- having 절은 추가적으로 그룹에 대한 처리를 준다
-- 처리 관점에서는 큰 상관 X
#7
SELECT d.department_name "Name", d.location_id "Location", COUNT(*) "Number of People", ROUND(AVG(e.salary), 2) "Salary"
FROM employees e JOIN departments d
USING (department_id)
GROUP BY d.department_name, d.location_id
-- group by attribute들은 select 절에 포함해주는 것이 일반적
-- 8, 9번 문제는 축을 변경하는 pivoting
-- 연도가 하나의 축을 형성하게 됨
-- DECODE(A, B, X, Y): A = B 이면 X를 출력, A ≠ B 이면 Y를 출력
-- https://m.blog.naver.com/regenesis90/222182177281
#8
SELECT COUNT(*) AS TOTAL,
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'), '2005', 1, 0)) AS "2005",
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'), '2006', 1, 0)) AS "2006",
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'), '2007', 1, 0)) AS "2007",
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'), '2008', 1, 0)) AS "2008"
FROM employees
#9
SELECT job_id AS "JOB",
SUM(DECODE(department_id, 20, salary, 0)) AS "Dept 20",
SUM(DECODE(department_id, 50, salary, 0)) AS "Dept 50",
SUM(DECODE(department_id, 80, salary, 0)) AS "Dept 80",
SUM(DECODE(department_id, 90, salary, 0)) AS "Dept 90",
SUM(salary) AS "Total"
FROM employees
GROUP BY job_id
'ComputerScience > Database' 카테고리의 다른 글
[Database] SQL Assignment 6 (1) | 2024.04.19 |
---|---|
[Database] SQL Assignment 4 (0) | 2024.04.02 |
[Database] SQL Assignment 3 (0) | 2024.04.02 |
[Database] SQL Assignment 2 (0) | 2024.04.01 |