SQL Assignements&Solutions
SQL Assignements&Solutions
TABLE : DEPT
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
TABLE: EMP
1) Display all the employees who are getting 2500 and excess salaries in
department 20.
4) Display all the employees who are getting some commission with their
designation is neither MANANGER nor ANALYST
Select *from EMP
Where Comm is not null and job in (‘Manager’,’ Analyst’);
5) Display all the ANALYSTs whose name doesn’t ends with ‘S’
Select * from EMP
Where job=’Analyst’ and job not like (‘%s’);
6) Display all the employees whose naming is having letter ‘E’ as the last but
one character
7) Display all the employees who total salary is more than 2000.
(Total Salary = Sal + Comm)
8) Display all the employees who are getting some commission in department 20
& 30.
10) Display all the employees who earning salary not in the range of 2500 and
5000 in department 10 & 20.
12) Display the departments that are having more than 3 employees under
it.
13) Display job-wise average salaries for the employees whose employee
number is not from 7788 to 7790.
14) Display department-wise total salaries for all the Managers and
Analysts, only if the average salaries for the same is greater than or
equal to 3000.
ID Name
101 Oracle
102 Oracle
S103 Oracle
101 Oracle
102 Java
103 Java
101 Java
102 Java
103 Java
101 Java
101 Java
101 Oracle
101 VB
102 ASP
ID NAME COUNT(*)
---------- --------------- ----------
101 java 3
101 oracle 4
102 java 2
103 java 2
ID NAME COUNT(*)
---------- --------------- ----------
101 vb 1
102 asp 1
102 oracle 1
103 oracle 1
17) Select only the duplicate records that are duplicated only once.
18) Select only the duplicate records that are not having the id=101.
ID NAME COUNT(*)
---------- --------------- ----------
102 java 2
103 java 2
ASSIGNMENTS ON SUBQUERIES
19)Display all the employees who are earning more than all the managers.
21)Select employee number, job & salaries of all the Analysts who are earning
more than any of the managers.
SQL> SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE
LOC='DALLAS');
5 rows selected.
23)Select department name & location of all the employees working for CLARK.
1 row selected.
3 rows selected.
1 row selected.
MAX(SAL)
----------
3000
1 row selected.
27)Display the third maximum salary.
MAX(SAL)
----------
2975
1 row selected.
28)Display all the managers & clerk who work in Accounting and Marketing
(SALES) departments.
30) Get all the employees who work in the same departments as of
SCOTT.
SQL> select *
2 from emp
3 where sal=(select sal from emp where ename='SMITH');
32) Display all the employees who are getting some commission in
marketing department where the employees have joined only on
weekdays.
33) Display all the employees who are getting more than the average
salaries of all the employees.
ASSIGNMENTS ON JOINS
ASSIGNMENTS ON EQUI-JOINS
34)Display all the managers & clerks who work in Accounts and Marketing
departments.
JOB DNAME
--------- --------------
CLERK RESEARCH
MANAGER RESEARCH
MANAGER SALES
MANAGER ACCOUNTING
CLERK RESEARCH
CLERK SALES
CLERK ACCOUNTING
7 rows selected.
1 select a.job,b.loc
2 from e a,d b
3 where a.deptno=b.deptno
4* and b.LOC<>'DALLAS' and a.job='SALESMAN'
SQL> /
JOB LOC
--------- -------------
SALESMAN CHICAGO
SALESMAN CHICAGO
SALESMAN CHICAGO
SALESMAN CHICAGO
36)Select department name & location of all the employees working for CLARK.
1 select a.ename,b.dname,b.loc
2 from e a,d b
3 where a.deptno=b.deptno
4* and a.ename='CLARK'
SQL> /
ENAME DNAME LOC
---------- -------------- -----------
CLARK ACCOUNTING NEW YORK
ASSIGNMENTS ON OUTER-JOINS
39)Display all the departmental information for all the existing employees and if
a department has no employees display it as “No employees”.
40)Get all the matching & non-matching records from both the tables.
41)Get only the non-matching records from DEPT table (matching records
shouldn’t be selected).
42)Select all the employees name along with their manager names, and if an
employee does not have a manager, display him as “CEO”.
ASSIGNMENTS ON SELF-JOINS
43)Get all the employees who work in the same departments as of SCOTT
44)Display all the employees who have joined before their managers.
1 select a.ename"empname",a.hiredate,b.ename"mgr_name",b.hiredate
2 from emp a,emp b
3 where 0>(a.hiredate-b.hiredate)
4* and a.mgr=b.empno
SQL> /
6 rows selected.
45)List all the employees who are earning more than their managers.
1 select a.ename"emp_name",a.sal,b.ename"mgr_name",b.sal
2 from emp a,emp b
3 where a.sal>b.sal
4* and a.mgr=b.empno
SQL> /
select a.*
from emp a
where 1< (select count(*) from emp b where b.sal=a.sal)
select a.*
from e a,e b
where a.sal=b.sal
and b.ename='SMITH' and a.ename<>'SMITH';
48)Display employee name , his date of joining, his manager name & his
manager's date of joining.
ASSIGNMENTS ON CORRELATED SUBQUERIES
49) Write a query to get 4th max salary from EMP table
50) Write a query to get 2nd & 6th max salary from EMP table
51) Write a query to get first 3 salaries from the EMP table
select * from emp e
where 2=(select count(*) from emp b where e.sal<b.sal)
52) Write a query to get 2nd least salary from the EMP table
SELECT A.*
FROM E A
WHERE 1=(SELECT COUNT(*) FROM E B WHERE B.SAL<A.SAL);
53) Write a query to get least 3 salaries from the EMP table
SELECT A.*
FROM E A
WHERE 2=(SELECT COUNT(*) FROM E B WHERE B.SAL<A.SAL)
/
54) List all the employees whose salaries are greater than their respective
departmental average salary.