Sunteți pe pagina 1din 12

Sample Tables

TABLE : DEPT

DEPTNO DNAME LOC

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

TABLE : EMP

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7369 SMITH CLERK 7902 17-Dec-80 800 20
7499 ALLEN SALESMAN 7698 20-Feb-81 1600 300 30
7521 WARD SALESMAN 7698 22-Feb-81 1250 500 30
7566 JONES MANAGER 7839 2-Apr-81 2975 20
7654 MARTIN SALESMAN 7698 28-Sep-81 1250 1400 30
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
7844 TURNER SALESMAN 7698 8-Sep-81 1500 0 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.
Ans: select * from emp where sal>=2500
and deptno=20;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


------ ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20

2.Display all the managers working in 20 & 30 department.


Ans: select * from emp
where job='MANAGER' and deptno in (20,30);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


------ ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 KE MANAGER 7839 01-MAY-81 2850 30

3.Display all the managers who don’t have a manager


Ans: select * from emp
where job='MANAGER' and mgr is null;

no rows selected

4.Display all the employees who are getting some commission with their
designation is neither MANANGER nor ANALYST
Ans: select * from emp
where comm >0 and job not in ('MANAGER','ANALYST');

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ---------- --------- ---------- --------- ---------- ---------- -----------------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 TIN SALESMAN 7698 28-SEP-81 1250 1400 30

5.Display all the ANALYSTs whose name doesn’t ends with ‘S’
Ans: select * from emp
where job='ANALYST' and ename not like '%S';

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


--------- ---------- --------- ---------- --------- ---------- ---------- ------------------
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 D ANALYST 7566 03-DEC-81 3000 20
6.Display all the employees whose naming is having letter ‘E’ as the last but one
character
Ans: select * from emp
where ename like '%E_';

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


-------- ---------- --------- ---------- --------- ---------- ---------- -------------------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 LER CLERK 7782 23-JAN-82 1300 10

7.Display all the employees who total salary is more than 2000.
(Total Salary = Sal + Comm)
Ans: select * from emp
where (sal+nvl(comm,0))>2000;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


------ ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7902 D ANALYST 7566 03-DEC-81 3000 20

8.Display all the employees who are getting some commission in department 20
& 30.
Ans: select * from emp
where comm>0 and deptno in (20,30);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


------ ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 TIN SALESMAN 7698 28-SEP-81 1250 1400 30

9.Display all the managers whose name doesn't start with A & S
Ans: select * from emp
where job='MANAGER' and
ename not like 'A%' and
ename not like 'S%'

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


-------- ---------- --------- ---------- --------- ---------- ---------- -------------------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 RK MANAGER 7839 09-JUN-81 2450 10
10. Display all the employees who earning salary not in the range of 2500 and
5000 in department 10 & 20.
Ans: select * from emp
where (sal not between 2500 and 5000)
and deptno in(20,30);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


------- ---------- --------- ---------- --------- ---------- ---------- -------------------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
ASSIGNMENTS ON GROUPING

1.Display job-wise maximum salary.


Ans: select job,max(sal) from emp
group by job;

JOB MAX(SAL)
--------- ----------
ANALYST 3000
CLERK 1300
MANAGER 2975
PRESIDENT 5000
SALESMAN 1600

2.Display the departments that are having more than 3 employees under it.
Ans: select deptno,count(*) from emp
group by deptno
having count(*)>3;

DEPTNO COUNT(*)
-------- ----------
20 5
30 6

3.Display job-wise average salaries for the employees whose employee number is
not from 7788 to 7790.
Ans: select job,avg(sal) from emp
where empno not between 7788 and 7790
group by job;

JOB AVG(SAL)
--------- ----------
ANALYST 3000
CLERK 1037.5
MANAGER 2758.33333
PRESIDENT 5000
SALESMAN 1400

4.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.
Ans: select deptno,sum(sal) from emp
where job in ('MANAGER','ANALYST')
group by deptno
having avg(sal)>=3000;

no rows selected

Consider the following table: -


Following Assignments(1 to 4) are based on the following table:

Table Name : SKILLS


ID Name
101 Oracle
102 Oracle
103 Oracle
101 Oracle
102 Java
103 Java
101 Java
102 Java
103 Java
101 Java
101 Java
101 Oracle
101 VB
102 ASP

1.Select only the duplicate records along-with their count.


Ans: select id,count(*) from skills
group by id
having count(*)>1;

ID COUNT(*)
----- ----------
101 7
102 4
103 3

2.Select only the non-duplicate records.


Ans: select id,count(*) from skills
group by id
having count(*)=1;

no rows selected

3.Select only the duplicate records that are duplicated only once.
Ans: select id,count(*) from skills
group by id
having count(*)=1;

no rows selected

4.Select only the duplicate records that are not having the id=101.
Ans: select id,count(*) from skills
where id<>101
group by id
having count(*)>=1
ID COUNT(*)
----- ----------
102 4
103 3
ASSIGNMENTS ON SUBQUERIES
1.Display all the employees who are earning more than all the managers.
Ans: 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

2.Display all the employees who are earning more than any of the managers.
Ans: SELECT * from emp
where sal >all(select sal from emp where job like 'MAN%');

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
7955 JOHN CLERK 7698 13-JAN-87 3500 0 40

3.Select employee number, job & salaries of all the Analysts who are earning
more than any of the managers.
Ans:

4.Select all the employees who work in DALLAS.


Ans: select * from emp
where deptno in
(select deptno from dept
where loc='DALLAS');

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


------ ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7566 ES MANAGER 7839 02-APR-81 2975 20

5.Select department name & location of all the employees working for CLARK.
Ans: select dname,loc from dept
where deptno in(select deptno from emp
where mgr in(select empno from emp
where ename='CLARK'));

DNAME LOC
-------------- -------------
ACCOUNTING NEW YORK

6.Select all the departmental information for all the managers


Ans: 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

7.Display the first maximum salary.


Ans: select max(sal) from emp;
MAX(SAL)
---------
500
8.Display the second maximum salary.
Ans: select max(sal) from emp
Where sal <(select max(sal) from emp);
MAX(SAL)
--------
3000

9.Display the third maximum salary.


Ans: select max(sal) from emp
Where sal <(select max(sal) from emp
Where sal <(select max(sal) from emp));
MAX(SAL)
--------
2975

10.Display all the managers & clerks who work in Accounts and Marketing
departments.
Ans: select ename from emp
where job in('MANAGER','CLERK')
AND deptno in(select deptno from dept
where dname in('ACCOUNTING','SALES'));

ENAME
---------
CLARK
MILLER
BLAKE
JAMES

11.Display all the salesmen who are not located at DALLAS.


Ans: select * from emp
where job='SALESMAN'
and deptno in
(select deptno from dept
where loc<>'DALLAS');

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


------ ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7844 NER SALESMAN 7698 08-SEP-81 1500 0 30

12.Get all the employees who work in the same departments as of


SCOTT.
Ans: select * from emp
where deptno in
(select deptno from emp
where ename='SCOTT')
and ename<>'SCOTT';

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


------ ---------- --------- ---------- ------------- ------ -------- ----------
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7369 MITH CLERK 7902 17-DEC-80 800 20

13.Select all the employees who are earning same as SMITH.


Ans: select * from emp
where sal in
(select sal from emp
where ename='SMITH')
and ename<>'SMITH';

no rows selected

14.Display all the employees who are getting some commission in


marketing department where the employees have joined only on
weekdays.
Ans: select * from emp
where comm>0
and deptno in
(select deptno from dept
where dname='SALES')
and to_char(hiredate,'dy') in('SAT','SUN');

15.Display all the employees who are getting more than the average
salaries of all the employees.
Ans: select * from emp
where sal>(select avg(sal)
from emp);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
------ ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
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
ASSIGNMENTS ON JOINS

ASSIGNMENTS ON EQUI-JOINS

1.Display all the managers & clerks who work in Accounts and Marketing
departments.
Ans: select A.*
from emp A join dept B
on a.deptno=b.deptno
and job in('MANAGER','CLERK')
and dname in('ACCOUNTING','SALES');

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
------- ---------- --------- ---------- --------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7900 JAMES CLERK 7698 03-DEC-81 950 30

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

2) Select department name & location of all the employees working for CLARK.

3) Select all the departmental information for all the managers

4) Select all the employees who work in DALLAS.

5) Delete the records from the DEPT table that don’t have matching
records in EMP

ASSIGNMENTS ON OUTER-JOINS

6) Display all the departmental information for all the existing employees and if
a department has no employees display it as “No employees”.

7) Get all the matching & non-matching records from both the tables.

8) Get only the non-matching records from DEPT table (matching records
shouldn’t be selected).

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

10)Get all the employees who work in the same departments as of SCOTT
11)Display all the employees who have joined before their managers.

12)List all the employees who are earning more than their managers.

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

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

15)Display employee name , his date of joining, his manager name & his
manager's date of joining.

S-ar putea să vă placă și