Beruflich Dokumente
Kultur Dokumente
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 salary
SQL> select * from emp where job = 'MANAGER' and sal <3000;
Q 03:
Display all employees who were hired in the year 1987
SQL> 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 employee
SQL> 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 first
SQL> select empno,ename,hiredate, sal, comm from emp
where hiredate = (select min(hiredate) from emp);
Q 08:
Display the name of the employee who is salesman
and managers and earn more than 2000
SQL> 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);
Q 24:
Find small (a) in all names its location and first occurrences
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;
Display
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;
Sharad Khare (084)
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;
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;
10
11
12
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.)
Sharad Khare (084)
13
14
begin
incr_sal := Review(e_id);
dbms_output.put_line('Value returned from function = ' || incr_sal);
end callReview;
15
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
ROW
begin
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, '12AUG-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 null
);
create table inventory
Sharad Khare (084)
16
(
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;
17
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 ROW
BEGIN
INSERT INTO EMP5 VALUES(:new.EMPNO, :new.ENAME,
:new.JOB, :new.MGR, :new.HIREDATE, :new.SAL,
:new.COMM, :new.DEPTNO);
END;
Trigger Created
Sharad Khare (084)
18
SQL> INSERT INTO EMP VALUES(9910, 'RAVI', 'Salesman', 1234, '12NOV-2014', 4000, 500, 10);
My custom Message: A NEW row is inserted in the EMP table
1 row created.
SQL> select * from emp5;
19
20