Sample Queries
Sample Queries
Sample Queries
7.List out the employees anuual salary with their names only.
10.List out the employees who are earning salary between 3000 and 4500
SELECT LAST_NAME FROM EMPL WHERE SALARY BETWEEN 3000 AND
4500;
14.List out the employees whose name start with “S” and end with “H”.
SELECT FIRST_NAME,MIDDLE_NAME,LAST_NAME FROM EMPL WHERE
LAST_NAME LIKE'S%H';
15.List out the employees whose name length is 5 and start with “S”
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.
20.list out the employee details according to their last_name in ascending order and
salaries in descending order
SELECT * FROM EMPL ORDER BY LAST_NAME,SALARY DESC;
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
SELECT DEPARTMENT_ID,MAX(SALARY),MIN(SALARY),AVG(SALARY)
FROM EMPL GROUP BY DEPARTMENT_ID;
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.
SELECT
TO_CHAR(HIRE_DATE,'YYYY')YEAR,TO_CHAR(HIRE_DATE,'MM')MONTH,CO
UNT(*)
FROM EMPL GROUP BY
TO_CHAR(HIRE_DATE,'YYYY'),TO_CHAR(HIRE_DATE,'MM') ORDER BY
YEAR,MONTH;
SELECT
TO_CHAR(HIRE_DATE,'YYYY')YEAR,TO_CHAR(HIRE_DATE,'MM')MONTH,CO
UNT(*) FROM EMPL
GROUP BY TO_CHAR(HIRE_DATE,'YYYY'),TO_CHAR(HIRE_DATE,'MM')
HAVING TO_CHAR(HIRE_DATE,'YYYY')='1985';
SELECT
TO_CHAR(HIRE_DATE,'YYYY')YEAR,TO_CHAR(HIRE_DATE,'MM')MONTH,CO
UNT(*) FROM EMPL
WHERE TO_CHAR(HIRE_DATE,'YYYY')='1985' AND
TO_CHAR(HIRE_DATE,'MM')='MAY'
GROUP BY TO_CHAR(HIRE_DATE,'YYYY'),TO_CHAR(HIRE_DATE,'MM');
33.Which is the department id, having greater than or equal to 3 employees joined
in April 1985.
SELECT
DEPARTMENT_ID,TO_CHAR(HIRE_DATE,'MM')MONTH,TO_CHAR(HIRE_DAT
E,'YYYY')YEAR,
COUNT(*) FROM EMPL WHERE TO_CHAR(HIRE_DATE,'MM')='APR' AND
TO_CHAR(HIRE_DATE,'YYYY')=1985
GROUP BY
TO_CHAR(HIRE_DATE,'YYYY'),TO_CHAR(HIRE_DATE,'MM'),DEPARTMENT_I
D HAVING COUNT(*)>=3;
Sub-Queries:
39.Update the employees salaries, who are working as Clerk on the basis of 10%.
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.
OR
SELECT NAME FROM DEPARTMENT
WHERE DEPARTMENT_ID
NOT IN (SELECT DEPARTMENT_ID FROM EMPL);
47.Find out the employees who earn greater than the average salary for their
department.
Joins
Simple joins
select employee_id,last_name,d.department_id
from empl e,department d where
e.department_id=d.department_id;
select employee_id,last_name,function
from empl e,job j where
e.job_id=d.job_id;
50.Display the employees with their department name and regional groups.
select employee_id,last_name,name,regional_group
from empl e,department d,location l
where e.department_id=d.department_id
and d.location_id=l.location_id;
51.How many employees who are working in different departments and display
with department name.
select count(employee_id),name
from empl e,department d
where e.department_id=d.department_id
group by name;
doubt:
Select name,count(*) from empl e,department d
where d.department_id=e.department_id and d.name='SALES';
53.Which is the department having greater than or equal to 5 employees and display
the department names in ascending order.
doubt:
Select regional_group,count(*) from empl 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;
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
Self Join:
60.Display the employee details who earn more than their managers salaries.
Outer Join:
select employee_id,last_name,name
from empl e,department d where
e.department_id(+)=d.department_id;
Select last_name,d.department_id,d.name
from empl e,department d
where e.department_id=d.department_id
and d.dname in ('SALES','OPERATIONS');
Set Operators:
65.List out the common jobs in Research and Accounting Departments in ascending
order.