Anda di halaman 1dari 10

1 .

Create a table Employee and Depat

Employee Table Structure Empno Ename Desig Caderno Doj Sal Deptno Number(4) Varchar2(20) Varchar2(8) Number(4) Date Number(4) Number(4)

Depat Table Structure Deptno Number(2) Dname Varchar2(20) Location Varchar2(15)

Solution :Create table Employee (Empno number(4),Ename varchar2(20), Desig varchar2(8),Caderno number(4), Doj date, Sal number(4), Deptno number(4)); Create table Depat (Deptno number(2),Dname varchar2(20),Location varchar2(15)); 2. Alter table Depat , make deptno as primary key Solution :Alter table Depat add constraint pky primary key(deptno) ; 3. Alter table Employee , make Empno as primary key and Deptno as foreign key Solution :Alter table Employee add constraint prk primary key(Empno); Alter table Employee add constraint fky foreign key(deptno) references depat (deptno) ;

4. Give the commant to view the details of the tables Employee and Depat Solution :Desc Employee; Desc Depat; 5 . Increase the column width by 1 for deptno in depat table Solution :Alter table depat modify deptno number(3); 6 . Insert 5 records in Depat table

Values to be inserted in Depat table 301 , Web ,Chennai 302,Accts,Bombay 303,HR,Banglore 304,Marketing ,Chennai 305,Sales,Chennai Solution :Insert into Depat values(301 , 'Web' ,'Chennai' ); Insert into Depat values(302,'Accts','Bombay' ); Insert into Depat values(303,'HR','Banglore'); Insert into Depat values(304,'Marketing' ,'Chennai'); Insert into Depat values(305,'Sales','Chennai'); 7. Increase the colomn width of deptno of emp table by 1 Solution :Alter table employee modify deptno number(3); 8. Increase the colomn width of salary as with precision 7 and scale 3 of Employee table. Solution :Alter table employee modify salary number(10,3); 9. Insert 5 records in Employee table Values to be inserted in Employee table 001 , vijay,Manager , 101,09-may-2002,10000,200,301 002,Valli,GM,102,10-may-2002,20000,300,302 003,shalini,AGM,103,11-may-2002,30000,400,303 004,Bhaskar ,PM,104,12-may-2002,40000,500,304 005,Jai,Manager,105,13-may-2002,50000,600,301 Solution :Insert into Employee values(001 ,' vijay','Manager ', 101,09-may-2002',10000,200,301 ); Insert into Employee values(002,'Valli','GM',102,'10-may-2002',20000,300,302 ); Insert into Employee values(003,'shalini','AGM',103,'11-may-2002',30000,400,303);

Insert into Employee values(004,'Bhaskar' ,'PM',104,'12-may-2002',40000,500,304 ); Insert into Employee values(005,'Jai','Manager',105,'13-may-2002',50000,600,301 ); 10. Select all records from the Employee table and Dept table Solution :Select * from Employee; Select * from Depat; 11. Select only the Employee name, Employee number ,department number from the employee table and save the sql statement in a file . Solution :Select empno,ename,deptno from Employee; 12. Select only the employee number 002 from the Employee table . Solution :Select * from Employee where empno=002; 13. Type the following sql prompt and without retyping the statement change maistake Select empno,name from emp; Solution :Select empno,name from emp; c/name/ename 14. Select all employees who are under the manager designation Solution :Select * from Employee where desig='manager'; 15. Select all employee who either their designation is equal to Manager or have salary greather than 10000 Solution :Select * from Employee where desig='manager' or salary>10000; 16. Select employee whose earn a salary between 10000 and 50000 Solution :Select * from Employee where salary between 10000 and 50000; 17. List all the employees whose name begin with v; Solution :Select * from Employee where ename like 'v%'; 18. Display all the employee with their salary lusted in desending order . Solution :Select * from Employee order by salary desc; 19. Display all the employees who have joined on 10 may 2002. Solution :Select * from Employee where doj='10-may-2002';

20. Create a table called sample with fields itemno number(3),itemname varchar2(20) add one sample record to it . Now give a command to delete the record . And now check whether the record has been deleted then drop the table . Solution :Create table sample(itemno number(3),itemname varchar2(20)); Insert into sample values(001,'Pen'); Delete sample; Select * from sample; Drop table sample; 21. Give the command to retrieve the table sample after dropping it. Solution :Rollback; 22. Now give the command to drop the sample table permanently from the database Solution :Drop table sample; commit;

23. Find the average salary of managers . Solution :Select avg(salary) from Employee where desig='Manager'; 24. Find the total salary and total commission of managers. Solution :Select sum(salary), sum(comm) from Employee where desig='Manager'; 25. Find the highest and lowest salaries and the difference between them Solution :Select MAX(salary),MIN(salary),MAX(salary)-MIN(salary) from Employee; 26. Find the number of employees in department 301 Solution :Select count(*) from Employee where deptno=301; 27. Give a query to calculate salary of the employee and list the total salary colomn name has Net salary.(total salary = salary+commission). Solution :Select empno,ename,deptno,salary,comm,salary+commm Net Salary from Employee; 28. Write a query to display the date of joining of all the employees in the following formats a. month dd,yyyy b.month dd,yy Solution :a. Select to_char(doj,'month,dd,yyyy') from Employee; b.Select to_char(doj,'month,dd,yy') from Employee;

29. Give a query to find the current date and current time from system. Solution :Select sysdate from dual; Select to_char(sysdate,'hh:mm:ss') from dual; 30. Select the employees who are working as that of Vijay's resignation. Solution :Select * from Employee desig =(select desig from Employee where ename='Vijay'); 31. Give a subquery to list the person who is getting lowest salary in the department 301 Solution :Select * from Employee salary =(select min(salary) from Employee where deptno=301); 32. Give a query to increase the salary of the manager to 10% Solution :Update Employee set salary =1.1*salary where desig='Manager'; 33. Give a query to raise 15% of salary of the AGM Solution :Update Employee set salary =1.5*salary where desig='AGM'; 34. Write a query to list all the department number in both Employee and Depat tables without repeating the department number . Solution :Select deptno from employee union select deptno from depat;

35. List the employee details with department name including the department which does not have any employees in employee table Solution :Select * from depat where deptno not in (select deptno from employee); 36. Give a command to find the location in which vijay is working Solution :Select ename , location from Employee , depat where ename='vijay' and Employee.deptno=Depat.deptno; 37. Give an update statement with save point and rollback to update the salary of vijay and check whether the total salary of the company does not exceed 160000 , if it exceeds then reduce the salary of valli to fit in. Solution :Update Employee set salary =10000 where ename ='vijay'; Savepoint salary1; Update Employee set salary =10000 where ename ='Valli'; Savepoint salary2; Select sum(salary) from Employee; Rollback to savepoint salary2; Update Employee set salary =5000 where ename='Valli';

Commit; 38. Create a sequence called newseq which starts with 10 with a maximum value of 10000 Solution :Create sequence newseq; Increment by 10 Start with 10 Maxvalue 10000 Cycle; 39. Create a cluster called emp_dept_clust which stores the Emp and Dept tables and clustered by the deptno colomn. Solution :Create cluster emp_dept (deptno number(3)); 40. Give a command to create table(sample_emp) with the same structure as the emp table , with out any values in it . Solution :Create table sample_emp as select * from Employee where 1=2; 41. Give a query to list employees in the department Web and deptno=301. Solution :Select * from employee where deptno =301 and deptno =(Select deptno from depat where dname= 'Web'); 42. Create a view called emp_view consisting of the employee , number , employee name , and designation for the employee department 301. Solution :Create view emp_view as select empno , ename desig from Employee where deptno =301;

PL/SQL
1. Write a block that accepts name and display it 5 times in output screen . Solution :DECLARE NAME VARCHAR2(15) :='&NAME'; N NUMBER(2) :=1; BEGIN WHILE N<=5 LOOP DBMS_OUTPUT.PUT_LINE(NAME); N:=N+1; END LOOP; END; 2. Write a PL/SQL block , which accepts principal duration (in months),and rate of interest and calculate maturity amount. Solution :DECLARE PRINCIPAL NUMBER :=&PRINCIPAL; MONTHS NUMBER :=&MONTHS; ROI NUMBER :=&ROI; MAMOUNT NUMBER; BEGIN MAMOUNT := PRINCIPAL + ( PRINCIPAL*ROI/100)*MONTHS; DBMS_OUTPUT.PUT_LINE(MAMOUNT); END;

3. Accept radius and calculate area of a circle . Use variable pi as constant . Solution :DECLARE RADIUS NUMBER :=&RADIUS; PI NUMBER :=3.14; AREA NUMBER; BEGIN AREA := Pi * ( RADIUS*RADIUS); DBMS_OUTPUT.PUT_LINE(AREA); END; 4. Write a PL/SQL block to display all the employee names along with salary of all the emplyees (Hint: do it without using cursors) Solution :BEGIN FOR EMPDET IN (SELECT ENAME,SALARY FROM EMPLOYEE) LOOP DBMS_OUTPUT.PUT_LINE(EMPDET.ENAME|| ' ' ||RMPDET.SALARY); END LOOP; END;

5.How do you define the variable used in FOR loop index in PL/SQL? Solution :BEGIN FOR I IN 1..100 LOOP DBMS_OUTPUT.PUT_LINE(I); END LOOP; END;

6. Write a PL/SQL block to accept a number and display whether it is less than, or equal , or greater than 10. Solution :DECLARE N NUMBER :=5; BEGIN IF N =10 THEN DBMS_OUTPUT.PUT_LINE(N||'EQUAL TO 10'); ELSEIF N <10 THEN DBMS_OUTPUT.PUT_LINE(N||'LESS THAN 10'); ELSEIF N >10 THEN DBMS_OUTPUT.PUT_LINE(N||'GREATER THAN 10'); END IF; END; 7.Write a PL/SQL block to generate EVEN,ODD number less than 100 using any One loop. Solution :BEGIN FOR I IN 1..100 LOOP IF MOD(I,2)!=0 THEN DBMS_OUTPUT.PUT_LINE(I); END IF; END LOOP; END; 8.Write a function that accepts a character string and returns the same string with all blanks deleted , along with an integer that indicates the number of blanks existed in the input string . Solution :CREATE OR REPLACE FUNCTION REMOVE_BLANK(NAME VARCHAR2) RETURN VARCHAR2 IS NEW_NAME VARCHAR2(100); SPACE NUMBER :=0; LEN NUMBER; N MUNBER :=1; LEN :=LENGTH(NAME); WHILE N<=LEN LOOP IF SUBSTR(NAME,N,1)=''THEN

SPACE :=SPACE +1; END IF N:= N+1; END LOOP; SELECT REPLACE(NAME,'') INTO NEW_NAME FROM DUAL; IF SPACE>0 THEN RETURN (NEW_NAME||''||SPACE||'SPACE REMORVED'); ELSE RETURN (NEW_NAME||'HAVING NO SPACE '); END IF; END; To execute above function in PL/SQL block DECLARE R VARCHAR2(100); NAME VARCHAR2(100):='D I SCO V E RY'; BEGIN; R:= REMOVE_BLANK(NAME); DBMS_OUTPUT.PUT_LINE(R); END; 9. Write a funtion that reverses a given string . Solution :CREATE REPLACE FUNCTION REVERSE_STRING (S VARCHAR2) RETURN VARCHAR2 IS R VARCHAR2(20); LEN NUMBER; BEGIN LEN := LENGTH(LTRIM(RTRIM(S))); FOR I IN REVERSE 1..LEN LOOP R:=R||SUBSTR(S,I,I); END LOOP; RETURN R; END; To execute above function in PL/SQL block DECLARE RNAME VARCHAR2(10); BEGIN; RNAME := REMOVE_STRING('NATIONAL'); DBMS_OUTPUT.PUT_LINE(RNAME); END; 10.Write a PL/SQL block with the example of local sub-program.

Solution :To execute above function in PL/SQL block DECLARE NAME VARCHAR2(20); PROCEDURE CUBE(X NUMBER) IS C NUMBER; BEGIN; C:=X*X*X; DBMS_OUTPUT.PUT_LINE('CUBE OF '||X||' IS'||C); END; To execute above function in PL/SQL block DECLARE NAME :='INTERNET WORLD'; CUBE(5); DBMS_OUTPUT.PUT_LINE(NAME); END;