Pooja Bijawat,Bachelor Degree in Computer Application

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

Transcript of Pooja Bijawat,Bachelor Degree in Computer Application

Oracle Assessment

A WORK REPORT SUBMITTED

IN PARTIAL FULLFILLMENT OF THE REQUIREMENT FOR THE DEGREE

Bachelor of Computer Application

Dezyne E’cole College

106/0, CIVIL LINES

AJMER

RAJASTHAN - 305001 (INDIA)

(JUNE, 2015)

www.dezyneecole.com

SUBMITTED BY

POOJA BIJAWAT

CLASS: BCA 3RD YEAR

1

PROJECT ABSTRACT

I am POOJA BIJAWAT 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 assessment 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 Statement……………………………………………….………………………………………………………………………………7

Grouping, Having……………………………………………………………………………….………………………………….………...17

Functions…………..……………………………………………………………………………………………………….…….…..24

Joins………………………………………………………………………………………………………………………………………33

Nested and Correlated Subqueries………………………………………………………………………………………..40

3

1. Create an Employee Table (Emp) with Following Fields:

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 -

Solution:

Create table Emp

(EMPNO number(4),ENAME varchar2(20),DEPTNO number(2),JOB varchar2(20),

SALARY number(5),COMM number(4),MGR number(4),HIREDATE date);

Desc Emp

Output:

Insert Atleast 5 records.

Solution:

Insert into Emp(EMPNO,ENAME,DEPTNO,JOB,SALARY,COMM,MGR,

HIREDATE)

values(:Empno,:Ename,:Deptno,:Job,:Salary,:Comm,:Mgr,:Hiredate)

Output:

4

How to fetch the record.

Solution:

Select * from Emp

Output:

2. Create a Department Table (Dept) with Following Fields:

FIELDS DATA TYPE SIZE

DEPTNO NUMBER 2

DNAME VARCHAR2 20

LOC (location) VARCHAR2 20

Solution:

Create table Dept

(DEPTNO number(4),DNAME varchar2(20),LOCATION varchar (20))

5

Desc Dept

Output:

Insert Atleast 5 records.

Solution:

Insert into Dept (DEPTNO,DNAME,LOCATION)

values(:DEPTNO,:DNAME,:LOCATION)

Output:

How to fetch the record.

Solution:

Select * from Dept

Output:

3. Create a SalGrade Table with Following Fields:

FIELDS DATA TYPE SIZE

GRADE NUMBER 1

6

LOSAL NUMBER 5

HISAL NUMBER 5

Solution:

Create table SalGrade(GRADE number(1),LOSAL number(5),HISAL number(5))

Desc SalGrade

Output:

Insert Atleast 5 records.

Solution:

Insert into SalGrade(GRADE,LOSAL,HISAL)

values(:GRADE,:LOSAL,:HISAL)

Output:

How to fetch the record.

Solution:

Select * from SalGrade

Output:

7

SELECT STATEMENT

1. List all the information about all Employees. Solution: Select * from Emp Output:

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

Solution: Select ename, salary from Emp Output:

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

Solution: Select * from Emp where deptno=20 Output:

8

4. List the Name of all ‘Analyst’ and ‘Salesman’. Solution: Select ename from Emp where job=’Analyst’ or job='Salesman' Output:

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

1981. Solution: Select * from Emp where hiredate<='30-sep-1981' Output:

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

Solution: Select ename,empno from Emp where job='MANAGER' Output:

9

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

Solution: Select ename,job from Emp 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 (7788,7369,7521,7839,7934) Output:

9. List the Employee detail who does not belongs to Department Number 10 and 30. Solution: Select * from Emp where deptno not in(10,30) Output:

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

Solution:

10

Select ename, salary from Emp where salary between '1000' and '2000'

Output:

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

Solution: Select ename from Emp where hiredate <= '30-jun-1981' or hiredate >= '31-dec-1981' Output:

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

Solution: Select ename, comm from Emp where comm>0 Output:

11

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

anybody.

Solution: Select distinct job, ename from Emp where mgr is null Output:

14. List the detail of the Employees, whose Salary is greater than 2000 and Commission is NULL. Solution: Select * from Emp where salary >2000 and comm is null Output:

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

Solution: Select * from Emp where ename like 'S%' Output:

16. List the Employee Names and Date of Joining in Descending Order of Date of Joining. The column title should be “Date Of Joining”.

12

Solution: Select ename,hiredate from Emp order by hiredate 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, salary, job,deptno from Emp order by deptno desc, ename asc, salary 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%.

Solution: Select ename,salary,salary+salary*50/100+salary*30/10-salary*10/100 as "Gross Salary" from Emp order by "Gross Salary" asc

13

Output:

19. List Salesman from dept No 30. Solution: Select * from Emp where job= 'Salesman' and deptno=30 Output:

20. List Clerks from 20 and salesman from 30. In the list the lowest earning employee must at top. Solution: Select * from Emp where job= 'Clerk' and deptno=20 or job='Salesman' and deptno=30 Output:

21. List different departments from Employee table.

Solution: Select distinct deptno from Emp Output:

14

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

Solution: Select * from Emp where ename like '%r' Output:

23. List employee who are not managed by anyone.

Solution: Select * from Emp where mgr is null Output:

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

Solution: Select * from Emp where ename like '%tt%' or ename like '%ll%' Output:

25. List employees earning salaries below 5000 and more than 30000.

15

Solution: Select * from Emp where salary<5000 or salary>30000 Output:

26. List employees who are drawing some commission. Display their total salary as well. Solution: Select empno,ename,deptno,job,salary,comm,mgr,hiredate, sum(salary+comm) from Emp where comm>0 Group by empno,ename,deptno,job,salary,comm,mgr,hiredate Output:

27. List employees who are drawing more commission than their salary. Only those records should be displayed where commission is given (also sort the output in the descending order of commission).

Solution: Select * from Emp where comm>salary Output:

16

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

Solution: Select * from Emp where hiredate<='1-feb-1981' Output:

29. List employees who are working as salesman and having names of four characters. Solution: Select * from Emp where job='Salesman' and ename like'____' Output:

30. List employee who are managed by 7839. Solution: Select * from Emp where mgr=7839 Output:

17

GROUPING, HAVING ETC.

1. List the Department number and total number of employees in each department. Solution: Select count(empno),deptno from Emp group by deptno Output:

2. List the different Job names (Designation) available in the EMP table. Solution: Select distinct job from Emp Output:

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

Solution: Select AVG(salary), deptno,count(empno) from Emp where deptno =20 group by deptno Output:

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

18

Solution: Select sum(salary+comm), deptno from Emp 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 job, count(empno) from Emp group by job order by count(empno) desc

Output:

6. List the Total salary, Maximum Salary, Minimum Salary and Average Salary of Employees job wise, for Department number 10 only. Solution: Select sum(salary+comm),max(salary),min(salary),avg(salary), job,deptno from Emp where deptno=10 group by job,deptno Output:

19

7. List the Average Salary of each Job, excluding ‘Manager’ Solution: Select avg(salary), job from Emp where job!='Manager' group by job

Output:

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

Solution: Select avg(salary), job, deptno from Emp group by job,deptno Output:

9. List the Average Salary of all departments, in which more than two people are working. Solution: Select avg(salary), deptno from Emp group by deptno having count (empno)>2 Output:

20

10. List the jobs of all Employees where Maximum Salary is greater than or equal to 5000. Solution: Select job from Emp group by job having max(salary)>=5000 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 sum(salary+comm),avg(salary),job,deptno from Emp where deptno=20 group by job, deptno having avg(salary)>1000 Output:

12. List the total salaries of only those departments in which at least 3 employees are working.

Solution: Select sum(salary+comm),deptno,count(empno) from Emp group by deptno having count(empno)=3

21

Output:

13. List the Number of Employees Managed by Each Manager Solution: Select count(empno) from Emp group by mgr Output:

14. List Average Commission Drawn by all Salesman Solution: Select avg(comm),job from Emp where job='Salesman' group by job Output:

22

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(:n,:n1) from dual; Ans:

Output:

3. Enter a number and check whether it is negative or positive. Solution: Select sign (:n) from dual Ans:

Output:

23

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

Solution: Select sqrt (10000) from dual; Output:

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

Solution: Select trunc (:n,1) from dual; Ans:

Output:

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

Solution: Select round (563.456,2), round (563.456,0) , round (563.456,-2) from dual Output:

7. Accept two numbers and display its corresponding character with the appropriate title. Solution: Select chr(:n1) "First", chr(:n2) "Second" from dual

24

Ans:

Output:

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

Solution: Select(:name1)||' '||(:name2) from dual Ans:

Output:

9. Display all the Employee names-with first character in upper case from EMP table. Solution: Select initcap(ename) from Emp Output:

25

10. Display all the department names in upper and lower cases. Solution: Select upper(dname),lower(dname) from Dept 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 ltrim (ename, 'S'), ltrim (ename,'A'), rtrim (ename,'r'), rtrim(ename,'n') from Emp Output:

26

12. Change all the occurrences of CL with P in job domain. Solution: Select job, replace (job,'Cl','P') from Emp Output:

13. Display the information of those Employees whose name has the second character A. Solution: Select ename,instr(ename,'a') from Emp where instr(ename,'a')=2 Output:

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

27

Solution: Select ASCII(:n)from dual; Ans:

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 Clerk. Solution: Select ename,job, instr(ename,'a') from Emp where job='Clerk' Output:

16. Find the Employee names with maximum number of characters in it. Solution: Select max(length(ename))from Emp Output:

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

Solution: Select ename, substr(salary,1,3) from Emp

28

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(salary*20/100) from Emp Output:

19. List all the Employee names, who have more than 20 years of experience in the company. Solution: Select ename from Emp where round(months_between(sysdate,hiredate)/12)>20 Output:

29

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','Mgr')from Emp Output:

21. Display Date in the Following Format Tuesday 31 December 2002. Solution: Select to_char(sysdate,'day yy-month-yyyy') from dual Output:

30

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

Solution: Select sysdate, next_day(add_months(sysdate,3),1) from dual Output:

31

Coverage Joins

1. List Employee Name, Job, Salary, Grade & the Location where they are working Solution: Select ename,job,salary,grade,location from Emp join Dept on Emp.deptno=Dept.deptno join Salgrade on salary between losal and hisal Output:

2. Count the Employees For Each Salary Grade for Each Location Solution: Select count(empno),grade,location from Emp join Dept on Emp.deptno=Dept.deptno join SalGrade on salary between losal and hisal group by grade,location Output:

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

32

Solution: Select avg(salary) from Emp join SalGrade on salary between losal and hisal where grade=3 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,salary,dname,grade from Emp join Dept on Emp.deptno=Dept.deptno join SalGrade on salary between losal and hisal where dname in ('Accounting','Research') and grade=3 Output:

5. List employee Names, Manager Names & also Display the Employees who are not managed by anyone Solution: Select e.ename, m.ename from Emp e left outer join Emp m on e.empno=m.mgr where e.mgr is null Output:

6. List Employees who are drawing salary less than the salary of Scott Solution: Select e. * from Emp e join emp f on e.salary<f.salary

33

where f.ename='Scott' Output:

7. List Employees who have joined the company before their managers Solution: Select e.ename,e.hiredate,m.ename,m.hiredate from Emp e Join Emp m on e.mgr=m.empno where e.hiredate<m.hiredate Output:

8. List Employee Name, Job, Salary, Department name and Location Of all employees Working at Japan Solution: Select ename,job,salary,dname,location from Emp join Dept on Emp.deptno=Dept.deptno where location=’Japan’

Output:

34

9. List Employee Name, Job, Salary, Hire Date and Location Of all employees reporting in Accounting or Sales Department Solution: Select ename, job, salary, hiredate, location, dname from Emp join Dept on Emp.deptno=Dept.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 Japan or China

Solution: Select ename, job, salary, dname,location from Emp Join Dept on Emp.deptno=Dept.deptno where location in('Japan', 'China') and salary>2000 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 ename, job, salary, dname, location from Emp right outer join Dept on Emp.deptno=Dept.deptno

35

Output:

12. List all Employee details and also calculate the Average Salary and Total Salary Given to All Employees Solution: Select sum(salary+comm), avg(salary), empno, ename, job, salary, deptno from Emp Group by empno, ename,job,salary,deptno Output:

36

Nested and Correlated sub queries

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

Solution: Select * from Emp where deptno= (select deptno from Dept where dname= 'Marketing') Output:

2. List Departments in which at least one employee is working (Use Nested) Solution: Select dname from Dept where deptno in(select deptno from Emp) Output:

3. Find the Names of employees who do not work in the same department of Scott. Solution: Select ename from Emp where deptno<> (select deptno from Emp where ename='Scott' Output:

37

4. List departments (dept details) in which no employee is working (use nested) Solution: Select dname from Dept where deptno <> all (select deptno from Emp)

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 Emp where salary>all (select avg(salary) from Emp where deptno=20) and deptno<>20 Output:

6. List Employee names drawing Second Maximum Salary Solution: Select ename from Emp where salary= (select max(salary) from Emp where salary< (select max(salary) from Emp)) Output:

38

7. List the Employee Names, Job & Salary for those employees who are drawing minmum salaries for their department (Use Correlated) Solution: Select e.ename, e.job, e.salary from Emp e where salary = (select min(salary) from Emp i where i.deptno=e.deptno) Output:

8. List the highest paid employee for each department using correlated sub query. Solution: Select * from Emp e where salary = (select max(salary) from Emp i where i.deptno=e.deptno) Output:

9. List Employees working for the same job type as of Petter and drawing less than him (use Self Join)

Solution: Select e.* from Emp e join Emp f on

39

e.job=f.job where e.salary<f.salary and f.ename='Petter'

Output: