SQL Queries: Create The Following Tables
SQL Queries: Create The Following Tables
net
SQL Queries
Create the following Tables:
LOCATION
Location_ID Regional_Group
122 NEW YORK
123 DALLAS
124 CHICAGO
167 BOSTON
DEPARTMENT
Department_ID Name Location_ID
10 ACCOUNTING 122
20 RESEARCH 124
30 SALES 123
40 OPERATIONS 167
JOB
Job_ID Function
667 CLERK
668 STAFF
669 ANALYST
670 SALESPERSON
671 MANAGER
672 PRESIDENT
EMPLOYEE
MID
EMPL FIRST MANA DEPAR
LAST_N DLE JOB_I HIRED SALAR
OYEE _NAM GER_I COMM TMEN
AME _NA D ATE Y
_ID E D T_ID
ME
17-
7369 SMITH JOHN Q 667 7902 DEC- 800 NULL 20
84
20-
7499 ALLEN KEVIN J 670 7698 FEB- 1600 300 30
85
04-
7505 DOYLE JEAN K 671 7839 APR- 2850 NULL 30
85
15-
7506 DENNIS LYNN S 671 7839 MAY- 2750 NULL 30
85
7507 BAKER LESLI D 671 7839 10- 2200 NULL 40
JUN-
E
85
22-
CYNT
7521 WARK D 670 7698 FEB- 1250 500 30
HIA
85
Simple Queries:
Where Conditions:
Order By Clause:
18. List out the employee id, last name in ascending order based on the employee
id.
19. List out the employee id, name in descending order based on salary column
20. list out the employee details according to their last_name in ascending order
and salaries in descending order
21. list out the employee details according to their last_name in ascending order
and then on department_id in descending order.
22. How many employees who are working in different departments wise in the
organization
23. List out the department wise maximum salary, minimum salary, average
salary of the employees
24. List out the job wise maximum salary, minimum salary, average salaries of
the employees.
25. List out the no.of employees joined in every month in ascending order.
26. List out the no.of employees for each month and year, in the ascending order
based on the year, month.
27. List out the department id having atleast four employees.
28. How many employees in January month.
29. How many employees who are joined in January or September month.
30. How many employees who are joined in 1985.
31. How many employees joined each month in 1985.
32. How many employees who are joined in March 1985.
33. Which is the department id, having greater than or equal to 3 employees
joined in April 1985.
Sub-Queries
43. List out the employees who earn more than every employee in department
30.
44. List out the employees who earn more than the lowest salary in department
30.
45. Find out whose department has not employees.
46. Find out which department does not have any employees.
47.Find out the employees who earn greater than the average salary for their
department.
Joins
Simple join
51.How many employees who are working in different departments and display
with department name.
52.How many employees who are working in sales department.
53.Which is the department having greater than or equal to 5 employees and
display the department names in ascending order.
54.How many jobs in the organization with designations.
55.How many employees working in “New York”.
Self Join:
Outer Join:
Set Operators:
51. SQL > Select name, count(*) from employee e, department d where
d.department_id=e.department_id group by name
52. SQL > Select name, count(*) from employee e, department d where
d.department_id=e.department_id group by name having name=’SALES’
53. SQL > Select name, count(*) from employee e, department d where
d.department_id=e.department_id group by name having count (*)>=5 order
by name
54. SQL > Select function, count(*) from employee e, job j where
j.job_id=e.job_id group by function
55. SQL > Select regional_group, count(*) from employee e, department d,
location l where e.department_id=d.department_id and
d.location_id=l.location_id and regional_group=’NEW YORK’ group by
regional_group
56. SQL > Select employee_id, last_name, grade_id from employee e,
salary_grade s where salary between lower_bound and upper_bound order by
last_name
57. SQL > Select grade_id, count(*) from employee e, salary_grade s where
salary between lower_bound and upper_bound group by grade_id order by
grade_id desc
58. SQL > Select grade_id, count(*) from employee e, salary_grade s where
salary between lower_bound and upper_bound and lower_bound>=2000 and
lower_bound<=5000 group by grade_id order by grade_id desc
59. SQL > Select e.last_name emp_name, m.last_name, mgr_name from
employee e, employee m where e.manager_id=m.employee_id
60. SQL > Select e.last_name emp_name, e.salary emp_salary, m.last_name,
mgr_name, m.salary mgr_salary from employee e, employee m where
e.manager_id=m.employee_id and m.salary
61. SQL > Select m.manager_id, count(*) from employee e, employee m where
e.employee_id=m.manager_id group by m.manager_id
62. SQL > Select last_name, d.department_id, d.name from employee e,
department d where e.department_id(+)=d.department_id
63. SQL > Select last_name, d.department_id, d.name from employee e,
department d where e.department_id(+)=d.department_id and
d.department_idin (select department_id from department where name IN
(‘SALES’,’OPERATIONS’))
64. SQL > Select function from job where job_id in (Select job_id from employee
where department_id=(select department_id from department where
name=’SALES’)) union Select function from job where job_id in (Select job_id
from employee where department_id=(select department_id from department
where name=’ACCOUNTING’))
65. SQL > Select function from job where job_id in (Select job_id from employee
where department_id=(select department_id from department where
name=’SALES’)) union all Select function from job where job_id in (Select
job_id from employee where department_id=(select department_id from
department where name=’ACCOUNTING’))
66. SQL > Select function from job where job_id in (Select job_id from employee
where department_id=(select department_id from department where
name=’RESEARCH’)) intersect Select function from job where job_id in
(Select job_id from employee where department_id=(select department_id
from department where name=’ACCOUNTING’)) order by function