0% found this document useful (0 votes)
36 views

Database Queries

The document contains the schema and sample data for tables like location, department, job, employee etc. It also contains various SQL queries on these tables like finding employees in a particular department, between salary range, order by clauses, group by, joins etc.

Uploaded by

AmirImam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Database Queries

The document contains the schema and sample data for tables like location, department, job, employee etc. It also contains various SQL queries on these tables like finding employees in a particular department, between salary range, order by clauses, group by, joins etc.

Uploaded by

AmirImam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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
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

7499 ALLEN KEVIN J 670 7698 20-FEB-85 1600 300 30

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

5) List out first name,last name,salary, commission for all employees

6) List out employee_id,last name,department id for all employees and rename


employee id as “ID of the employee”, last name as “Name of the employee”,
department id as “department ID”

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.

35) Display the employees who are working in Sales department

36) Display the employees who are working as “Clerk”.

37) Display the employees who are working in “New York”

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.

52) How many employees who are working in sales department.

Non – Equi Join:

57) List out the no. of employees on grade wise.

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.

61) Show the no. of employees working under every manager.

Outer Join:

63) Display all employees in sales or operation departments.

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:

4) SQL > Select * from loc;

5) SQL > Select first_name, last_name, salary, commission from employee;

6) SQL > Select employee_id “id of the employee”, last_name “name”,


department id as “department id” from employee;

9) SQL > Select * from employee where department_id=20

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

18) SQL > Select employee_id, last_name from employee order by


employee_id

19) SQL > Select employee_id, last_name, salary from employee order by
salary desc

23) SQL > Select department_id, count(*), max(salary), min(salary), avg(salary)


from employee group by department_id

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

26) SQL > Select to_char(hire_date,’yyyy’) Year, to_char(hire_date,’mon’)


Month, count(*) “No. of employees” from employee group by
to_char(hire_date,’yyyy’), to_char(hire_date,’mon’)

34) SQL > Select * from employee where salary=(select max(salary) from
employee)

35) SQL > Select * from employee where department_id IN (select


department_id from department where name=’SALES’)

36) SQL > Select * from employee where job_id in (select job_id from job
where function=’CLERK’

37) SQL > Select * from employee where department_id=(select


department_id from department where location_id=(select location_id from
location where regional_group=’New York’))

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

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

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’))

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

You might also like