Beruflich Dokumente
Kultur Dokumente
Query 2> List the employee number, name, job title & hiredate of employees.
SQL> select ename, job, sal, sal*12 "ANNUAL SALARY" from emp;
-------------------------------------------------------------------------------------------------------------------------
JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN
-------------------------------------------------------------------------------------------------------------------------
Query 5> List name & salary of all employees who are clerk.
ENAME SAL
---------- ---------
SMITH 800
ADAMS 1100
JAMES 950
MILLER 1300
-------------------------------------------------------------------------------------------------------------------------
Query 6>List employee number, name, job, salary & hiredate of employees of dept number 20.
SQL> select empno, ename, job, sal, hiredate from emp where deptno = 20;
-------------------------------------------------------------------------------------------------------------------------
Query 7> List the name, job, salary of everyone hired on December 17, 1980.
SQL> select ename, job, sal from emp where hiredate = '17-DEC-80';
-------------------------------------------------------------------------------------------------------------------------
Query 8> List the department name & number greater then or equal to 20.
SQL> select dname, deptno from dept where deptno >= 20;
DNAME DEPTNO
----------------- ------------
RESEARCH 20
SALES 30
OPERATIONS 40
-------------------------------------------------------------------------------------------------------------------------
Query 9> List name, salary & commission of employees whose commission is greater than
their salaries.
SQL> select ename,sal, comm from emp where comm > sal;
-------------------------------------------------------------------------------------------------------------------------
Query 10> List name & employee numbers of managers who earn more than 2600. Display in
alphabetical order by name.
SQL> select ename, empno from emp where job = 'MANAGER' and sal >2600 order by
ename;
ENAME EMPNO
------------------------------
BLAKE 7698
JONES 7566
-------------------------------------------------------------------------------------------------------------------------
Query 11> List the information about managers & president from the column job in EMP
table order by deptno.
SQL> select * from emp where job in ('MANAGER', 'PRESIDENT') order by deptno;
-------------------------------------------------------------------------------------------------------------------------
Query 12> List all employees whose name that do not end with letter ‘S’.
-------------------------------------------------------------------------------------------------------------------------
Query 13> List employees whose name falls in alphabetic range ‘C’ and ‘L’.
SQL> select * from emp where ename between 'C%' and 'L%';
-------------------------------------------------------------------------------------------------------------------------
Query 14> List employees whose name starts with letter ‘T’ & ends with ‘R’.
-------------------------------------------------------------------------------------------------------------------------
Query 15> List all row s from Emp table by converting NULL value in COMM column to 0.
SQL> select empno, ename, job, mgr, hiredate, sal, nvl(comm, 0) "COMM", deptno from emp;
-------------------------------------------------------------------------------------------------------------------------
Query 16> List all employees who do not get any commission.
SQL> select ename from emp where comm = 0 or nvl(comm,0) = 0;
ENAME
----------
SMITH
JONES
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
-------------------------------------------------------------------------------------------------------------------------
Query 17> List name & hiredates of employees in department 20. Display the hiredate
formatted as ‘12/03/84’.
SQL> select ename, to_char(hiredate, 'mm/dd/yy') "HIREDATE" from emp where deptno =
20;
ENAME HIREDATE
---------- ---------------------
SMITH 12/17/80
JONES 04/02/81
SCOTT 04/19/87
ADAMS 05/23/87
FORD 12/03/81
-------------------------------------------------------------------------------------------------------------------------
ENAME HIREDATE
---------- ---------------------
SMITH 17-DEC-80
JAMES 03-DEC-81
FORD 03-DEC-81
-------------------------------------------------------------------------------------------------------------------------
Query 19> Find average annual salary in each department.
SQL> select deptno, avg(sal*12) "ANNUAL SALARY" from emp group by deptno;
-------------------------------------------------------------------------------------------------------------------------
SQL> select count(ename) "NO. OF EMPLOYEES" from emp where deptno = 30;
NO. OF EMPLOYEES
-----------------------------
6
-------------------------------------------------------------------------------------------------------------------------
Query 21> Compute the average, maximum & minimum salaries of each department.
SQL> select deptno,avg(sal) "AVG SAL",max(sal) "MAX SAL",min(sal) "MIN SAL" from
emp group by deptno;
-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
SQL> select ename, to_char(hiredate, 'MONTH, DDSP YYYY') "HIREDATE" from emp;
ENAME HIREDATE
---------- ----------------------------------------------------
SMITH DECEMBER , SEVENTEEN 1980
ALLEN FEBRUARY , TWENTY 1981
WARD FEBRUARY , TWENTY-TWO 1981
JONES APRIL , TWO 1981
MARTIN SEPTEMBER, TWENTY-EIGHT 1981
BLAKE MAY , ONE 1981
CLARK JUNE , NINE 1981
SCOTT APRIL , NINETEEN 1987
KING NOVEMBER , SEVENTEEN 1981
TURNER SEPTEMBER, EIGHT 1981
ADAMS MAY , TWENTY-THREE 1987
JAMES DECEMBER , THREE 1981
FORD DECEMBER , THREE 1981
MILLER JANUARY , TWENTY-THREE 1982
-------------------------------------------------------------------------------------------------------------------------
Query 26> Find all departments which have more than 3 employees.
SQL> select deptno, count(ename) from emp group by deptno having count(ename) > 3;
DEPTNO COUNT(ENAME)
---------- ------------------------------------
20 5
30 6
Query 27> Find average salary & average total enumeration for each job type remember.
SQL> select job, avg(sal) "SALARY", count(job) "No.of Employees" from emp group by job;
***************SUB-QUERIES***************
Query 29> Display employees who earn more than the lowest salary in department 30.
SQL> select ename from emp where sal > (select min(sal) from emp where deptno = 30);
ENAME
----------
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
FORD
MILLER
Query 30> List employees who earn more than every employee in department 30.
SQL> select ename from emp where sal > (select max(sal) from emp where deptno = 30);
ENAME
----------
JONES
SCOTT
KING
FORD
Query 32> List employees in department 20 or where salary is greater than 3000.
JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
**************PL/SQL***************
Query 1> Create view vEmp containing employee number, name & calculated annual salary
For department 10.
SQL> create view vemp as select empno, ename, sal*12 "ANNUAL SALARY" from emp
where deptno = 10;
View created.
Query 2> Create a view named vfleet from fleet_header such that it contains only the following
column day, depaeture time & the route_id.
SQL> create view vfleet as select day, fleet_header.route_id, time_travel from fleet_header,
ticket_
header where fleet_header.route_id = ticket_header.route_id;
View created.
SQL> select * from vfleet;
Query 3> Create view vemplocation from emp & department table with following columns –
Employee number, name, hiredate, department number & location.
SQL> create view vemplocation as select empno, ename, emp.deptno, loc from emp, dept
2 where emp.deptno = dept.deptno;
View created.
-------------------------------------------------------------------------------------------------------------------------
Query4> create sequence enoseq with the following specifications minimum value 0, maximum
value 20, increment by 2, start with 1 with cycle &cache 10.
Sequence created.
NEXTVAL
---------
1
NEXTVAL
---------
5
NEXTVAL
---------
7
-------------------------------------------------------------------------------------------------------------------------
Query 5> write a procedure to find whether the given year is leap year or not.
Procedure created.
-------------------------------------------------------------------------------------------------------------------------
Query 7> Write a procedure fibo to print Nth Fibonacci number where n is any integer passed
as parameter to it . 0 1 1 2 3 5………………………………….N
Query 8> Write a procedure to insert new records into DEPT table use sequence to generate
Department number, new-dname & new-loc are passed as parameters to calling procedure.
Query 9> Write a function spower which takes two arguments & return xⁿ.
Function created.
SPOWER(3,2)
-----------
9
-------------------------------------------------------------------------------------------------------------------------
Query 10> Write a function on EMP table which takes job & department as argument &
returns average salary for the given department & job type.
SQL> create function avg_sal (job1 in varchar2, dept in number)
2 return number is
3 aavgsal emp.sal%type;
4 begin
5 select avg(sal) into aavgsal from emp
6 where job = job1 and deptno = dept group by deptno;
7 return aavgsal;
8 end;
9 /
Function created.
AVERGAE SALARY
--------------
1300
-------------------------------------------------------------------------------------------------------------------------
Query 11> Write a function on EMP table which takes empno as argument & returns manager
name of the employee.
Function created.
________________________________________________________________________________
_
Query 14> Write a PL/SQL program which checks whether the quantity in hand of a
particular item was less than 20 then a message should be displayed as “Quantity Not Enough”
SQL> ed prod.sql;
declare
id product.prod_cd%type := &id;
qty product.qty_on_hand%type;
begin
select qty_on_hand into qty from product where prod_cd = id;
if qty < 20 then
dbms_output.put_line(id||': QUANTITY NOT ENOUGH');
end if;
end;
/
SQL> start prod.sql;
Enter value for id: 'P004'
old 2: id product.prod_cd%type := &id;
new 2: id product.prod_cd%type := 'P004';
P004: QUANTITY NOT ENOUGH
PL/SQL procedure successfully completed.
-------------------------------------------------------------------------------------------------------------------------
Query 15> Write a PL/SQL program that will select only those values from the ITEM table
where the itemid is 300 & Calculate the total to be price*quantity & print the value. If a value
is not found then message should be displayed.
declare
cursor c_prod is select item_id, actual_price, qty from item30 where item_id = 300;
tot number;
price item30.actual_price%type;
qty item30.qty%type;
id item30.item_id%type;
begin
open c_prod;
fetch c_prod into id, price, qty;
if c_prod%notfound then
dbms_output.put_line('NOT FOUND');
else
tot := price*qty;
dbms_output.put_line('TOTAL PRICE FOR ITEMID '||id||' is '||tot);
end if;
close c_prod;
end;
/
SQL> start it_qty.sql;
TOTAL PRICE FOR ITEMID 3000 is 2430000
PL/SQL procedure successfully completed.
SQL> start it_qty.sql;
NOT FOUND
PL/SQL procedure successfully completed.
-------------------------------------------------------------------------------------------------------------------------
Query 16> write a PL/SQL program that will select all rows from EMP table. The block should
print the number of rows returned, the values of empno column & the corresponding ename.
SQL> ed emp.sql;
DECLARE
CURSOR c_emp is select empno, ename from emp;
eno emp.empno%type;
ename emp.ename%type;
BEGIN
open c_emp;
dbms_output.put_line('EMPNO ENAME');
dbms_output.put_line('------------------------------');
loop
fetch c_emp into eno, ename;
exit when c_emp%NOTFOUND;
dbms_output.put_line(eno||' '||ename);
end loop;
dbms_output.put_line('------------------------------');
dbms_output.put_line(c_emp%ROWCOUNT||' ROWS SELECTED');
close c_emp;
END;
/
________________________________________________________________________________
_