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
NIKHIL KHANDELWAL
CLASS: BCA 3RD YEAR
1
PROJECT ABSTRACT
I am NIKHIL KHANDELWAL 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 Clause......................................................................................................................................... 7
Group By And Having ....................................................................................................................... 18
Functions ............................................................................................................................................. 23
Joins ...................................................................................................................................................... 31
Nested and Corelated Sub Queries ............................................................................................... 36
3
1. Create an Employee Table (Employee) 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 Employee (empno number(4), ename varchar2(20), deptno number(2), job varchar2(20), sal number(5),comm number(4), mgr number(4), hiredate date); Output:
Insert At least 5 records. Solution: Insert into Employee (empno,ename,deptno,job,sal,comm,mgr,hiredate) Values (:empno,:ename,:deptno,:job,:sal,:comm,:mgr,:hiredate);
4
2. Create a Department Table (Department) with Following Fields:
Solution: Create table department (deptno number (2), dname varchar2(20), loc varchar2(20)); Output:
FIELDS DATA TYPE SIZE
DEPTNO NUMBER 2
DNAME VARCHAR2 20
LOC (location) VARCHAR2 20
5
Insert Atleast 5 records. Solution: Insert into department(deptno,dname,loc) values (:deptno,:dname,:loc); Output:
3. Create a SalGrade Table with Following Fields:
FIELDS DATA TYPE SIZE
GRADE NUMBER 1
LOSAL NUMBER 5
HISAL NUMBER 5
Solution: Create table salgrade (grade number(1), losal number(5), hisal number(5)); Output:
6
Insert At least 5 records. Solution: Insert into salgrade(grade,losal,hisal)values(:grade,:losal,:hisal); Output:
7
SELECT STATEMENT
1. List all the information about all Employee.
Solution: Select* from Employee Output:
2. Display the Name of all Employee along with their Salary.
Solution: Select ename,job from Employee Output:
3. List all the Employeeloyee Names who is working with Department Number is
20.
Solution: Select ename from Employee Where deptno=20
Output:
8
4. List the Name of all ‘ANALYST’ and ‘SALESMAN’.
Solution:
Select ename from Employee where job in('Analyst','salesman')
Output:
5. Display the details of those Employee who have joined before the end of Sept.
1981. Solution: Select ename from Employee Where hiredate<'30-sep-1981' Output:
6. List the Employee Name and Employee Number, who is ‘MANAGER’.
Solution: Select empno,ename from Employee Where job='manager' Output:
9
7. List the Name and Job of all Employee who are not ‘CLERK’. Solution: Select ename,job from Employee Where job!='clerk' Output:
8. List the Name of Employee, whose Employee Number is 7369,7521,7839,7934 or 7788.
Solution: Select ename from Employee 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
10. List the Employee Name and Salary, whose Salary is vary from 1000 to 2000. 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-1981.
Solution: Select ename from Employee Where hiredate<'30-Jun-1981' or hiredate>'31-Dec-1981' Output:
12. List the Commission and Name of Employee, who are availing the Commission. Solution: Select ename,comm from Employee
11
Where comm is not null Output:
13. List the Name and Designation (job) of the Employee who does not report to anybody. Solution: Select ename,job from Employee Where mgr is null Output:
14. List the detail of the Employee, whose Salary is greater than 2000 and Commission is NULL.
Solution: Select* from Employee Where sal>2000 and comm is null Output:
15. List the Employee details whose Name start with ‘S’. Solution: Select* from Employee Where ename like 's%' Output:
12
16. List the Employee Names and Date of Joining in Descending Order of Date of Joining. The 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
Solution: Select ename,sal,job,deptno from Employee Order by deptno desc,ename asc,sal desc Output:
18. Order of Department Number, Ascending Order of Name and Descending Order
of Salary. Solution: Select ename,sal,job,deptno from Employee
13
order by deptno desc,ename asc,sal desc Output:
19. 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, sal, sal+sal*50/100+sal*30/100-sal*10/100 as "Netsalary" from Employee order by "Netsalary" asc Output:
20. List Salesman from department No 20. Solution: Select* from Employee Where deptno=20 and job='salesman' Output:
14
21. List Clerks from 30 and salesman from 20. In the list the lowest earning
Employee must at top. Solution: select* from Employee where deptno=20 and job='salesman' or deptno=30 and job='clerk' order by sal asc Output:
22. List different departments from Employee table. Solution: Select distinct deptno from Employee Output:
23. List Employee having “N” at the end of their Name Solution: select* from Employee where ename like'%n' Output:
24. List Employee who are not managed by anyone. Solution: select* from Employee
15
where mgr is null Output:
25. List Employee who are having “TT” or “LL” in their names. Solution: Select* from Employee Where ename like '%tt%' or ename like '%ll%' Output:
26. List Employee earning salaries below 1500 and more than 3000. Solution: Select* from Employee Where sal<1500 or sal>3000 Output:
27. List Employee who are drawing some commission. Display their total salary as well.
Solution: Select ename,sal,comm,sal+comm totalsal from Employee Where comm is not null Output:
16
28. List Employee 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 Employee Where comm>sal order by comm desc Output:
29. List the Employee who joined the company in the month of “FEB”. Solution: Select* from Employee where hiredate between '1-feb-80' and '28-feb-80' Output:
30. List Employee who are working as salesman and having names of five characters.
Solution: Select* from Employee Where job='salesman' and ename like '_____' Output:
17
31. List Employee who are managed by 7788. Solution: Select* from Employee Where mgr=7788 Output:
18
GROUPING, HAVING ETC.
1. List the Department number and total number of Employee in each department.
Solution: Select deptno,count(empno)from Employee Group by deptno Output:
2. List the different Job names (Designation) available in the EMPLOYEE table.
Solution: Select distinct job from Employee
Output:
3. List the Average Salary and number of Employee working in Department number 20.
Solution: Select avg(sal),deptno,count(empno) From Employee Where deptno=20 Group by deptno
Output:
4. List the Department Number and Total Salary payable at each Department.
19
Solution: Select sum(sal),deptno From Employee group by deptno Output:
5. List the jobs and number of Employee in each Job. The result should be in Descending Order of the number of Employee.
Solution: Select job,count(empno) From Employee Group by job Order by count (empno) desc Output:
6. List the Total salary, Maximum Salary, Minimum Salary and Average Salary of Employee job wise, for Department number 20 only. Solution: Select job,sum(sal),max(sal),min(sal),avg(sal) From Employee Where deptno=20 Group by job Output:
20
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 deptno,job,avg(sal) from Employee Group by job,deptno Output:
9. List the Average Salary of all departments, in which more than three people are working.
Solution: Select deptno,avg(sal),count(empno)
21
From Employee Group by deptno Having count(empno)>3 Output:
10. List the jobs of all Employee where Maximum Salary is greater than or equal to 4000.
Solution: Select job,max(sal) From Employee Group by job Having max(sal)>=4000 Output:
11. List the total salary and average salary of the Employee job wise, for department number 20 and display only those rows having average salary greater than 1000.
Solution: Select job,sum(sal),avg(sal) From Employee Where deptno=20 Group by job Having avg(sal)>1000 Output:
22
12. List the total salaries of only those departments in which at least 4 Employee are working. Solution: Select deptno,count(empno),sum(sal+comm) From Employee Group by deptno Having count(empno)>=4
Output:
13. List the Number of Employee Managed by Each Manager
Solution: Select count(empno),mgr From Employee Where mgr is not null Group by mgr Output:
14. List Average Commission Drawn by all Salesman
Solution: Select job,avg(comm) From Employee Where job='salesman' Group by job Output:
23
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(:num1,:num2) from dual Output:
3. Enter a number and check whether it is negative or positive.
Solution: Select sign(:num) from dual Output:
24
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(:num,-2) as "upto-2",trunc(:num,1) as "upto1" from dual Output:
6. Find the rounding value of 563.456, up to 2, 0 and 2 places of decimal.
Solution: Select round(563.456,2) as "upto2",round(563.456) as "upto0",round(563.456,2) as "upto2" from dual Output:
7. Accept two numbers and display its corresponding character with the
appropriate title.
25
Solution: Select chr(:num1) as "First Char",chr(:num2) as "Second Char" from dual Output:
8. Input two names and concatenate it separated by two spaces.
Solution: Select concat(concat(:name1,' '),:name2) from dual Output:
9. Display all the Employee names-with first character in upper case from
EMPLOYEE table. Solution: Select initcap(ename) from Employee Output:
26
10. Display all the department names in upper and lower cases.
Solution: Select upper(dname),lower(dname) from department Output:
11. Extract the character S and A from the left and R and N from the right of the
Employee name from EMPLOYEE table.
Solution: Select ltrim(ename,'sa') as "from-Left", rtrim(ename,'rn') as "from-Right" from Employee Output:
12. Change all the occurrences of CL with P in job domain.
Solution: Select job,replace(job,'cl','p') from Employee Output:
27
13. Display the information of those Employee 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(:character) from dual Output:
15. Display the Employee names along with the location of the character A in the
Employee name from EMPLOYEE table where the job is CLERK.
Solution: Select ename,instr(ename,'o') from Employee Where job='clerk'
28
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 Employee who are getting salary in four figures.
Solution: Select ename,length(sal) from Employee where length(sal)=4 Output:
18. Display only the first three characters of the Employee name and their H RA
(salary * .20), truncated to decimal places. Solution: Select substr(ename,1,3),trunc(sal*.20) as "HRA" from Employee Output:
29
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) as "experience(in years)" 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','MNGR',job) as "job(updated)" from Employee Output:
30
21. Display Date in the Following Format Tuesday 31 December 2002.
Solution: Select to_char(Sysdate,'DAY dd MONTH yyyy') from dual Output:
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,sal,grade,loc From Employee join salgrade On sal between losal and hisal Join department using(deptno) Output:
2. Count the Employee For Each Salary Grade for Each Location
Solution: Select LOC,grade,count(empno) From Employee join salgrade On sal between losal and hisal Join department using(deptno) Group by grade,LOC Output:
32
3. List the Average Salary for Those Employee who are drawing salaries of grade 2.
Solution: Select grade,avg(sal) From Employee join salgrade On sal between losal and hisal Where grade=2 Group by grade Output:
4. List Employee Name, Job, Salary Of those Employee who are working in Accounting or Research department but drawing salaries of grade 3.
Solution: Select ename,job,sal,dname from Employee join salgrade On sal between losal and hisal join department using(deptno) Where grade=2 and dname in('accounting','research') Output:
5. List Employee Names, Manager Names & also Display the Employee who are not managed by anyone Solution: Select e.ename as "Employee ",m.ename as "Manager" From Employee e left outer join Employee m On m.empno=e.mgr Output:
33
6. List Employee who are drawing salary more than the salary of SCOTT
Solution: Select e.* from Employee e join Employee f On e.sal>f.sal where f.ename='scott' Output:
7. List Employee who have joined the company before their managers
Solution: Select distinct e.* from Employee e join Employee f On e.hiredate>f.hiredate Output:
8. List Employee Name, Job, Salary, Department No, Department name and Location Of all Employee Working at NEW YORK Solution: Select ename,job,sal,deptno,dname,loc from Employee join department using(deptno) Where loc='newyork'
34
Output:
9. List Employee Name, Job, Salary, Hire Date and Location Of all Employee reporting in Accounting or Sales Department Solution: Select ename,job,sal,deptno,hiredate,dname,loc from Employee join department using(deptno) where dname in('accounting','sales')
Output:
10. List Employee Name, Job, Salary, Department Name, and Location for Employee drawing salary more than 2000 and working at New York or Dallas.
Solution: Select ename,job,sal,dname,loc from Employee join department using (deptno) where sal>2000 and loc in('newyork','dallas')
Output:
11. List Employee Name, Job, Salary, Department Name, Location Of all Employee , also list the Department Details in which no Employee is working
Solution:
35
Select ename,job,sal,dname,loc from Employee right outer join department using(deptno)
Output:
36
Nested and Correlated subqueries
1. List Employee who are working in the Sales Department (Use Nested)
Solution: Select * from Employee Where deptno=(Selectdeptno from department where dname='sales') Output:
2. List Departments in which at least one Employee is working (Use Nested)
Solution: Select dname from department Where deptno in(Selectdeptno from Employee ) Output:
3. Find the Names of Employee who do not work in the same department of Scott.
Solution: Select * from Employee Where deptno not in(Selectdeptno from Employee where ename='scott') Output:
37
4. List departments (department details) in which no Employee is working (use nested). Solution: Select dname from department Where deptno not in(Selectdeptno from Employee )
Output:
5. List Employee who are drawing the Salary more than the Average salary of DEPTNO 30. Also ensure that the result should not contain the records of DEPTNO 30
Solution: Select * from Employee Where deptno!=30 and sal>(Selectavg(sal) from Employee where deptno=30 group by deptno)
Output:
6. List Employee names drawing Second Maximum Salary
Solution: Select* from Employee Where sal=(Selectmax(sal) from Employee where sal<(Selectmax(sal) from Employee )
38
Output:
7. List the Employee Names, Job & Salary for those Employee who are drawing minmum salaries for their department (Use Correlated)
Solution: Select* from Employee e Where e.sal=(Selectmin(sal) from Employee i where e.deptno=i.deptno ) Output:
8. List the highest paid Employee for each department using correlated sub query.
Solution: Select* from Employee e Where e.sal=(Selectmax(sal) from Employee i where e.deptno=i.deptno ) Output:
9. List Employee working for the same job type as of PETER and drawing more than him (use Self Join)
Solution: Select e.* from Employee e join Employee f on e.sal>f.sal and e. Job=f.job where f.ename='peter' Output:
39