Database Queries
Database Queries
LOCATION
Location_ID Regional_Group
123 DALLAS
124 CHICAGO
167 BOSTON
DEPARTMENT
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
EMPL
OYEE
DEP
ART
EMPLOY LAST_ FIRST_ MIDDLE_ JOB MANAG HIRE SALA
COMM ME
EE_ID NAME NAME NAME _ID ER_ID DATE RY
NT_I
D
17-DEC-
7369 SMITH JOHN Q 667 7902 800 NULL 20
84
04-APR-
7505 DOYLE JEAN K 671 7839 2850 NULL 30
85
DENNI 15-MAY-
7506 LYNN S 671 7839 2750 NULL 30
S 85
10-JUN-
7507 BAKER LESLIE D 671 7839 2200 NULL 40
85
CYNTHI
7521 WARK D 670 7698 22-FEB-85 1250 500 30
A
Questions:
Simple Queries:
4) List all the locations
Where Conditions:
9) List out the employees who are working in department 20
10) List out the employees who are earning salary between 3000 and 4500
13) List out the employees whose name starts with “S”
14) List out the employees whose name start with “S” and end with “H”
16) List out the employees who are working in department 10 and draw the
salaries more than 3500
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
Group By & Having Clause:
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.
Sub-Queries
34) Display the employee who got the maximum salary.
Sub-Query operators: (ALL,ANY,SOME,EXISTS)
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.
JOINS
Simple join
51) How many employees who are working in different departments and
display with department name.
58) Display the employ salary grades and no. of employees between 2000 to
5000 range of salary.
Self Join:
60) Display the employee details who earn more than their managers salaries.
Outer Join:
Set Operators:
65) List out the ALL jobs in Sales and Accounting Departments.
66) List out the common jobs in Research and Accounting Departments in
ascending order.
Answers:
10) SQL > Select * from employee where salary between 3000 and 4500
13) SQL > Select * from employee where last_name like ‘S%’
14) SQL > Select * from employee where last_name like ‘S%H’
16) SQL > Select * from employee where department_id=10 and salary>3500
19) SQL > Select employee_id, last_name, salary from employee order by
salary desc
24) SQL > Select job_id, count(*), max(salary), min(salary), avg(salary) from
employee group by job_id
25) SQL > Select to_char(hire_date,’month’)month, count(*) from employee
group by to_char(hire_date,’month’) order by month
34) SQL > Select * from employee where salary=(select max(salary) from
employee)
36) SQL > Select * from employee where job_id in (select job_id from job
where function=’CLERK’
43) SQL > Select * from employee where salary > all (Select salary from
employee where department_id=30)
44) SQL > Select * from employee where salary > any (Select salary from
employee where department_id=30)
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’
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
61) SQL > Select m.manager_id, count(*) from employee e, employee m where
e.employee_id=m.manager_id group by m.manager_id
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