You are on page 1of 17

Sample Tables DEPT

TABLE :

DEPTNO DNAME 10 20 30 40

LOC

ACCOUNTING NEW YORK RESEARCH SALES OPERATIONS DALLAS CHICAGO BOSTON

--TABLE:

EMP
MGR 7902 7698 7698 7839 7698 7839 7839 7566 7698 7788 7698 7566 7782 HIREDATE 17-Dec-80 20-Feb-81 22-Feb-81 2-Apr-81 28-Sep-81 1-May-81 9-Jun-81 9-Dec-82 17-Nov-81 8-Sep-81 12-Jan-83 3-Dec-81 3-Dec-81 23-Jan-82 SAL 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 0 1400 COMM 300 500 DEPTNO 20 30 30 20 30 30 10 20 10 30 20 30 20 10

EMPNO ENAME JOB 7369 SMITH CLERK 7499 ALLEN SALESMAN 7521 7566 7698 7782 7788 7839 7876 7900 7902 7934 WARD JONES BLAKE CLARK SCOTT KING ADAMS JAMES FORD MILLER SALESMAN MANAGER MANAGER MANAGER ANALYST PRESIDENT CLERK CLERK ANALYST CLERK

7654 MARTIN SALESMAN

7844 TURNER SALESMAN

ASSIGNMENTS ON OPERATORS
1) Display all the employees who are getting 2500 and excess salaries in department 20. Select * from EMP Where Sal>2500 and deptno=20; 2) Display all the managers working in 20 & 30 department. Select job, deptno from emp Where job=Manager and deptno in(20,30); 3) Display all the managers who dont have a manager Select mgr from emp Where mgr is null; 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 doesnt 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 Select * from EMP Where ename not like (%e_); 7) Display all the employees who total salary is more than 2000. (Total Salary = Sal + Comm) Select * from EMP Where (sal+comm)>2000; 8) Display all the employees who are getting some commission in department 20 & 30. Select * from EMP Where Comm is not null and deptno in (20,30);

9) Display all the managers whose name doesn't start with A & S Select * from EMP Where job=Manager and INstr (ename, A, 1, 1) =0 and INstr (ename, S, 1, 1) =0; or ename like [!as]%; 10) Display all the employees who earning salary not in the range of 2500 and 5000 in department 10 & 20. Select * from EMP Where Sal not between 2500 and 5000 and deptno in (10,20);

ASSIGNMENTS ON GROUPING
11) Display job-wise maximum salary. Select job,max(sal) from emp group by job; 12) Display the departments that are having more than 3 employees under it. Select deptno, count(*) from emp Group by deptno having count(*)>3; 13) Display job-wise average salaries for the employees whose employee number is not from 7788 to 7790. Select job, avg (sal)avg_sal from emp Where empno not between 7788 and7790 And group by job; 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. Select job, deptno, sum(sal)tatal_sal, avg(sal)avg_sal Where job in (Analyst, Manager) group by job, deptno having avg(sal)>=3000;

Consider the following table: Assignment 5,6,7 & 8 are based on the following table Table Name : SKILLS ID 101 102 S103 101 102 103 101 102 Name Oracle Oracle Oracle Oracle Java Java Java Java

103 101 101 101 101 102 15)

Java Java Java Oracle VB ASP

Select only the duplicate records along-with their count.

1 select id,name,count(*) from skills 2* group by id,name having count(*)>1 SQL> / ID NAME COUNT(*) ---------- --------------- ---------101 java 3 101 oracle 4 102 java 2 103 java 2 16) Select only the non-duplicate records.

SQL> select id,name,count(*) from skills 2 group by id,name having count(*)<=1; 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. SQL> select id, name, count (*) 2 from skills 3 group by id, name 4 having count (*) = 2; ID NAME COUNT (*) ---------- ---------- ---------102 JAVA 2 103 JAVA 2 18) Select only the duplicate records that are not having the id=101.

SQL> select id,name,count(*) from skills 2 group by id,name having count(*)>1 and 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. SELECT *FROM EMP WHERE SAL> (SELECT MAX (SAL) FROM EMP WHERE JOB=MANAGER); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ------ ---------- --------- ---------- --------- ---------- ---------- ---------7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7902 FORD ANALYST 7566 03-DEC-81 3000 20 20)Display all the employees who are earning more than any of the managers. SQL> SELECT *FROM EMP WHERE SAL>(SELECT MIN(SAL) FROM EMP 2 WHERE JOB='MANAGER'); EMPNO ENAME ---------- ---------7566 JONES 7698 BLAKE 7788 SCOTT 7839 KING 7902 FORD JOB MGR HIREDATE SAL COMM DEPTNO --------- ---------- --------- ---------- ---------- ---------MANAGER 7839 02-APR-81 2975 20 MANAGER 7839 01-MAY-81 2850 30 ANALYST 7566 19-APR-87 3000 20 PRESIDENT 17-NOV-81 5000 10 ANALYST 7566 03-DEC-81 3000 20

21)Select employee number, job & salaries of all the Analysts who are earning more than any of the managers. SQL> SELECT EMPNO,JOB,SAL FROM EMP WHERE JOB='ANALYST' AND 2 SAL>(SELECT MIN(SAL) FROM EMP WHERE JOB='MANAGER'); EMPNO JOB SAL ---------- --------- ---------7788 ANALYST 3000 7902 ANALYST 3000 22)Select all the employees who work in DALLAS. SQL> SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE LOC='DALLAS'); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 900 20 7566 JONES MANAGER 7839 02-APR-81 2975 20

7788 SCOTT 7876 ADAMS 7902 FORD 5 rows selected.

ANALYST CLERK ANALYST

7566 19-APR-87 7788 23-MAY-87 7566 03-DEC-81

3000 1100 3000

20 20 20

23)Select department name & location of all the employees working for CLARK. SQL> SELECT DEPTNO,DNAME,LOC FROM DEPT 2 WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE ENAME='CLARK'); DEPTNO DNAME LOC ---------- -------------- ------------10 ACCOUNTING NEW YORK 1 row selected. 24)Select all the departmental information for all the managers SQL> SELECT * FROM DEPT WHERE DEPTNO IN(SELECT DEPTNO FROM EMP WHERE JOB='MANAGER') DEPTNO DNAME LOC ---------- -------------- ------------10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 3 rows selected. 25)Display the first maximum salary. SQL> SELECT *FROM EMP 2 WHERE SAL=(SELECT MAX(SAL) FROM EMP); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7839 KING PRESIDENT 17-NOV-81 5000 10 1 row selected. 26)Display the second maximum salary. SQL> SELECT MAX(SAL) FROM EMP 2 WHERE SAL<(SELECT MAX(SAL) FROM EMP); MAX(SAL) ---------3000 1 row selected.

27)Display the third maximum salary. SQL> SELECT MAX(SAL) FROM EMP 2 WHERE SAL<(SELECT MAX(SAL) FROM EMP 3 WHERE SAL<(SELECT MAX(SAL) FROM EMP)); MAX(SAL) ---------2975 1 row selected.

28) Display all the managers & clerk who work in Accounting and Marketing
(SALES) departments. SQL> select * from emp 2 where job='MANAGER' OR JOB='CLERK' AND DEPTNO IN 3 (SELECT DEPTNO FROM DEPT WHERE DNAME IN('ACCOUNTING','SALES')); EMPNO ENAME ---------- ---------7566 JONES 7698 BLAKE 7782 CLARK 7900 JAMES 7934 MILLER JOB MGR HIREDATE SAL COMM DEPTNO --------- ---------- --------- ---------- ---------- ---------MANAGER 7839 02-APR-81 2975 20 MANAGER 7839 01-MAY-81 2850 30 MANAGER 7839 09-JUN-81 2450 10 CLERK 7698 03-DEC-81 950 30 CLERK 7782 23-JAN-82 1300 10

29)Display all the salesmen who are not located at DALLAS.

30) Get all the employees who work in the same departments as of SCOTT. SQL> select empno,ename,job,deptno 2 from emp 3 where job='SALESMAN' AND 4 DEPTNO IN(SELECT DEPTNO FROM DEPT WHERE LOC<>'DALLAS'); EMPNO ENAME JOB DEPTNO ---------- ---------- --------- ---------7499 ALLEN SALESMAN 30 7521 WARD SALESMAN 30 7654 MARTIN SALESMAN 30 7844 TURNER SALESMAN 30

31)

Select all the employees who are earning same as SMITH.

SQL> select * 2 from emp 3 where sal=(select sal from emp where ename='SMITH'); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 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. SQL> select a.job,b.dname 2 from e a,d b 3 where a.deptno=b.deptno 4 and a.job in('MANAGER','CLERK'); JOB DNAME --------- -------------CLERK RESEARCH MANAGER RESEARCH MANAGER SALES MANAGER ACCOUNTING CLERK RESEARCH CLERK SALES CLERK ACCOUNTING 7 rows selected. 35)Display all the salesmen who are not located at DALLAS. 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 37)Select all the departmental information for all the managers SQL> select a.job,b.deptno,b.dname,b.loc 2 from e a,d b 3 where a.deptno=b.deptno 4 and a.job='MANAGER'; JOB DEPTNO DNAME LOC --------- ---------- -------------- --------MANAGER 10 ACCOUNTING NEW YORK MANAGER 20 RESEARCH DALLAS MANAGER 30 SALES CHICAGO 38)Select all the employees who work in DALLAS. SQL> select a.*,b.loc 2 from e a,d b 3 where a.deptno=b.deptno 4 and b.loc='DALLAS'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO LOC ---------- ---------- --------- ---------- --------- ---------- ---------- -------------------7369 SMITH CLERK 7902 17-DEC-80 9000 20 DALLAS 7566 JONES MANAGER 7839 02-APR-81 3975 20 DALLAS 7788 SCOTT ANALYST 7566 19-APR-87 4000 20 DALLAS 7876 ADAMS CLERK 7788 23-MAY-87 5000 20 DALLAS 7902 FORD ANALYST 7566 03-DEC-81 4000 20 DALLAS

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

SQL> select a.* 2 from emp a,emp b 3 where a.deptno=b.deptno 4 and b.ename='SCOTT' 5 AND a.ename<>'SCOTT'; EMPNO ENAME 7369 7566 7876 7902 SMITH JONES ADAMS FORD JOB CLERK MANAGER CLERK ANALYST MGR HIREDATE 7902 17-DEC-80 SAL 800 2975 1100 3000 COMM DEPTNO 20 20 20 20 ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7839 02-APR-81 7788 23-MAY-87 7566 03-DEC-81

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> / empname ALLEN WARD JONES CLARK BLAKE HIREDATE mgr_name HIREDATE 20-FEB-81 BLAKE 22-FEB-81 BLAKE 02-APR-81 KING 09-JUN-81 KING 01-MAY-81 KING 01-MAY-81 01-MAY-81 17-NOV-81 17-NOV-81 17-NOV-81

---------- --------- ---------- ---------

SMITH

17-DEC-80 FORD

03-DEC-81

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> / emp_name SCOTT FORD SAL mgr_name 3000 JONES 3000 JONES SAL 2975 2975

---------- ---------- ---------- ----------

46)Fetch all the employees who are earning same salaries.

select a.* from emp a where 1< (select count(*) from emp b where b.sal=a.sal) EMPNO ENAME 7369 7782 7788 7839 7902 SMITH CLARK KING FORD JOB CLERK MANAGER PRESIDENT ANALYST CLERK MGR HIREDATE 902 17-DEC-80 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7566 03-DEC-8 7782 23-JAN-82 SAL COMM 13000 13000 4000 14500 4000 14500 DEPTNO 20 10 20 10 20 10

------ ---------- --------- ---------- --------- ---------- ---------- ----------

SCOTT ANALYST

7934 MILLER

47)Select all the employees who are earning same as SMITH.

select a.* from e a,e b where a.sal=b.sal and b.ename='SMITH' and a.ename<>'SMITH'; EMPNO ENAME 7782 CLARK JOB MANAGER MGR HIREDATE 7839 09-JUN-81 SAL 13000 COMM 10 DEPTNO

----- ---------- --------- ---------- --------- ---------- ---------- ----------

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

select * from emp e where 3=(select count(*) from emp b where e.sal<b.sal)

50)

Write a query to get 2nd & 6th max salary from EMP table

select * from emp e where 1=(select count(*) from emp b where e.sal<b.sal) union select * from emp e where 5=(select count(*) from emp b where e.sal<b.sal)

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); EMPNO ENAME 7844 TURNER JOB SALESMAN MGR HIREDATE 7698 08-SEP-81 SAL 2000 COMM 0 DEPTNO 30

----- ---------- --------- ---------- --------- ---------- ---------- ----------

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) / EMPNO ENAME 7499 ALLEN JOB SALESMAN MGR HIREDATE 7698 20-FEB-81 SAL 2600 COMM 300 DEPTNO 30

------ ---------- --------- ---------- --------- ---------- ---------- ----------

54) List all the employees whose salaries are greater than their respective departmental average salary.

You might also like