0% found this document useful (0 votes)
238 views11 pages

SQL Questions With Answer

The document contains details of tables like location, department, job, employee with sample data. It also contains 33 SQL queries on these tables with conditions like where, order by, group by, having and subqueries. The queries retrieve employee, department, job and location details and analyze employee count, salaries, joining details based on conditions.

Uploaded by

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

SQL Questions With Answer

The document contains details of tables like location, department, job, employee with sample data. It also contains 33 SQL queries on these tables with conditions like where, order by, group by, having and subqueries. The queries retrieve employee, department, job and location details and analyze employee count, salaries, joining details based on conditions.

Uploaded by

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

SQL QUESTIONS WITH ANSWER

Location
Location_Id Regional_Group
122 New York
123 Dallas
124 Chicagoz
167 Boston

create table LOCATION(Location_ID INTEGER,Regional_Group VARCHAR2(100));


INSERT INTO LOCATION VALUES(122,'NEW YORK');
INSERT INTO LOCATION VALUES(123,'DALLAS');
INSERT INTO LOCATION VALUES(124,'CHICAGO');
INSERT INTO LOCATION VALUES(167,'BOSTON');
SELECT * FROM LOCATION;
SELECT * FROM (SELECT * FROM LOCATION) L;
Department
Department_Id Name Location
10 Accounting 122
20 Research 124
30 Sales 123
40 Operations 167

create table DEPARTMENT(Department_ID INTEGER,Name VARCHAR2(100),Location_ID INTEGER);


INSERT INTO DEPARTMENT VALUES(10,'ACCOUNTING',122);
INSERT INTO DEPARTMENT VALUES(20,'RESEARCH',124);
INSERT INTO DEPARTMENT VALUES(30,'SALES',123);
INSERT INTO DEPARTMENT VALUES(40,'OPERATIONS',167);
SELECT * FROM DEPARTMENT;
SELECT * FROM (SELECT * FROM DEPARTMENT) D;
Job
Job_Id Function
667 Clerk
668 Staff
669 Analyst
670 Salesperson
671 Manager
672 President
create table JOB(Job_ID INTEGER,Function VARCHAR2(100));
INSERT INTO JOB VALUES(667,'CLERK');
INSERT INTO JOB VALUES(668,'STAFF');
INSERT INTO JOB VALUES(669,'ANALYST');
INSERT INTO JOB VALUES(670,'SALESPERSON');
INSERT INTO JOB VALUES(671,'MANAGER');
INSERT INTO JOB VALUES(672,'PRESIDENT');
SELECT * FROM JOB;
SELECT * FROM (SELECT * FROM JOB) J;

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

create table EMPLOYEE(EMPLOYEE_ID INTEGER,


LAST_NAME VARCHAR2(100),
FIRST_NAME VARCHAR2(100),
MIDDLE_NAME VARCHAR2(100),
JOB_ID INTEGER,
MANAGER_ID INTEGER,
HIREDATE DATE,
SALARY INTEGER,
COMM INTEGER,
DEPARTMENT_ID INTEGER);
SELECT * FROM EMPLOYEE;
INSERT INTO EMPLOYEE VALUES(7369,'SMITH','JOHN','Q',667,7902,'17-DEC-84',800,NULL,20);
INSERT INTO EMPLOYEE VALUES(7499,'ALLEN','KEVIN','J',670,7698,'20-FEB-85',1600,300,30);
INSERT INTO EMPLOYEE VALUES(7505,'DOYLE','JEAN','K',671,7839,'04-APR-85',2850,NULL,30);
INSERT INTO EMPLOYEE VALUES(7506,'DENNIS','LYNN','S',671,7839,'15-MAY-85',2750,NULL,30);
INSERT INTO EMPLOYEE VALUES(7507,'BAKER','LESLIE','D',671,7839,'10-JUN-85',2200,NULL,40);
INSERT INTO EMPLOYEE VALUES(7521,'WARK','CYNTHIA','D',670,7698,'22-FEB-85',1250,500,30);
SELECT * FROM EMPLOYEE;
SELECT * FROM (SELECT * FROM EMPLOYEE) E;

--QUERIES BASED ON THE ABOVE TABLES:

--Simple Queries:
--1.List all the employee details

select * from EMPLOYEE;

--2.List all the department details

select * from Department;

--3.List all job details

select * from Job;

---4.List all the locations

select * from Location;

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


select first name,last name,salary,commission from EMPLOYEE;

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

select employee_id as"ID of the employee",last_name as"Name of the employee",department id


as"department ID" from employee;

---7.List out the employees annual salary with their names only.

select First_name||Middle_Name||Last_Name as "names",Salary*12 from EMPLOYEE;

--Where conditions:

---8.List the details about “SMITH”

select * from EMPLOYEE where Last_Name='SMITH';

--9.List out the employees who are working in department 20

select * from EMPLOYEE where Department_Id=20;

---10.List out the employees who are earning salary between 3000 and 4500

select *from EMPLOYEE where Salary between 3000 and 4500;

---11.List out the employees who are working in departments 10 or 20

select * from EMPLOYEE where Department_Id=20 or Department_Id=10;

---12.Find out the employees who are not working in department 10 or 30

select * from EMPLOYEE where Department_Id not in (select Department_Id from EMPLOYEE where
Department_Id=10 or Department_Id= 30)

---13.List out the employees whose name starts with “S”

select * from EMPLOYEE where First_Name like 'S%';

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

select * from EMPLOYEE where First_Name like S%' and length(First_Name)=4;

----16.List out the employees who are working in department 10 and draw the salaries more than 3500

select * from EMPLOYEE where (Salary>3500) and (Department_Id=10);

---17.List out the employees who are not receiving commission

select * from employee where commission is null;

--Order by Clause:

----18.List out the employee id , last name in ascending order based on the employee id

select employee_id,Last_Name from EMPLOYEE order by employee_id asc;

-----19.List out the employee id , name in descending order based on salary column

select employee_id,Last_Name from EMPLOYEE order by salary desc;

----20.List out the employee details according to their last_name in ascending order and salaries in
descending order

select * from EMPLOYEE order by Last_Name asc, Salary desc;

---21.List out the employee details according to their last name in ascending order and then on
department id in descending order

select * from EMPLOYEE order by Last_Name asc, employee_id desc;

---Group by & Having Clause:

---22.How many employees who are working in different departments wide in the organization

select count(*),department_id from EMPLOYEE group by department_id ;

---23.List out the department wise maximum salary,minimum salary,average salary of the employees

select min(Sal),max(Sal),avg(Sal),DeptId from EMPLOYEE group by DeptId;


---24.List out the job wise maximum salary,minimum salary,average salary of the employees

select min(Sal),max(Sal),avg(Sal),jobid from EMPLOYEE group by jobid;

---25.List out the no of employees joined in every month in ascending order

select count(*),to_char(HireDate,'mm') from EMPLOYEE group by to_char(HireDate,'mm') order by


to_char(HireDate,'mm')asc;

----26.List out the no of employees for each month and year, in the ascending order based on the year ,
month

select count(*),to_char(HIREDATE,'mm,YY') from employee


group by to_char(HIREDATE,'mm,YY')
order by to_char(HIREDATE,'mm,YY')asc;

---27.List out the department id having atleast 4 employees

select DeptId,COUNT(*) from EMPLOYEE GROUP BY DeptId HAVING COUNT(*)>=4;

---28.How many employees in January month

SELECT count(*),to_char(HIREDATE,'mm') FROM employee WHERE to_char(HIREDATE,'mm')='01';

----29.How many employees who are joined in January or September month

SELECT count(*),to_char(HIREDATE,'mm') FROM employee WHERE to_char(HIREDATE,'mm')='08' or


to_char(HIREDATE,'mm')='06'
group by to_char(HIREDATE,'mm');

---30.How many employees who are joined in 1985

SELECT count(*),to_char(HIREDATE,'yy') FROM EMPLOYEE WHERE to_char(HIREDATE,'yy')='85' group by


to_char(HIREDATE,'yy');

---31.How many employees joined in each month in 1985

select count(*),to_char(HIREDATE,'mm,yy') from EMPLOYEE WHERE to_char(HIREDATE,'yy')='85'group by


to_char(HIREDATE,'mm,yy');

----32.How many employees who are joined in march 1985


select count(*),to_char(HIREDATE,'mm,yy') from EMPLOYEE
WHERE to_char(HIREDATE,'mm,yy')='03,85' group by to_char(HIREDATE,'mm,yy');

-----33.Which is the department id , having greater than or equal to 3 employees joined in april 1985

select DEPARTMENT_ID,COUNT(*)"NO OF EMPLOYEE" from EMPLOYEE


WHERE to_char(HIREDATE,'mm,yy')='04,85' GROUP BY DEPARTMENT_ID HAVING COUNT(*)>=3;

---Sub Queries:

---34.Display the employee who got the maximum salary

select * from EMPLOYEE where SALARY in (select max(salary) from EMPLOYEE);

---35.Display the employees who are working in sales department

select * from EMPLOYEE where DEPARTMENT_ID in (select DEPARTMENT_ID from DEPARTMENT where
NAME='SALES');

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

select * from EMPLOYEE where JOB_ID in (select JOB_ID from JOB where function='CLERK');

---37.Display the employees who are working in “New york”

select * from EMPLOYEE where DEPARTMENT_ID IN (SELECT DEPARTMENT_ID FROM DEPARTMENT


WHERE LOCATION_ID in (select LOCATION_ID from LOCATION where Regional_Group='NEW YORK'));

---38.Find out no of employees working in “Sales” department

select COUNT(*)"NO OF EMPLOYEE" from EMPLOYEE where DEPARTMENT_ID in (select


DEPARTMENT_ID from DEPARTMENT where NAME='SALES');

----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');

---40.Delete the employees who are working in accounting department


delete from EMPLOYEE where DEPARTMENT_ID in (select DEPARTMENT_ID from DEPARTMENT where
NAME=’ACCOUNTING’);

---41.Display the second highest salary drawing employee details

select * from EMPLOYEE where SALARY=(selEct max(SALARY) from EMPLOYEE where SALARY<(select
max(SALARY) from EMPLOYEE));

---42.Display the Nth highest salary drawing employee details

---Sub Query Operators (ALL,ANY,SUM,EXISTS)

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

---45.Find out whose department has not employees

select DISTINCT DEPARTMENT_ID from DEPARTMENT where DEPARTMENT_ID NOT IN (select


DEPARTMENT_ID from EMPLOYEE);

---46.Find out which department does not have any employees

select DISTINCT DEPARTMENT_ID from DEPARTMENT where DEPARTMENT_ID NOT IN (select


DEPARTMENT_ID from EMPLOYEE);

----Co-Related Sub Queries:

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

----JOINS Simple Join:

---48.Lsit out the employees with their department names


SELECT E.EMPLOYEE_ID,E.LAST_NAME,E.FIRST_NAME,E.MIDDLE_NAME,E.JOB_ID,E.MANAGER_ID,
E.HIREDATE,E.SALARY,E.COMM,E.DEPARTMENT_ID,D.NAME FROM EMPLOYEE E,DEPARTMENT D
WHERE E.DEPARTMENT_ID=D.DEPARTMENT_ID;

----49.Display employees with their designations(jobs)


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,J.FUNCTION FROM EMPLOYEE E,JOB J
WHERE E.JOB_ID=J.JOB_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;

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


SELECT COUNT(*) FROM EMPLOYEE E,DEPARTMENT D WHERE E.DEPARTMENT_ID=D.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 SELECT D.NAME FROM EMPLOYEE E,DEPARTMENT D
WHERE E.DEPARTMENT_ID=D.DEPARTMENT_ID GROUP BY D.NAME HAVING COUNT(*) >=5;

----54.How many jobs in the organization with designations


SELECT FUNCTION,COUNT(*) FROM EMPLOYEE E,JOB J WHERE E.JOB_ID=J.JOB_ID GROUP BY
J.FUNCTION;

---55.How many employees working in “New york”


select * from EMPLOYEE where DEPARTMENT_ID IN (SELECT DEPARTMENT_ID FROM DEPARTMENT
WHERE LOCATION_ID in (select LOCATION_ID from LOCATION where Regional_Group='NEW YORK'));

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

----63.Display all employees in sales or operational departments


Select last_name, d.department_id, d.name from employee e, department d where
e.department_id(+)=d.department_id and d.department_id in
(select department_id from department where name IN ('SALES','OPERATIONS'))

----Set Operators:

---64.List out the distinct jobs in sales and accounting departments


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

----65.List out the ALL jobs in sales and accounting departments


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

You might also like