Created
December 19, 2017 15:01
-
-
Save JZHeadley/86bfde36729f267763bbcd5a4db95f98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| create or replace view num_employees_dept as | |
| select | |
| nvl(department_id,0) department, | |
| count(*) as num_Employees | |
| from employees | |
| group by department_id; | |
| select * from num_employees_dept; | |
| create or replace view avg_sal_dept as | |
| select | |
| nvl(department_id,0) department, | |
| avg(salary) salary | |
| from employees | |
| group by department_id; | |
| select * from avg_sal_dept; | |
| -- using the views | |
| select | |
| avg_sal_dept.department, | |
| salary, | |
| num_employees | |
| from | |
| avg_sal_dept join NUM_EMPLOYEES_DEPT | |
| on NUM_EMPLOYEES_DEPT.DEPARTMENT = avg_sal_dept.DEPARTMENT | |
| where num_employees < 10 and salary > 5000; | |
| -- straight sql | |
| select | |
| avg_sal.department, | |
| salary, | |
| num_employees | |
| from | |
| (select | |
| nvl(department_id,0) department, | |
| avg(salary) salary | |
| from employees | |
| group by department_id) avg_sal | |
| join | |
| (select | |
| nvl(department_id,0) department, | |
| count(*) as num_Employees | |
| from employees | |
| group by department_id) num_emp | |
| on num_emp.DEPARTMENT = avg_sal.DEPARTMENT | |
| where num_employees < 10 and salary > 5000; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment