SQL Assignements[1]
Short Description
Hi it is very imp once you solve these queries...
Description
Sample Tables
TABLE :
DEPT
DEPTNO DNAME
--TABLE:
LOC
10
ACCOUNTING NEW YORK
20
RESEARCH
30
SALES
40
OPERATIONS
DALLAS CHICAGO BOSTON
EMP
EMPNO ENAME JOB 7369 SMITH CLERK 7499 ALLEN SALESMAN
MGR 7902 7698
HIREDATE 17-Dec-80 20-Feb-81
SAL 800 1600
COMM 300
DEPTNO 20 30
500
30
7521
WARD
SALESMAN
7698
22-Feb-81
1250
7566
JONES
MANAGER
7839
2-Apr-81
2975
7654 MARTIN SALESMAN
7698
28-Sep-81
1250
7698
BLAKE
MANAGER
7839
1-May-81
2850
30
7782
CLARK
MANAGER
7839
9-Jun-81
2450
10
7788
SCOTT
ANALYST
7566
9-Dec-82
3000
20
7839
KING
PRESIDENT
17-Nov-81
5000
10
20 1400
0
30
7844 TURNER SALESMAN
7698
8-Sep-81
1500
30
7876
ADAMS
CLERK
7788
12-Jan-83
1100
20
7900
JAMES
CLERK
7698
3-Dec-81
950
30
7902
FORD
ANALYST
7566
3-Dec-81
3000
20
7934
MILLER
CLERK
7782
23-Jan-82
1300
10
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 don’t 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 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 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(*) 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 id101; 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
ANALYST CLERK ANALYST
7566 19-APR-87 7788 23-MAY-87 7566 03-DEC-81
3000 1100 3000
20 20 20
5 rows selected. 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 2 WHERE SAL 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 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
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
JOB
MGR HIREDATE
SAL
COMM
DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369
SMITH
7566
JONES
7876
ADAMS
7902
FORD
CLERK
7902
17-DEC-80
800
20
2975
20
MANAGER
7839 02-APR-81
CLERK
7788 23-MAY-87
1100
20
ANALYST
7566 03-DEC-81
3000
20
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
HIREDATE mgr_name HIREDATE
---------- --------- ---------- --------ALLEN
20-FEB-81 BLAKE
01-MAY-81
WARD
22-FEB-81 BLAKE
01-MAY-81
JONES
02-APR-81 KING
17-NOV-81
CLARK
09-JUN-81 KING
17-NOV-81
BLAKE
01-MAY-81 KING
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
SAL mgr_name
SAL
---------- ---------- ---------- ---------SCOTT
3000 JONES
2975
FORD
3000 JONES
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
JOB
MGR HIREDATE
SAL COMM
DEPTNO
------ ---------- --------- ---------- --------- ---------- ---------- ---------7369
SMITH
CLERK
902 17-DEC-80
13000
20
7782
CLARK
MANAGER
7839 09-JUN-81
13000
10
7788
SCOTT ANALYST
7566 19-APR-87
4000
20
7839
KING
PRESIDENT
14500
10
7902
FORD
ANALYST
4000
20
14500
10
7934 MILLER
CLERK
17-NOV-81 7566 03-DEC-8 7782 23-JAN-82
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
JOB
MGR HIREDATE
SAL
COMM
DEPTNO
----- ---------- --------- ---------- --------- ---------- ---------- ---------7782 CLARK
MANAGER
7839 09-JUN-81
13000
10
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
View more...
Comments