Professional Documents
Culture Documents
Data Analysis and Integration: SQL Views (Cont.)
Data Analysis and Integration: SQL Views (Cont.)
• Higher schemas
Views
count_emp_dept sum_salary_dept
Views
curr_dept_emp curr_dept_manager curr_salaries curr_titles
Base tables
employees departments dept_emp dept_manager salaries titles
• count_emp_dept(dept_no, count_emp)
– a view to show the number of employees in each department
• sum_salary_dept(dept_no, sum_salary)
– a view to show the sum of salaries by department
• count_emp_dept(dept_no, count_emp)
– a view to show the number of employees in each department
• sum_salary_dept(dept_no, sum_salary)
– a view to show the Sum of salaries by department
select dept_no
from (select dept_no, count(emp_no) as count_emp
from curr_dept_emp
group by dept_no) as a
where count_emp > 40;
select dept_no
from (select dept_no, count(emp_no) as count_emp
from curr_dept_emp
group by dept_no) as a
where count_emp > 40;
select dept_no
from (select dept_no, count(emp_no) as count_emp
from (select emp_no, dept_no
from dept_emp
where from_date <= current_date and to_date >= current_date) as b
group by dept_no) as a
where count_emp > 40;
IST/MEIC Análise e Integração de Dados - 2022/2023 - 1º Sem 8
Query unfolding (count_emp_dept)
• Note that
select dept_no
from (select dept_no, count(emp_no) as count_emp
from (select emp_no, dept_no
from dept_emp
where from_date <= current_date and to_date >= current_date) as b
group by dept_no) as a
where count_emp > 40;
select dept_no
from dept_emp
where from_date <= current_date and to_date >= current_date
group by dept_no
having count(emp_no) > 40;
select sum(sum_salary)
from (select b.dept_no, sum(a.salary) as sum_salary
from curr_salaries as a, curr_dept_emp as b
where a.emp_no = b.emp_no
group by b.dept_no) as c;
select sum(sum_salary)
from (select b.dept_no, sum(a.salary) as sum_salary
from curr_salaries as a, curr_dept_emp as b
where a.emp_no = b.emp_no
group by b.dept_no) as c;
select sum(sum_salary)
from (select b.dept_no, sum(a.salary) as sum_salary
from (select emp_no, salary
from salaries
where from_date <= current_date and to_date >= current_date) as a,
(select emp_no, dept_no
from dept_emp
where from_date <= current_date and to_date >= current_date) as b
where a.emp_no = b.emp_no
group by b.dept_no) as c;
+-----------------+
| sum(sum_salary) |
+-----------------+
| 18656036 |
+-----------------+
1 row in set (0.01 sec)
×
×
×
×
×
×
×
×
×
×
×
×
IST/MEIC Análise e Integração de Dados - 2022/2023 - 1º Sem 22
Schema matching (departments)
×
×
+---------+--------------------+
(select dept_no, dept_name | dept_no | dept_name |
from employees.departments) +---------+--------------------+
| d009 | Customer Service |
union | d005 | Development |
| d002 | Finance |
(select departmentid, departmentname | d003 | Human Resources |
from company.department); | d001 | Marketing |
| d004 | Production |
| d006 | Quality Management |
| d008 | Research |
| d007 | Sales |
| 101 | IT |
| 102 | HR |
| 103 | Finance |
| 104 | Sales |
| 105 | marketing |
+---------+--------------------+
14 rows in set (0.00 sec)
+--------+---------+
| emp_no | dept_no |
+--------+---------+
| 10721 | d009 |
| 11260 | d009 |
| ... | ... |
| 1008 | 101 |
| 1014 | 101 |
| ... | ... |
+--------+---------+
+--------+-----------+
| emp_no | salary |
+--------+-----------+
| 10721 | 44812.00 |
| 11260 | 52435.00 |
| ... | ... |
| 1001 | 850000.00 |
| 1002 | 650000.00 |
| ... | ... |
+--------+-----------+
+---------+--------------------+ +--------------+----------------+
| dept_no | dept_name | | departmentid | departmentname |
+---------+--------------------+ +--------------+----------------+
| d009 | Customer Service | | 101 | IT |
| d005 | Development | | 102 | HR |
| d002 | Finance | | 103 | Finance |
| d003 | Human Resources | | 104 | Sales |
| d001 | Marketing | | 105 | marketing |
| d004 | Production | +--------------+----------------+
| d006 | Quality Management |
| d008 | Research |
| d007 | Sales |
+---------+--------------------+