SQL Questions With Answer
SQL Questions With Answer
Location
Location_Id Regional_Group
122 New York
123 Dallas
124 Chicagoz
167 Boston
EMPLOYEE
Employ Last_Name First_Name Middle_ Job_Id Manager HireDate Department_ Salary
ee_id Name _Id Id
7369 Smith John Q 667 7902 17-dec- 10 20000
84
7499 Allen Kevin J 670 7698 20-feb- 10 22000
85
7505 Doyle Jean K 671 7839 04-Apr- 30 24000
85
7506 Dennis Lynn S 671 7839 15-May- 40 30000
85
7507 Baker Leslie D 671 7839 10-Jun- 20 35000
85
7521 Wark Cynthia D 670 7698 22-Feb- 10 22000
85
--Simple Queries:
--1.List all the employee details
---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”
---7.List out the employees annual salary with their names only.
--Where conditions:
---10.List out the employees who are earning salary between 3000 and 4500
select * from EMPLOYEE where Department_Id not in (select Department_Id from EMPLOYEE where
Department_Id=10 or Department_Id= 30)
---14.List out the employees whose name starts with ”S” and ends with “H”
select * from EMPLOYEE where First_Name like S%' and Last_Name like '%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
--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 wide in the organization
---23.List out the department wise maximum salary,minimum salary,average salary of the employees
----26.List out the no of employees for each month and year, in the ascending order based on the year ,
month
-----33.Which is the department id , having greater than or equal to 3 employees joined in april 1985
---Sub Queries:
select * from EMPLOYEE where DEPARTMENT_ID in (select DEPARTMENT_ID from DEPARTMENT where
NAME='SALES');
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%
update EMPLOYEE set SALARY=SALARY*0.1 where JOB_ID in (select JOB_ID from JOB where
function='CLERK');
select * from EMPLOYEE where SALARY=(selEct max(SALARY) from EMPLOYEE where SALARY<(select
max(SALARY) from EMPLOYEE));
---43.List out the employees who earn more than every employee in department 30
select COUNT(*) from employee where salary > ALL(select salary from employee where
Department_id=30);
---44.List out the employee who earn more than the lowest salary in department 30
select * from employee where salary > all (select min(salary) from employee where Department_id=30);
----47.Find out the employees who earn greater than the average salary for their department
SELECT DISTINCT * FROM EMPLOYEE WHERE SALARY > ALL(SELECT AVG(SALARY) FROM EMPLOYEE
GROUP BY DEPARTMENT_ID);
---50.Display the employees with their department name and regional groups
SELECT E.EMPLOYEE_ID,E.LAST_NAME,E.FIRST_NAME,E.MIDDLE_NAME,E.DEPARTMENT_ID,E.JOB_ID,
E.MANAGER_ID,E.HIREDATE,E.SALARY,E.COMM,D.NAME,L.REGIONAL_GROUP
FROM EMPLOYEE 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(*),D.NAME FROM EMPLOYEE E,DEPARTMENT D WHERE
E.DEPARTMENT_ID=D.DEPARTMENT_ID GROUP BY D.NAME;
----53.Which is the department having greater than or equal to 5 employees and display the department
names in ascending order SELECT D.NAME FROM EMPLOYEE E,DEPARTMENT D
WHERE E.DEPARTMENT_ID=D.DEPARTMENT_ID GROUP BY D.NAME HAVING COUNT(*) >=5;
----Outer Join:
----62.Display the employee details with all departments
Select E.LAST_NAME,D.DEPARTMENT_ID,D.NAME from EMPLOYEE E, DEPARTMENT D where
E.DEPARTMENT_ID(+)=D.DEPARTMENT_ID;
----Set Operators:
----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
PLSQL questions
1. Create an anonymous block to fetch records from a table through cursor and display data?
2. Write a plsql cursor program Calculate the total salary without using sum() function?
3. Write a package which will have one procedure and one function. The procedure should
display min salary, max salary, avg salary per dept. The function should return all empno for
a given department?
4. Write a function to display employee information based on a particular department?
Note : (2 more questions having sir that is based on functions and procedure)