SQL Queries Gcreddy
SQL Queries Gcreddy
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
EMPLO
LAST_N FIRST_ MIDDLE_ JOB_ MANAG HIRED SALA COM DEPARTM
YEE_I
AME NAME NAME ID ER_ID ATE RY M ENT_ID
D
17- NUL
7369 SMITH JOHN Q 667 7902 800 20
DEC-84 L
20-
7499 ALLEN KEVIN J 670 7698 1600 300 30
FEB-85
04- NUL
7505 DOYLE JEAN K 671 7839 2850 30
APR-85 L
15-
NUL
7506 DENNIS LYNN S 671 7839 MAY- 2750 30
L
85
10- NUL
7507 BAKER LESLIE D 671 7839 2200 40
JUN-85 L
CYNTHI 22-
7521 WARK D 670 7698 1250 500 30
A FEB-85
Simple Queries:
A)SQL > Select employee_id “id of the employee”, last_name “name", department id
as “department id” from employee;
7.List out the employees anuual salary with their names only.
Where Conditions:
8) List the details about “SMITH”
10)List out the employees who are earning salary between 3000 and 4500
12) Find out the employees who are not working in department 10 or 30
13) List out the employees whose name starts with “S”
14) List out the employees whose name start with “S” and end with “H”
15) List out the employees whose name length is 4 and start with “S”
16)List out the employees who are working in department 10 and draw the
salaries more than 3500
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.
26) List out the no.of employees for each month and year, in the ascending order
based on the year, month.
Sub-Queries
A) Select * from employee where job_id in (select job_id from job where
function=’CLERK’
39)Update the employees salaries, who are working as Clerk on the basis of
10%.
A) Select distinct e.salary from employee where & no-1=(select count(distinct salary)
from employee where sal>e.salary)
43) List out the employees who earn more than every employee in department
30.
A) Select * from employee where salary > all (Select salary from employee where
department_id=30)
44)List out the employees who earn more than the lowest salary in department
30.
A) Select * from employee where salary > any (Select salary from employee where
department_id=30)
46) Find out which department does not have any employees.
A) Select name from department d where not exists (select last_name from employee
e where d.department_id=e.department_id)
Co-Related Sub Queries:
47.Find out the employees who earn greater than the average salary for their
department.
A) Select employee_id, last_name, salary, department_id from employee e where salary
> (select avg(salary) from employee where department_id=e.department_id)
Joins
Simple join
51.How many employees who are working in different departments and display
with department name.
A) Select name, count(*) from employee e, department d where
d.department_id=e.department_id group by name
53.Which is the department having greater than or equal to 5 employees and display
the department names in ascending order.
A) Select name, count(*) from employee e, department d where
d.department_id=e.department_id group by name having count (*)>=5 order by name
58.Display the employ salary grades and no. of employees between 2000 to 5000
range of salary.
A) 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
Self Join:
Outer Join:
Set Operators:
64.List out the distinct jobs in Sales and Accounting Departments.
A) 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’))
66.List out the common jobs in Research and Accounting Departments in ascending
order.
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