Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College

41
Oracle Assessment A WORK REPORT SUBMITTED IN PARTIAL FULLFILLMENT OF THE REQUIREMENT FOR THE DEGREE Bachelor of Computer Application Dezyne E’cole College 106/10, CIVIL LINES AJMER RAJASTHAN -305001 (INDIA) (JUNE, 2015) www.dezyneecole.com SUBMITTED BY DIVYANSH MEHTA CLASS: BCA 3 RD YEAR

Transcript of Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College

Oracle Assessment

A WORK REPORT SUBMITTED

IN PARTIAL FULLFILLMENT OF THE REQUIREMENT FOR THE DEGREE

Bachelor of Computer Application

Dezyne E’cole College

106/10, CIVIL LINES

AJMER

RAJASTHAN -305001 (INDIA)

(JUNE, 2015)

www.dezyneecole.com

SUBMITTED BY

DIVYANSH MEHTA

CLASS: BCA 3RD YEAR

1

PROJECT ABSTRACT

I am DIVYANSH MEHTA Student of 3rd year doing my Bachelor Degree in Computer

Application.

In the following pages I gave compiled my work learnt during my 3rd year at college.

The subject is RDBMS. These assessments are based on Relational Database

Management System that is useful to work with front end (user interface) application.

Take example of an online form if the user filling up an online form (E.g. SBI Form,

Gmail registration Form) on to the internet whenever he/she clicks on the submit

button the field value is transfer to the backend database and stored in Oracle, MS-

Access, My SQL, SQL Server.

The purpose of a database is to store and retrieve related information later on. A

database server is the key to solving the problems for information management.

In these assessments we are using Oracle 10g as the Relation Database Software.

The back-end database is a database that is accessed by user indirectly through an

external application rather than by application programming stored within the

database itself or by low level manipulation of the data (e.g. through SQL

commands).

Here in the following assessment we are performing the following operations:

1. Creating tables to store the data into tabular format(schemas of the data base)

2. Fetching the data from the database(Using Select Query)

3. Joining the multiple data tables into one(To reduces the redundancy of the data)

4. Nested Queries(Queries within Queries)

2

Contents

Select Clause .......................................................................................................................................... 8

Group, Having ........................................................................................................................................ 19

Functions ............................................................................................................................................... 25

Nested & Correlated .................................................................................................................. 34

Coverage Joins ............................................................................................................................ 37

3

1 Create an Employee Table with Following Fields:

Solution:

CREATE TABLE EMPLOYEE

(EMPNO NUMBER (4),

ENAME VARCHAR2 (20),

DEPTNO NUMBER (2),

JOB VARCHAR2 (20),

SAL NUMBER (5),

COMM NUMBER (4),

MGR NUMBER (4));

How to Display Table Structure?

Solution:

Desc Employee

Output:

Insert At least 5 records.

FIELDS DATA TYPE SIZE

EMPNO NUMBER 4

ENAME VARCHAR2 20

DEPTNO NUMBER 2

JOB VARCHAR2 20

SAL NUMBER 5

COMM NUMBER 4

MGR NUMBER 4

HIREDATE DATE -

4

Solution:

Insert into employee

(EMPNO, ENAME, DEPTNO, JOB, SAL, COMM, MGR, HIREDATE)

values(:epno,:ena,:dptno,:job,:sal,:comm,:mgr,:hd)

How to fetch Records

Solution:

Select * from employee

Output:

2 Create a Depart Table (Dept) with Following Fields:

FIELDS DATA TYPE SIZE

DEPTNO NUMBER 2

DNAME VARCHAR2 20

LOC (location) VARCHAR2 20

5

Solution:

Create table depart

(DEPTNO NUMBER (2),

DNAME VARCHAR2 (20),

LOC VARCHAR2 (20))

How to display table structure

Solution:

Desc depart

Output:

Insert At least 5 records.

Solution:

Insert into depart

(DEPTNO, DNAME, LOC)

values (:depno,:dname,:loc)

Output:

How to fetch record from depart table

Solution:

Select * from depart

Output:

6

3 Create a SalGrd Table with Following Fields:

FIELDS DATA TYPE SIZE

GRADE NUMBER 1

LOSAL NUMBER 5

HISAL NUMBER 5

Create table salgrd

(GRADE NUMBER (1),

LOSAL NUMBER (5),

HISAL NUMBER (5))

Display table structure

Solution:

Desc salgrd

Output:

Insert At least 5 records.

Solution:

Insert into salgrd

(GRADE, LOSAL, HISAL)

Values (:g,:l,:h)

7

Output

How to fetch record from salgrd

Solution:

Select * from salgrd

Output:

8

SELECT STATEMENT

1. List all the information about all Employees.

Solution:

Select * from employee

Output:

2. Display the Name of all Employees along with their Salary.

Solution:

Select ename, sal from employee

Output:

3. List all the Employee Names who is working with Department Number is 20.

Solution:

Select ename from EMP where deptno=2

Output:

9

4. List the Name of all ‘MANAGER’ and ‘SALESMAN’

Solution:

Select * from employee where job=’manager’ or job=’salesman’

Output:

5. Display the details of those Employees who have joined before the end of Sept.

1992

Solution:

Select * from emp where hiredate=’29-sep-1992’

Output:

6. List the Employee Name and Employee Number, who is ‘MANAGER’.

Solution:

Select ename,empno.job from emp where job=’manager’

Output:

10

7. List the Name and Job of all Employees who are not ‘CLERK’.

Solution:

Select ename, job from employee where job!=’clerk’

Output:

8. List the Name of Employees, whose Employee Number is 7369,7521,7839,7934

or 7788.

Solution:

Select ename from emp where empno in(7369,7521,7839,7934,7788)

Output:

9. List the Employee detail who does not belongs to Department Number 10 and

30.

Solution:

Select * from employee where deptno not in(10,30)

Output:

10. List the Employee Name and Salary, whose Salary is vary from 1000 to 2000.

11

Solution:

Select ename, sal from employee where sal between 1000 and 2000

Output:

11. List the Employee Names, who have joined before 30-Jun-1981 and after Dec-

1980

Solution:

Select ename from employee where hiredate<’30-jun-1981’ and hiredate>’31- dec-

1980’

Output:

12. List the Commission and Name of Employees, who are availing the

Commission.

Solution:

Select ename ,comm from employee where comm>0

Output:

12

13. List the Name and Designation (job) of the Employees who does not report to

anybody.

Solution:

Select ename ,job from employee where mgr is null

Output:

14. List the detail of the Employees, whose Salary is greater than 1000 and

Commission is NULL.

Solution:

Select * from employee where sal>1000 and comm is null

Output:

15. List the Employee details whose Name start with ‘a’.

Solution:

Select * from employee where ename like ‘s%’

Output:

13

16. List the Employee Names and Date of Joining in Descending Order of Date of

Joining. Th0e column title should be “Date Of Joining”

Solution:

Select ename,hiredate as ”Date of Joining” from employee order by “Date of Joining”

desc

Output:

17. List the Employee Name, Salary, Job and Department Number and display it in

Descending Order of Department Number, Ascending Order of Name and

Descending Order of Salary.

Solution:

Select ename,sal,job,deptno from employee order by deptno desc ,ename asc,sal

desc

Output:

18. List the Employee Name, Salary, PF, HRA, DA and Gross Salary; Order the

result in Ascending Order of Gross Salary. HRA is 50% of Salary, DA is 30%

and PF is 10%.

14

Solution:

Select ename,sal,sal+(sal*50)/100+(sal*30)/100-(sal*10)/100 as "gross sal" from

employee order by "gross sal" Asc

Output:

19. List Salesman from dept No 40.

Solution:

Select job from employee where deptno=40 and job=’salesman’

Output:

20. List Clerks from 20 and salesman from 40. In the list the lowest earning

employee must at top.

Solution:

Select job,deptno from employee where job=’clerk’ and deptno=30 or job=’salesman’

and deptno= 40 order by ename asc

Output:

21. List different departments from Employee table.

15

Solution:

Select distinct deptno from employee

Output:

22. List employees having “n” at the end of their Name

Solution:

Select * from employee where ename like’%n’

Output:

23. List employee who are not managed by anyone

Solution:

Select ename from employee where mgr is null

Output:

24. List employees who are having “TT” or “LL” in their names

Solution:

Select ename from employee where ename like ‘%TT’ or ename like ’%ll%’

Output:

16

25. List employees earning salaries below 1500 and more than 3000

Solution:

Select ename,sal from employee where sal<1500 or sal >3000

Output:

26. List employees who are drawing some commission. Display their total salary

as well.

Solution:

Select ename sal+comm from employee where comm>0

Output:

27. List employees who are drawing more sal than their comm. Only those records

should be displayed where commission is given (also sort the Output in the

descending order of commission).

Solution:

Select ename from employee where sal>comm and comm is not null order by comm.

desc

Output:

17

28. List the employees who joined the company in the month of “FEB”.

Solution:

Select ename from employee where hiredate>’1-feb 1991’ or hiredate<’28-feb-1991’

Output:

29. List employees who are working as salesman and having names of four

characters

Solution:

Select ename from employee where job=’salesman’ and ename like ‘____’

Output:

30. List employee who are managed by 7789.

Solution:

Select ename from employee where mgr=7789

Output:

18

19

GROUPING, HAVING ETC

1. List the Department number and total number of employees in each

department.

Solution:

Select deptno,count(empno) from employee group by deptno

Output:

2. List the different Job names (Designation) available in the EMP table.

Solution:

Select distinct job from employee

Output:

3. List the Average Salary and number of Employees working in Department

number 20.

Solution:

Select avg(sal),deptno,count(empno) from employee where deptno=20 group by

deptno

Output:

20

4. List the Department Number and Total Salary payable at each Department.

Solution:

Select deptno,sum(sal+comm) from employee group by deptno

Output:

5. List the jobs and number of Employees in each Job. The result should be in

Descending Order of the number of Employees.

Solution:

Select sum(sal+comm),avg(sal),min(sal),max(sal),job from employee where

deptno=10 group by job

Output:

6. List the Total salary, Maximum Salary, Minimum Salary and Average Salary of

Employees job wise, for Department number 10 only.

Solution:

Select job,count(empno) from employee group by job order by count(empno)desc

Output:

21

7. List the Average Salary of each Job, excluding ‘MANAGERS’.

Solution:

Select job,avg(sal) from employee where job!=manager group by job

Output:

8. List the Average Monthly Salary for each Job within each department.

Solution:

Select avg(sal),job,deptno from employee group by job,deptno

Output:

9. List the Average Salary of all departments, in which more than five people are

working.

Solution:

Select count(empno),avg(sal),deptno from employee group by deptno having

count(empno)>=1

22

Output:

10. List the jobs of all Employees where Maximum Salary is greater than or equal

to 1000.

Solution:

Select max(sal),job from employee group by job having max(sal)>1000

Output:

11. List the total salary and average salary of the Employees job wise, for

department number 20 and display only those rows having average salary

greater than 1000.

Solution:

Select job,sum(sal+comm),avg(sal) from employee where deptno=20 group by job

having avg(sal)>1000

Output:

12. List the total salaries of only those departments in which at least 1 employees

are working.

Solution:

23

Select sum(sal+comm),count(empno),deptno from employee group by deptno having

count(empno)>=1

Output:

13. List the Number of Employees Managed by Each Manager

Solution:

Select count(empno),mgr from emp group by mgr

Output:

14. List Average Commission Drawn by all manager

Solution:

Select avg(comm)from emp where job=’manager’ group by job

Output:

24

FUNCTIONS

1. Calculate the remainder for two given numbers. (213,9)

Solution:

Select mod(213,9) from dual

Output:

2. Calculate the power of two numbers entered by the users at run time of the

query.

Solution:

Select power(:n1,:n2)from dual

User:

Output:

3. Enter a number and check whether it is negative or positive.

Solution:

Select sign(:n1)from dual

User:

Output:

25

4. Calculate the square root for the given number. (i.e. 10000).

Solution:

Select sqrt(1000) from dual

Output:

5. Enter a float number and truncate it up to 1 and -2 places of decimal

Solution:

Select trunc(:n1,-2) from dual

User:

Output:

6. Find the rounding value of 563.456, up to 2, 0 and -2 places of decimal.

Solution:

Select round(563.456,2)from dual

Output:

26

7. Accept two numbers and display its corresponding character with the

appropriate title.

Solution:

Select chr(:n1)”first”,chr(:n2) as”sec”from dual

User:

Output:

8. Input two names and concatenate it separated by two spaces.

Solution:

Select(:n1||’ ’||:n2) from dual

User:

Output:

9. Display all the Employee names-with first character in upper case from EMP

table.

Solution:

Select initcap(ename) from employee

27

Output:

10. Display all the department names in upper and lower cases

Solution:

Select upper(dname),lower(dname) rom depart

Output:

11. Extract the character S and A from the left and R and N from the right of the

Employee name from EMP table.

Solution:

Select ename,ltrim(ename,’sa’),rtrim(ename,’rn’) from employee

Output:

12. Change all the occurrences of CL with Pin job domain.

28

Solution:

Select job,replace(job,’cl’,’pin’) from employee

Output:

13. Display the information of those Employees whose name has the second

character A.

Solution:

Select * from employee where instr(ename’a’)=2

Output:

14. Display the ASCII code of the character entered by the user

Solution:

Select ascii(:n1)from dual

User:

Output:

15. Display the Employee names along with the location of the character A in the

Employees name from EMP table where the job is Salesman

29

Solution:

Select ename,job from emp where instr(ename,’a’)<>0 and job=’salesman’

Output:

16. Find the Employee names with maximum number of characters in it.

Solution:

Select max(length(ename)) from employee

Output:

17. Display the salary of those Employees who are getting salary in three figures.

Solution:

Select sal from employee where length(sal)>0

Output:

18. Display only the first three characters of the Employees name and their H RA

(salary * .20), truncated to decimal places.

Solution:

Select substr(ename,1,3),trunc(sal*20) from employee

Output:

30

19. List all the Employee names, who have more than 20 years of experience in the

company.

Solution:

Select ename,round(months_between(sysdate,hiredate)/12)from employee where

round(months_between(sysdate,hiredate)/12)>20

Output:

20. Display the name and job for every Employee, while displaying jobs,

'CLERK'should be displayed as 'LDC' and 'MANAGER' should be displayed as

'MNGR'. The other job title should be as they are.

Solution:

Select ename,job,decode(job,’clerk’,’ldc’,’manager’,’salesman’) from employee

Output:

31

21. Display Date in the Following Format Tuesday 31 December 2002

Solution:

Select to_char(sysdate,’day yy-month-yyyy’)from dual

Output:

22. Display the Sunday coming After 3 Months from Today’s Date.

Solution:

Select next_day(add_months(sysdate,3),1) from dual

Output:

32

Nested and Correlated Sub queries

1. List Employees who are working in the Sales Department (Use Nested)

Solution:

Select * from employee where deptno=(select deptno from depart where

dname=’sales’)

Output:

2. List Departments in which at least one employee is working (Use Nested)

Solution:

Select dname from depart where deptno in(select deptno from employee)

Output:

3. Find the Names of employees who do not work in the same department of

Scott.

Solution:

select * from emp

where deptno<>(select deptno from emp

where ename='mark')

Output:

33

4. List departments (dept details) in which no employee is working (use nested)

Solution:

Select * from depart

where deptno<>all(select deptno from employee)

Output:

5. List Employees who are drawing the Salary more than the Average salary of

DEPTNO 20. Also ensure that the result should not contain the records of

DEPTNO 20

Solution:

Select * from employee

Where sal > (select avg(sal) from employee where deptno=20)and deptno!=20

Output:

6. List Employee names drawing Second Maximum Salary

Solution:

Select ename from EMP

Where sal=(select max(sal) from emp where sal<(select max(sal) from emp))

Output:

34

7. List the Employee Names, Job & Salary for those employees who are drawing

minimum salaries for their department (Use Correlated)

Solution:

Select ename,job,sal from employee e

Where sal=(select min(sal) from employee f where e.deptno=f.deptno)

Output:

8. List the highest paid employee for each department using correlated sub

query.

Solution:

Select ename,job,sal from employee e

Where sal=(select max(sal) from employee f where e.deptno=f.deptno)

Output:

9. List Employees working for the same job type as of BLAKE and drawing more than

him (use Self Join)

Solution:

35

Select e.* from employee e join employee f on e.job=f.job where e.sal > f.sal and

f.ename=’blake’

Output:

36

Coverage Joins

1. List Employee Name, Job, Salary, Grade & the Location where they are working

Solution:

select ename,job,sal,grade,loc from employee join salgrd

on sal between losal and hisal

join depart

Output:

2. Count the Employees For Each Salary Grade for Each Location

Solution:

select count(empno),grade,loc from employee join salgrd

on sal between losal and hisal

join depart

using(deptno)

group by grade,loc

Output:

3. List the Average Salary for Those Employees who are drawing salaries of

grade2

37

Solution:

Select avg(sal),grade from employee join salgrd

on sal between losal and hisal where grade=2 group by grade

Output:

4. List Employee Name, Job, Salary Of those employees who are working in

Accounting or Research department but drawing salaries of grade 3

Solution:

Select ename,job,sal,grade from employee join salgrd

on sal between losal and hisal where job in('clerk','manager') and grade=1

Output:

5. List employee Names, Manager Names & also Display the Employees who are

not managed by anyone

Solution:

Select e.ename employee,m.ename manager

from employee e left outer join employee m

on m.empno=e.mgr

Output:

6. List Employees who are drawing salary more than the salary of SCOTT

38

Solution:

select e.ename vish,m.ename

from employee e join employee m

on m.sal<e.sal

where m.ename='mark'

Output:

7. List Employees who have joined the company before their managers

Solution:

Select e.* from employee e join employee m on e.empno=m.mgr

where e.hiredate>m.hiredate

Output:

8. List Employee Name, Job, Salary, Department No, Department name and

Location Of all employees Working at AJMER

Solution:

Select ename,job,sal,deptno,dname,loc from employee join depart using(deptno)

where loc='ajmer'

Output:

9. List Employee Name, Job, Salary, Hire Date and Location Of all employees

reporting in Accounting or Sales Department

Solution:

39

Select ename,job,sal,hiredate,loc from

employee join depart using(deptno)

where dname='accounting' or dname='sales'

Output:

10. List Employee Name, Job, Salary, Department Name, Location for Employees

drawing salary more than 2000 and working at AJMEr

Solution:

Select ename, job,sal,dname,loc from employee join depart using(deptno) where

sal<3000 and loc='ajmer'

Output:

11. List Employee Name, Job, Salary, Department Name, Location Of all

employees, also list the Department Details in which no employee is working

Solution:

Select e.ename,e.sal,d.dname,d.loc from employee e right outer join depart d on

e.deptno=d.deptno

Output:

12. List all Employee details and also calculate the Average Salary and Total Salary

Given to All Employee.

40

Solution:

Select sum(sal+comm),avg(sal),empno,ename,job,sal,deptno from employee group

by empno,ename,job,sal,deptno

Output: