dbms lab - practical file
Post on 22-Dec-2015
130 Views
Preview:
DESCRIPTION
TRANSCRIPT
Lab 01:- 02-Aug-2014:Q 01: Display all employees who are “Managers” & “Clerks”SQL> select * from emp where job in ('MANAGER','CLERK');
Q 02: Display all employees who are “Managers” and earning less than 3000 as salarySQL> select * from emp where job = 'MANAGER' and sal <3000;
Q 03: Display all employees who were hired in the year 1987SQL> select * from emp where hiredate like '%87';
Q 04: Display all employees whose name start with “w” and ends with “d”SQL> select * from emp where ename like 'W%D';
Q 05: Display all employees whose name contains letter “s”SQL> select * from emp where ename like '%S%';
Q 06: Display total salary for each employeeSQL> select empno,ename,hiredate, sal, comm, sal + nvl(comm,0) as total_salary from emp;
Q 07: Display the name of the employee who was hired firstSQL> select empno,ename,hiredate, sal, comm from empwhere hiredate = (select min(hiredate) from emp);
Q 08: Display the name of the employee who is salesman and managers and earn more than 2000SQL> select * from emp where job in('SALESMAN','MANAGER') and sal > 2000;
Q 09: Display all employees who are managers and all salesman who are earning more than 2000
SQL> select * from emp where job = 'MANAGER' or
(job = 'SALESMAN' and sal>2000);
Sharad Khare (084) 1
Lab 02:- 16-Aug-2014:
Q 10: Display average salary of employees having salary greater than $1500 from “Emp” table.
SQL> select job, avg(sal) from emp group by job having avg(sal) > 1500;
Q 11: Display the department no and total no. of employees from “Emp” table and group them by department number.
SQL> select deptno, count(*) as total_emp from emp group by deptno;
Q 12: Display the total salary from “Emp” table where department no is not from either 10 or 20.
SQL> select deptno, sum(sal) from emp where deptno not in (10,20) group by deptno;
Q 13: Display dept no. and total salary from “Emp” table where job is not equals to salesman and employee having total salary greater than 6000.
SQL> select deptno, sum(sal) from emp where job != 'SALESMAN' group by deptno having sum(sal) > 6000;
Q 14: Display dept no and total number of job in “Emp” table and group them by “Deptno”.
SQL> select deptno, count(*), job from emp group by deptno,job order by deptno asc;
Q 15: Display second maximum salary from “Emp” table.
Sharad Khare (084) 2
SQL> select ename,sal from emp where sal in(select max(sal) from emp where sal not in (select max(sal) from emp));
Sharad Khare (084) 3
Lab 03:- 23-Aug-2014:
Q 16: Create a duplicate table with data of “Emp” table
SQL> Desc EMP2;
SQL> create table EMP2 AS select * from EMP;
SQL> select empno, ename, hiredate, sal from EMP2;
Q 17: Create a table structure duplicate from “Emp” table without any data
SQL> Desc EMP3;
SQL> create table EMP3 AS select * from emp where 1 = 2;
SQL> select count(*) from EMP3;
Q 18: Create a new table same as employee “Emp”
SQL> desc emp4;
SQL> CREATE TABLE EMP4 ( EMPNO NUMBER(4) PRIMARY KEY, ENAME VARCHAR2(10), JOB VARCHAR2(9), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7,2), COMM NUMBER(7,2), DEPTNO NUMBER(2) );
SQL> desc emp4;
Sharad Khare (084) 4
Sharad Khare (084) 5
Lab 04:- 30-Aug-2014:
Q 19: Increase the salary of all employees who are working in Dept no. 20 by “15%”
SQL> select empno, ename, hiredate, deptno, sal from emp2where deptno = 20;
SQL> update emp2 set sal = sal + sal*.15 where deptno = 20;
Q 20: Change the job of all the employees who are “Salesman” to the “Marketing Executive”
SQL> update emp2 set job = 'MARKETING' where job = 'SALESMAN';
Q 21: Increase the salary of employee “WALT” by 10% and transfer him from “Dept no” 10 to 30
SQL> update emp2 set sal = sal + sal*.10, deptno = 30where ename = 'WALT';
Q 22: Add the “Address” column in “Emp” table
SQL> alter table EMP2 add (ADDRESS VARCHAR2(50));
Q 23: Add the Unique constraint in the “Name” columnSQL> alter table emp4 add constraint name UNIQUE (ENAME);
Q 24: Drop the column “Address” in the “Emp” table
SQL> alter table emp2 drop column ADDRESS;
Sharad Khare (084) 6
Sharad Khare (084) 7
Lab 05:- 06-Sep-2014:
Q 25: Display all the name from “Emp” table where character are more then 4
SQL> select empno, ename from emp where length(ename) > 4;
Q 26: Display the phone number of “KOTA” city without showing the STD code for all the Employees
SQL> select ename, city, substr(phone, instr(phone,'-',1,1)+1, length(phone)) as Phone from emp4 where phone is not NULL;
Q 27: Find small (a) in all names its location and first occurrences
SQL> select empno, ename, INSTR(ENAME,'a',1,1) as Occurances from emp4;
Q 28: Display all sounding names like; “Sanjiv” or “Sanjeev”
SQL> select empno, ename from emp4 where SOUNDEX(ename) = SOUNDEX('Sanjiv');
Sharad Khare (084) 8
Lab 06:- 13-Sep-2014:
UNION & INTERSECT
Q 29: Display all the customers who are having account in the bank and loan in the bank and both account & Loan in the bank
SQL> select custid from depositorunionselect custid from borrower;
Q 30: Display the customer who are having loan as well as account in the bank
SQL> select custid from depositor intersect select custid from borrower;
Q 31: Display the customer who are having account only in the bank and no loan
SQL> select custid from depositor minus select custid from borrower;
Q 32: Display all the customer who are having only loan in the bank and no account
SQL> select custid from borrower minus select custid from depositor;
Q 33: Display the name, hire-date and 90 days review date for the employee; who work in department no. 10
SQL> select ename, hiredate, (hiredate + 90) as Review from emp where deptno = 10;
Q 34: Display the name hire-date and six month review date for all the employees who work in department no. 20
Sharad Khare (084) 9
SQL> select ename, hiredate, add_months(hiredate,6) as Review from emp where deptno = 30;
Q 35: Display employee name and number of weeks employed who work in department no. 30
SQL> select ename, hiredate, sysdate, (sysdate - hiredate) / 7 "Weeks Employed" from emp where deptno = 30;
Q 36: Display hire-date for all employees who work in department no 20 in “DD Of Month YYYY” format e.g. “13 Of September 2014”
SQL> select ename, to_char(hiredate,'DD "OF" MONTH YYYY') as "Hire Date" from emp where deptno = 20;
Q 37: Produce the hire-date in a format as shown below“Employee hired on May 20th, 1992 at 16:27”
SQL> select ename, to_char(hiredate, '"Employee hired on" MON DDTH","YYYY "at" HH24:MM') as "Hire_Date" from emp;
Sharad Khare (084) 10
Lab 07:- 27-Sep-2014:
JOIN Query
Q 38: Display the “Dept” name for all employee who are working in Dept no. 10
SQL> select d.dname, e.ename from dept d, emp e where e.deptno = 10 and d.deptno = e.deptno;
Q 39: Display the “Ename”, “Job”, Dept no. for Employee whose name are “John” and “Smith”
SQL> select e.ename, e.job, e.deptno, d.dname from dept d, emp e where e.ename in ('JOHN', 'SMITH') and d.deptno = e.deptno;
Q 40: Display “Emp no.”, “Job”, “location”of an employee who work in Dept no. 20 & 30
SQL> select e.empno, e.job, e.deptno, d.locfrom emp e, dept dwhere e.deptno in (20, 30)and e.deptno = d.deptno;
Sharad Khare (084) 11
Lab 08:- 04-Oct-2014:
Query 41: Display
Sharad Khare (084) 12
Lab 09:- 11-Oct-2014:
PL/SQL
Q 42: In the Employee table, If Emp. No. 7639 salary is less than or equal to 3000 then increase its salary by 10% otherwise increase its salary by 20%
SQL> DECLARE usal emp.sal%type; empnum emp.empno%type;BEGIN SELECT sal,empno into usal, empnum from emp WHERE empno = &empnum; DBMS_OUTPUT.PUT_LINE('variable usal contains value = ' || usal); DBMS_OUTPUT.PUT_LINE('variable empnum contains value = ' || empnum); IF usal <= 3000 then DBMS_OUTPUT.PUT_LINE('Increasing salary by 10%'); update emp set sal = sal + sal*.10 where empno = empnum; ELSE DBMS_OUTPUT.PUT_LINE('Increasing salary by 20%'); update emp set sal = sal + sal*.20 where empno = empnum; END IF;EXCEPTION WHEN no_data_found THEN DBMS_OUTPUT.PUT_LINE('OOps! No such employee exist...'); END;
Sharad Khare (084) 13
Q 43: In the Employee table, Increase the salary of the employee 7369 on the basis of following conditions:(1) If sal. <= 2000, then give 10% increase(2) If sal. in between 2001 and 3000 then give 20% salary hike(3) Otherwise give him 30% hike
SQL> DECLARE usal emp.sal%type; empnum emp.empno%type;BEGIN SELECT sal,empno into usal, empnum from emp WHERE empno = &empnum; DBMS_OUTPUT.PUT_LINE('variable usal contains value (old salary) = ' || usal); DBMS_OUTPUT.PUT_LINE('variable empnum contains value = ' || empnum); IF usal <= 2000 then DBMS_OUTPUT.PUT_LINE('Increasing salary by 10%'); update emp set sal = sal + sal*.10 where empno = empnum; ELSIF usal >2000 AND usal <= 3000 then DBMS_OUTPUT.PUT_LINE('Increasing salary by 20%'); update emp set sal = sal + sal*.20 where empno = empnum; ELSE DBMS_OUTPUT.PUT_LINE('Increasing salary by 30%'); update emp set sal = sal + sal*.30 where empno = empnum; END IF;EXCEPTION WHEN no_data_found THEN DBMS_OUTPUT.PUT_LINE('OOps! No such employee exist...'); END;
Q 44: Table Emp9 (Employee Nine) is having the same structure as Emp. table. Insert the record of Emp. no. 7369 into Emp9
SQL> CREATE TABLE EMP9 AS SELECT * FROM EMP WHERE 1 = 2;
SQL> DECLARE FullRow emp%rowtype; empnum emp.empno%type;Sharad Khare (084) 14
BEGIN SELECT * into FullRow from emp WHERE empno = &empnum; DBMS_OUTPUT.PUT_LINE('variable FullRow contains employee num = ' || FullRow.empno); INSERT INTO EMP9 VALUES (FullRow.empno, FullRow.ename, FullRow.job, FullRow.mgr, FullRow.hiredate, FullRow.sal, FullRow.comm, FullRow.deptno); EXCEPTION WHEN no_data_found THEN DBMS_OUTPUT.PUT_LINE('OOps! No such employee exist...'); END;
Sharad Khare (084) 15
Lab 10:- 18-Oct-2014:
Query 45:
Sharad Khare (084) 16
Lab 11:- 25-Oct-2014:
Query 46:
Sharad Khare (084) 17
Lab 12:- 01-Nov-2014:
Q 47: Write a Stored Procedure that increases a salary of an Employee by the amount which is accepted as a parameter, also check the NULL salary and missing Employee number
SQL> create or replace procedure incr(e_id number, amt number)AS vsal emp.sal%type; missing_salary exception;begin select sal into vsal from emp where empno = e_id; dbms_output.put_line('Old salary is = ' || vsal);
if vsal is NULL then Raise missing_salary; else update emp set sal = (vsal + amt) where empno = e_id; dbms_output.put_line('Salary updated by = ' || (vsal + amt)); end if;exception when missing_salary then dbms_output.put_line('salary is null for' || e_id); when no_data_found then dbms_output.put_line('no record found for employee' || e_id); end incr;
SQL> exec SQL> exec incr(7369, 32);Old salary is = 968Salary updated by = 1000
PL/SQL procedure successfully completed.
Sharad Khare (084) 18
Q 48: The company wants to calculate the annual increments of the Employee. Create a function to calculate the increment based on the following conditions:(1) If Salary is less <= 3000, then increase is 20% of net salary (net salary = Sal. + Comm.)(2) If between 3000 to 6000 then increment will be 30%(3) Otherwise increment will be 40%
SQL> create or replace function Review(e_id number)Return numberAS incr emp.sal%type; net emp.sal%type; vsal emp.sal%type; vcomm emp.comm%type; missing_salary exception;begin select sal, nvl(comm,0) into vsal,vcomm from emp where empno = e_id; net:= vsal + vcomm; dbms_output.put_line('Current salary is = ' || vsal); dbms_output.put_line('Current comm is = ' || vcomm); dbms_output.put_line('net salary is = ' || net); if vsal <3000 then incr := 0.20 * net; dbms_output.put_line('Increment on salary is = ' || incr); elsif vsal > 3000 and vsal < 6000 then incr := 0.30 * net; dbms_output.put_line('Increment on salary is = ' || incr); else incr := 0.40 * net; dbms_output.put_line('Increment on salary is = ' || incr); end if; return incr;exception when missing_salary then dbms_output.put_line('salary is null for' || e_id); when no_data_found then dbms_output.put_line('no record found for employee' || e_id); end review;
Sharad Khare (084) 19
Procedure that is calling above function
SQL> create or replace procedure callReview(e_id number)AS incr_sal emp.sal%type;begin incr_sal := Review(e_id); dbms_output.put_line('Value returned from function = ' || incr_sal);end callReview;
Sharad Khare (084) 20
Lab 13:- 08-Nov-2014:
Q 49: Display the next characters of each Name in the Emp. table.Example; ABCD -> BCD
SQL> select ename, substr(ename, 2, length(ename)) as "Name" from emp;
TRIGGERS
Q 50: Whenever a new record is inserted a message should be given as“A new record is Inserted”
SQL> CREATE or replace trigger trig_emp_msg_on_insert AFTER INSERT ON EMP FOR EACH ROWbegin dbms_output.PUT_LINE('My custom Message: A NEW row is inserted in the EMP table');end;
SQL> INSERT INTO EMP VALUES(8888, 'VARUN', 'CEO', 1234, '12-AUG-2013', 4000, 500, 10);
Q 51: In the Sales table, whenever a new order has been placed, quantity of the ordered Product will automatically be reduced in the Inventory table
SQL> create table Sales( custId number(8) primary key, Qty number(6) not null, prodID varchar2(10) not nullSharad Khare (084) 21
) ;
create table inventory( ProdID varchar2(10) not null, Qty number(6) not null);
SQL>
insert into inventory values ('p205', 10);insert into inventory values ('p310', 5);insert into inventory values ('p303', 10);
SQL> create or replace trigger inventory_reduce after insert on Sales for each row begin update inventory set Qty = Qty- :new.Qty where ProdID = :new.ProdID; end;
SQL> select * from inventory where ProdID = 'p205';
PRODID QTY---------- ----------p205 10
SQL> select * from sales where ProdId = 'p205';
no rows selected
SQL> insert into sales values (1001, 2, 'p205');
1 row created.
SQL> SQL> select * from inventory where ProdID = 'p205';
PRODID QTY---------- ----------p205 8
Sharad Khare (084) 22
Sharad Khare (084) 23
Lab 14:- 15-Nov-2014:
Q 52: Create a Trigger to display a message with new balance after withdrawn of amount from Account
SQL> create table ACCOUNT( CustID number(30) primary key, Account number(30) not null, Amount number(30) not null, Threshold number(30) not null);
SQL> insert into account values (1001,2014101,10000, 500);insert into account values (1002,2014102,10000, 500);insert into account values (1003,2014103,10000, 500);insert into account values (1004,2014104,10000, 500);
SQL> SQL> update Account set amount = 6000 where CustID = 1001;Your updated balance is:6000
1 row updated.
Q 53: If there is change in EMP. table, same changes should be done in the duplicate table in EMP5 tbale.
SQL> SQL> create table emp5 2 as select * from emp where 1 = 2;
Table created.
SQL>CREATE or replace trigger TRIG_INSERT_EMP5 AFTER INSERT ON EMP FOR EACH ROWBEGIN INSERT INTO EMP5 VALUES(:new.EMPNO, :new.ENAME, :new.JOB, :new.MGR, :new.HIREDATE, :new.SAL, :new.COMM, :new.DEPTNO);Sharad Khare (084) 24
END;
Trigger Created
SQL> INSERT INTO EMP VALUES(9910, 'RAVI', 'Salesman', 1234, '12-NOV-2014', 4000, 500, 10);My custom Message: A NEW row is inserted in the EMP table
1 row created.
SQL> select * from emp5;
Sharad Khare (084) 25
Lab 15:- 29-Nov-2014:
Query 54:
Lab 16:- 06-Dec-2014:
Query 55:
Sharad Khare (084) 26
top related