Anda di halaman 1dari 20

Tutorials>>Oracle Queries

Oracle Queries
y y y y y y y y y y y

Create And Insert Commands Select Command And Like Operator Update Command Delete, Commit, Rollback And Save Point Alter Command Date Functions Numeric And Date Functions Numeric And Character Functions Views PL/SQL Programs Functions And Procedures

Create And Insert Commands>>employee table 1.Create a table 'employee' with the following fields empno varchar2 6(primary key) ename varchar2 20(not null) job char 10 hiredate date basic_sal number (9,2) comm number (7,2) dept_no varchar2 2 Q: create table employee(empno varchar2(6) primary key, ename varchar2(20) not null, job char(10), hiredate date, basic_sal number(9,2), comm number(7,2), dept_no varchar2(2)); 2.Describe the table 'employee' Q: desc employee

3.Insert the following data into the above 'employee' table

empno ename job hiredate basic_sal comm dept_no E0001 kim Manager 15-dec-02 5000 500 D001 24-AprE0002 Bruce Analyst 4000 400 D002 99 E0003 Arnold Clerk 10-jan-01 2500 250 D004 E0004 Holyfield Tester 10-oct-01 3000 300 D002 E0005 kelly Admin 11-apr-99 2000 200 D003
Q: insert into employee values('E0001','Kim','Manager', '15-Dec-02','5000.00','500','D001'); insert into employee values('E0002','Bruce','Analyst', '24-Apr-99','4000','400','D002'); insert into employee values('E0003','Arnold','Clerk', '10-jan-01','2500','250','D004'); insert into employee values('E0004','Holyfield','Tester', '10-oct-01','3000','300','D002'); insert into employee values('E0005','Kelly','Admin', '11-Apr-99','2000','200','D003');
Create And Insert Commands>>dept table 1.Create a table 'depttable' with the following fields deptno varchar2 8 dname varchar2 20 loc varchar2 20 Q: create table depttable(deptno varchar2(8), dname varchar2(20), loc varchar2(20)); 2. Describe the table 'depttable' Q: desc depttable 3. insert the following data into the above 'depttable' table

deptno

dname

loc

d001 Development Noida d002 R&D Rocky Creek d003 Admin Seattle d004 Accounts Seattle d005 Production Noida
Q: insert into depttable values('D001','Development','Noida'); insert into depttable values('D002','R And D', 'Rocky Creek'); insert into depttable values('D003','Admin','Seattle'); insert into depttable values('D004','Accounts','Seattle'); insert into depttable values('D005','Production','Noida');

Create And Insert Commands>>client_mast table 1. Create a table 'client_mast' with the following fields client_no varchar2 6(primary key) name varchar2 20(not null) address varchar2 25 city varchar2 20 pincode number 6 state varchar2 15 bal_due number (8,2) Q: create table client_mast(client_no varchar2(6) primary key, name varchar2(20) not null, address varchar2(25), city varchar2(20), pincode number(6), state varchar2(15), bal_due number(8,2)); 2. Describe table client_mast Q: desc client_mast 3. insert the following data into the above 'client_mast' table

client_no name B001 Procurez B002 BMW B003 Takenote B004 B005 Teoco ASAP

address 12,Sunbay street 6, Rocky creek 7,Hudsonbay 1243,Princiton circle 23,North city

city pincode state bal_due Gainesville 1233 Florida 3500 Jacksonville 1234 Florida 3488 Puria 6454 Illinois 4555 Fairfax Puria 3433 Virginia 4433 4354 Illinois 3600

Q: insert into client_mast values('B001','Procurez','12,sunbay street', 'Gainesville','1233','Florida','3500'); insert into client_mast values('B002','BMW','6,Rocky creek', 'Jacksonville','1234','Florida','3488'); insert into client_mast values('B003','Takenote','7,Hudsonbay', 'Puria','6454','Illinois','4555'); insert into client_mast values('B004','Teoco','1243,Princiton circle', 'Fair fax','3433','Virginia','4433'); insert into client_mast values('B005','ASAP','23,North city', 'Puria','4354','Illinois','3600'); Create And Insert Commands>>product_mast table 1. Create a table 'product_mast' with the following fields prodcut_no varchar2 6 description varchar2 14 profit_perc number (5,2) units varchar2 10 qty_available number 6 sell_price number (7,2) cost_price number (7,2) Q: create table product_mast(product_no varchar2(10), description varchar2(20), profit_perc number(5,2), units varchar2(10), qty_available number(6), sell_price number(7,2), cost_price number(7,2)); 2. Describe table product_mast Q: desc product_mast 3. insert the following data into the above 'product_mast' table

prodcut_no description profit_perc units qty_available sell_price cost_price pro23 CD Player 5 Piece 100 210 200 pro45 Television 10 Piece 35 1320 1200 pro55 Refrigerator 15 piece 60 920 800 pro65 Oven 12 Piece 40 504 450 pro75 Microwave 20 piece 100 900 750
Q: insert into product_mast values('&product_no', '&description','&profit_perc','&units', '&qty_available','&sell_price', '&cost_price'); Create And Insert Commands>>salesman_mast table 1. Create a table 'salesman_mast' with the following fields

salesman_no varchar2 10 name varchar2 20(not null) address varchar2 20 city varchar2 18 pincode number 6 state varchar2 15 sal_amount number (8,2) target number (5,2) Q: create table salesman_mast(salesman_no varchar2(10), name varchar2(20) not null, address varchar2(20), city varchar2(18), pincode number(6), state varchar2(15), sal_amount number(8,2), target number(5,2)); 2. Describe table salesman_mast Q: desc salesman_mast 3. insert the following data into the above 'salesman_mast' table

salesman_n pincod name address city o e Samm Jacksonvill sa001 43,NW 1234 y e 2, sa002 aron Gainesville 1233 Avenue Quinc 2,moon sa003 Janesville 1235 y App 3,Silversa004 Teddy Harryville 1233 spoons 4,st.stree sa005 Byron Puria 12 t
Q:

state Florida Florida Florida Tennesse e Illinois

sal_amoun targe t t 2500 2500 2500 2300 3500 200 200 200 200 100

insert into salesman_mast values('&salesman_no','&name', '&address','&city','&pincode','&state','&sal_amount', '&target');

Oracle Queries>>Select And Like Operators I. employee table 1. List the names of the employee who have a salary less than Rs.3000 from employee table Q. select ename from employee where basic_sal<3000 2. List the employee name, job and department number of

everyone whose name fall in the alphabetical range 'C' to 'L' from employee table. Q. 3. List all the employees whose name starts with the letter 'K' from employee table. Q. select ename from employee where ename like 'K%'; 4. List the employee name working in department D002, D003 from employee table. Q. select ename from employee where dept_no='D002' or dept_no='D003'; 5. List all employee whose name start with 'A' and end with 'd' from employee table. Q. select ename from employee where ename like 'A%d'; 6. List all managers and salesman with salary over 2500 from employee table. Q. select ename from employee where (job='Manager' or job='Sales man') and basic_sal>2500; 7. Display all the employee names in the ascending order of their date of joining from employee table. Q. select ename from employee order by hiredate asc; 8. Display all the employees in alphabetical order from employee table. Q. select ename from employee order by ename; 9. List all employee who where hired during 1999 from employee table. Q. select ename from employee where to_char(hiredate,'yy')=99; 10. List all employees whose commission is more than Rs.300 from employee table. Q. select ename from employee where comm>300; II.client_mast table 1. Find out the names of all the clients from client_mast table. Q. select name from client_mast; 2. Retrieve all the records from client_mast table Q. select * from client_mast; 3. Retrieve all the list of names, address and city of all the clients from client_mast Q. select name,address,city from client_mast; 4. List all the clients who are staying in Florida from

client_mast table Q. select name from client_mast where state="Florida"; III.Dept table 1. List the department name which is located in Noida and Rockey Creek from Dept table. Q. select dname from depttable where loc='Noida' or loc='Rocky Creek'; Oracle Queries>>Update Commands I. employee table 1. Change the basic salary Rs.3000 where basic salary less than 2500 from employee table Q. update employee set basic_sal=3000 where basic_sal<2500; 2. Change the basic_sal=3000 where job in clerk from employee table. Q. update employee set basic_sal=3000 where job='Clerk'; 3. Change the basic_salary of employee number E004 to Rs. 3500 from employee table. Q. update employee set basic_sal=3500 where empno='E0004'; II.client_mast table 1. Change the city of client_no 'B001' from 'Gainesville' to 'Paul Street' from client_mast table. Q. update client_mast set city='Paul Street' where client_no='B001'; 2. Change the bal_due of client no B005 to Rs.2000 from client_mast table. Q. update client_mast set bal_due=2000 where client_no='B005'; 3. Change the name to 'infospace' of client_no B004 in client_mast table. Q. update client_mast set name='infospace' where client_no='B004'; 4. Change the client_no 'B004' to 'B009' in the table client_mast. Q. update client_mast set client_no='B009' where client_no='B004'; III.Dept table 1. Change the department name to 'sales' from the DEPT tale where deptno is 'd004'. Q. update depttable set dname='sales' where deptno='D004'; IV.salesman_mast table

1. Change the city of the salesman from 'Jacksonville' to 'Huston' from salesman_mast table. Q. update salesman_mast set city='Huston' where city='Jacksonville'; V. product_mast table 1. Change the description of product number 'PR065' to AC in the product_mast table. Q. update product_mast set description='AC' where product_no='PR065'; 2. Change the profit percent of product number 'PR065' to 25% in the product_mast table. Q. update product_mast set profit_perc=25 where product_no='PR065'; 3. Change the available quantity of product number 'PR065' to 120 in the product_mast table. Q. update product_mast set qty_available=120 where product_no='PR065'; 4. Change the cost price and selling price of product number 'PR065' to 5000 and 6250 in the product_mast table. Q. update product_mast set sell_price=5000, cost_price=6250 where product_no='PR065'; 5. Change the units where product number 'PR065' to 'pack of 10' in the product_mast table. Q. update product_mast set units='Pack of 10' where product_no='PR065'; Oracle Queries>>Delete, Commit, Rollback and Renaming A Table

Delete, Commit, Rollback and Renaming A Table


1. Delete all salesman from the salesman_mast whose salaries are equal to Rs.2000. Q. delete from salesman_mast where sal_amount=2000; 2. Write a query to undo the above delete query. Q. rollback; 3. Delete all products from product_mast where the Qty_Available is equal to 100. Q. delete from product_mast where qty_available=100 ; 4. Write a query such that the above query cannot be undone.

Q. commit; 5. Delete from client_mast where the state venue is "Illinois". Q. delete from client_mast where state='Illinois'; 6. Delete all employees from employee where basic_sal is less than 2000. Q. delete from employee where basic_sal<2000; 7. Write a query to mark the above two queries such that the first delete operation is undo Q. savepoint d1; delete from client_mast where state='Illinois'; rollback to d1; 8. Delete employee from the employee table where basic_sal is equal to Rs.3000 and job='clerk'. Q. delete from employee where basic_sal=3000 and job='Clerk'; 9. Delete employee from the employee table where job='clerk' or manager. Q. delete from employee where job='Clerk' or job='Manager'; 10. Delete all records from depttable table. Q. delete from depttable; 11. Delete all managers and salesman with salary over 1500 from employee table. Q. delete from employee where job='Manager' or job='salesman' and basic_sal>1500; 12. Delete all employee name, job and department number of everyone whose name fall in the alphabetical range 'C' to 'L' from employee table. Q. 13. Write a query so the the following statement can be delivered 'Kim is working as manager since 15-12-2002' where employee number is E0001. Q. select ename || ' is working as ' || job ||'since' || hiredate from employee where ename='Kim'; 14. Rename the table name from client_mast to T_client_mast. Q. rename client_mast to T_client_mast; 15. Write a query the column name 'name' should be displayed as employee name from the employee table.

Q. select ename as "Name" from employee; Oracle Queries>>Alter Command

Alter Command
1. Add a column "Telephone_no" of data type 'number' and size ='10' to the employee table. Q. alter table employee add(telephone_no number(10)); 2. Add a column "country" of datatype 'char' and size='15' to the client_mast table. Q. alter table client_mast add (country char(15)); 3. Increase the size of "description" 20 to 25 in product_mast table. Q. alter table product_mast modify (description varchar2(25)); 4. Modify the "product_no" key as a primary key from product_mast table. Q. alter table product_mast add primary key(product_no); 5. Drop the primary key of client_mast table. Q. alter table client_mast drop primary key; 6. Define the "deptno" as not null constraint from dept table. Q. alter table depttable modify deptno not null; 7. Display empno, ename, and annual salary of each employee from employee table with a column name "Annual Salary" on the basis of basic_sal. Q. select empno as "Employee Number", ename as "Employee Name", basic_sal*12 as "Annual salary" from employee; 8. Add two columns"HOD" and "Strength" of datatype varchar2 and size=15 to the dept table. Q. alter table depttable add (hod varchar2(15),strength varchar2(15)); 9. Add a check constraint for deptno field to the dept table check deptno from the 'D001', 'D002' and 'D003'. Q. Date Functions>>sysdate And add_months Functions

sysdate And add_months Functions


sysdate: 1. Display the system date Q. select sysdate from dual;

2. Display 10 days after the system date Q. select sysdate+10 from dual; add_months: 1. Display 3 months after from the system date Q. select add_months(sysdate,3) from dual; 2. Display 12 before from the date '12-mar-1990' Q. select add_months('12-mar-1990',-12) from dual; 3. Using the fractional number Q. select add_months('12-mar-1990',1.5) from dual; [add_months() converts the fractional number as whole number] select add_months('12-mar-1990',1) from dual; [The above two queries are same] some examples Q1. select add_months('28-feb-1989',1.9999) from dual; (or) select add_months('28-feb-1989',1) from dual; Q2. select add_months('28-feb-1989',-12.9999) from dual; (or) select add_months('28-feb-1989',-12) from dual; Q3. select add_months('28-feb-1989',0.5) from dual; (or) select add_months('28-feb-1989',0) from dual; Date Functions>>last_day Function

last_day Function
last_day: 1. Display the last day of the system date Q. select last_day(sysdate) from dual; 2. Display the last day of 31st May 2010 Q. select last_day('31-may-10') from dual; 3. Display last day of the month 3 months after from the system date Q. select last_day(add_months(sysdate,3)) from dual; 4. select the last day of '16-feb-2010' Q. select last_day('16-feb-10') from dual; Date Functions>>new_time Function

new_time Function
The following example shows the time how it is different from one zone to

another Q. select to_char(new_time(to_date('09151994 12:30 AM','MMDDYYYY HH:MI AM'), 'CST','hdt'),'Month DD,YYYY HH:MI AM') from dual; Date Functions>>next_day Function

next_day Function
1. Display the first monday in the year 2010 Q. select next_day('01-jan-2010','monday') from dual; 2. Display first thrusday in the year 2011 Q. select next_day('01-jan-2011','thursday') from dual; 3. Display next friday of the system date Q. select next_day(sysdate,'friday') from dual; Date Functions>>to_date and to_char with round function

to_date and to_char with round function


round(): 1. Write a query to round back to current century Q. select to_char(round(to_date('22-may-2010'),'cc'),'DD-mon-yyyy') from dual; 2. Write a query to round up to next century Q. select to_char(round(to_date('22-sep-2094'),'cc'),'DD-mon-yyyy') from dual; 3. Write a query to round down to the first of the year Q. select round(to_date('22-may-2010'),'yyyy') from dual; 4. Write a query to round up to the first of the year Q. select round(to_date('22-sep-2010'),'year') from dual; 5. Write a query to round up to the quarter(first date in the quarter) Q. select round(to_date('12-mar-1994'),'Q') from dual; 6. Write a query to round to the quarter Q. select round(to_date('12-aug-2010'),'Q') from dual; select round(to_date('12-dec-2010'),'Q') from dual; 7. Write a query to print the day of the particular date Q. select to_char(to_date('22-may-2010'),'day') from dual;

select to_char(to_date(sysdate,'day') from dual; 8. Write a query round to date of nearest sunday for '22-may-2010' Q. select round(to_date('22-may-2010'),'ww') from dual; 9. write a query round back to nearest day(time always midnight). Q. select to_char(round(to_date('11-sep-2010 10:00 AM','DD-Mon-yy HH:MI AM'), 'DD'),'DD-Mon-yy HH:MI AM') from dual; 10. write a query to round forward to the nearest day Q. select to_char(round(to_date('11-sep-2010 4:00 PM','DD-Mon-yy HH:MI AM'), 'DD'),'DD-Mon-yy HH:MI AM') from dual; 11. Write a query to round back to the nearest hour Q. select to_char(round(to_date('11-sep-2010 4:17PM','DD-Mon-yy HH:MI AM'), 'HH'),'DD-Mon-yy HH:MI AM') from dual; Date Functions>>sysdate And add_months Functions

sysdate And add_months Functions


sysdate: 1. Display the system date Q. select sysdate from dual; 2. Display 10 days after the system date Q. select sysdate+10 from dual; add_months: 1. Display 3 months after from the system date Q. select add_months(sysdate,3) from dual; 2. Display 12 before from the date '12-mar-1990' Q. select add_months('12-mar-1990',-12) from dual; 3. Using the fractional number Q. select add_months('12-mar-1990',1.5) from dual; [add_months() converts the fractional number as whole number] select add_months('12-mar-1990',1) from dual; [The above two queries are same] some examples Q1. select add_months('28-feb-1989',1.9999) from dual; (or) select add_months('28-feb-1989',1) from dual; Q2. select add_months('28-feb-1989',-12.9999) from dual; (or)

select add_months('28-feb-1989',-12) from dual; Q3. select add_months('28-feb-1989',0.5) from dual; (or) select add_months('28-feb-1989',0) from dual;

Oracle Queries on Functions


1. Select those clients whose bal_due is greater than value 2000 from client_mast table. Q. select name,bal_due from client_mast where bal_due>2000; 2. Calculate the average price of all the products from Product_mast table. Q. select avg(cost_price) from product_mast; 3. calculate the maximum and minimum product prices and rename the output as max_price and min_price respectively from product_mast table. Q. select max(cost_price) as "max_price", min(cost_price) as "min_price" from product_mast ; 4. Count the number of products having price greater than or equal to 1200. Q. select count(*) from product_mast where cost_price>=1200; 5. Display employee number and day on which employee hired from employee table. Q. select empno,to_char(to_date(hiredate),'DAY') as Day from employee; 6. Display the total no.of working days of each employee from employee table. Q. select ename as "Name",months_between(sysdate,hiredate) as "No.of days" from employee; 7. Display the date, 20 days after today's date. Q. select sysdate+20 from dual; 8. Count all distinct job types from employee table. Q. select count(distinct job) from employee; 9. Using LPAD function pad the job types with 15 characters by '*' symbol Q. select lpad(job,15,'*') job from employee; 10. Display the length of each employee name from employee table. Q. select length(ename) from employee; 11. Display the value of 3 raised to 4 using power function

Q.

select power(4,3) from dual;

12. Display the square root of 81 Q. select sqrt(81) from dual; 13. Display the number of bytes used by ename from employee table. Q. select vsize(ename) from employee; 14. Display the ename those are having sound like Kim from employee table. Q. select soundex('Kim') from employee;

1. Create a view as empview from employee table where basic_sal=3000 and job='Clerk' Q. create view empview as select ename from employee where basic_sal=3000 and job='Clerk '; 2. Display the result using above view Q. select * from empview; 3. Drop the above created view Q. drop view empview; 4. Create a view as empview from employee table with basic_sal,empno,ename. Q. create view empview as select basic_sal,empno,ename from employee; 5. Display the result using the above create view Q. select * from empview; 6. create a view as clientview from client_mast with all columns of table Q. create view clientview as select * from client_mast; 7. Drop the above create empview Q. drop view empview;

views in oracle

PL/SQL programs in oracle PL/SQL blocks


y y y y y

PL/SQL block to find out the square of a number Write a PL/SQL block to find out the square of a number. sql>ed <filename> //to open a file Q. Set Serveroutput on

y y y y y y y y y

Declare x number := &x; s number; Begin s := x*x; Dbms_output.put_line('Square of'||x||'='||s); end; / sql>@<filename> //to run the PL/SQL block

PL/SQL block to compute the area of the circle and square


Write a PL/SQL block to compute the area of the circle and square. Q. Set Serveroutput on Declare r number(5,2) := &r; s number(5,2) := &s; ca number(5,2); sa number(5,2); Begin ca := (3.14)*r*r; sa := s * s; Dbms_Output.put_line('area of circle='||ca); Dbms_Output.put_line('area of square='||sa); End; /

PL/SQL block to conversion of celsius to faranheat and viceversa


rite a PL/SQL block to convert celsius to fahrenheat and viceversa. Q. set serveroutput on declare ct number(5,2) := &ct; ftc number(5,2); ft number(5,2) := &ft; ctc number(5,2); begin ftc := (ct*9/5)+32; dbms_output.put_line('Faran heat temparature='||ftc); ctc := (ft-32)*5/9; dbms_output.put_line('Celsius temparature='||ctc); end; /

PL/SQL block to find largest from the three nos


Write a PL/SQL block to find big among 3 nos. Q. set serveroutput on declare

a number := &a; b number := &b; c number := &c; begin if(a>b and a>c) then dbms_output.put_line('big number='||a); elsif(b>c) then dbms_output.put_line('big number='||b); else dbms_output.put_line('big number='||c); end if; end; /

PL/SQL block to find factorial value of any no


Write a PL/SQL block to find factorial of a number. set serveroutput on declare n number := &n; fact number :=1; i number :=1; begin for i in 1..n loop fact := fact*i; end loop; dbms_output.put_line('factorial='||fact); end; /

PL/SQL block to find whether the entered year is leap year or not
Write a PL/SQL block to find leap year or not. set serveroutput on declare year number:= &year; begin if(mod(year,4) = 0) then dbms_output.put_line('Leap year'); else dbms_output.put_line('Not leap year'); end if; end; /

PL/SQL block to display first 20 odd nos PL/SQL block to display first 20 odd numbers
To display first 20 odd numbers. set serveroutput on declare ctr number(2) :=1; i number :=1; begin while ctr <=20 loop if(mod(i,2)!=0) then ctr := ctr+1; dbms_output.put_line(i); end if; i := i+1; end loop; end; /

PL/SQL block to display first 10 nos divisible by 3 and 5 PL/SQL block to display first 10 numbers divisible by 3 and 5
To display first 10 numbers divisible by 3 and 5. set serveroutput on declare ctr number(2) :=1; i number :=1; begin while ctr <=10 loop if(mod(i,3)=0 and mod(i,5)=0) then ctr := ctr+1; dbms_output.put_line(i); end if; i := i+1; end loop; end; /

PL/SQL block for displaying student grade PL/SQL block to display student result
To display student result set serveroutput on declare sub1 number := &sub1; sub2 number := &sub2; sub3 number := &sub3; total number; avge number(5,2);

begin total := sub1+sub2+sub3; avge := total/3; dbms_output.put_line('total='||total); dbms_output.put_line('average='||avge); if(avge<50) then dbms_output.put_line('Fail'); elsif(avge>=50 or avge<60) then dbms_output.put_line('Second division'); elsif(avge>=60 or avge<75) then dbms_output.put_line('First division'); elsif(avge>=75) then dbms_output.put_line('Distinction'); end if; end; /

Procedures and functions in oracle Procedures and functions


y

Create a function for cube of any number

Function for cube of number in oracle


Create a function for cube of any number. create or replace function fncube(n number) return number is begin return n*n*n; end; Running a function sql>@ sql>function create successfully sql>select (parameter) from dual;

Create a function for Simple Interest Function to find simple interest in oracle
Create a function to find simple interest. create or replace function simple(p number,n number,r number) return number is begin return(p*n*r/100); end;

/ Running a function sql>@ sql>function create successfully sql>select (parameter) from dual;

Create a recurssive function for generating factorial Finding factorial using recurssion in oracle
Finding factorial using recurssion create or replace function rfact(n positive) return integer begin if(n=1) then return 1; else return n*rfact(n-1); end if; end; / is

Anda mungkin juga menyukai