0% found this document useful (0 votes)
16 views18 pages

Practical Query 1 To 17

Uploaded by

anujpatel28104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views18 pages

Practical Query 1 To 17

Uploaded by

anujpatel28104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

305-M.Sc(CA&IT) Grow More Institute of M.

Sc(CA&IT)
[Create following Three Tables:-

Salesman:

SNUM SNAME CITY COMMISSION(%)

PIYUSH LONDON
1001 12
1002 NIRAJ SURAT 13

1003 MITI LONDON 11

1004 RAJESH BARODA 15

1005 ANAND NEW DELHI 10

1006 RAM PATAN 10

1007 LAXMAN BOMBAY 09

SNUM : A Unique number assign to each salesman. SNAME: The name ofsalesman.
CITY :The location ofsalesman.
COMMISSION : The salesman commission on order.

Customer:
customer
CNUM CNAME CITY RATING SNUM
2001 HARDIK LONDON 100 1001
2002 GITA ROME 200 1003
2003 LAXIT SURAT 200 1002
2004 GOVIND BOMBAY 300 1002
2005 CHANDU LONDON 100 1001
2006 CHAMPAK SURAT 300 1007
2007 PRATIK ROME 100 1004
Create table customer
(Cnum number(10)primary key,
Cname varchar2(10),
City varchar2(10),
Rating number(10),
Snum number(10),
Foreign key(snum) references salesman(snum));

CNUM : A Unique number assign to eachcustomer. CNAME: The name ofcustomer.


CITY :The location ofcustomer.
RATING: A level of preference indicator given to this customer. SNUM : A salesman number
assign to this customer.

Asst.Prof. Nilam suthar


Pgdca dept Page 1
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)

Order:-

ONUM AMOUNT ODATE CNUM SNUM


3001 18.69 10/03/99 2008 1007
3002 767.19 10/03/99 2001 1001
3003 1900.10 10/03/99 2007 1004
3004 5160.45 10/03/99 2003 1002
3005 1098.25 10/04/99 2008 1007
3006 1713.12 10/04/99 2002 1003
3007 75.75 10/05/99 2004 1002
3008 4723.00 10/05/99 2006 1001
3009 1309.95 10/05/99 2004 1002
3010 9898.87 10/06/99 2006 1001

ONUM : A Unique number assign to each Order. AMOUNT: Amount of order in Rs.
ODATE : The date of order.
CNUM : The number of customer making the order. SNUM : The number of salesman
credited with the sale.

Asst.Prof. Nilam suthar


Pgdca dept Page 2
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)

How to Create Tables:-


how to create table in oracle using sql.

first of all define the structure of the table.

query: create table salesman


(snum number(4),
sname varchar2(10),
city varchar2(10),
commission number(2));

customer table:

query: create table customer


(cnum number(4),
cname varchar2(10),
city varchar2(10),
rating number(3),
snum number(4),
foreign key(snum) references salesman(snum));

Orders table:

query: create table orders


(onum number(4),
amount number(6,2),
odate date,
cnum number(4),
foreign key(cnum) references customer(cnum));
snum number(4),
foreign key(snum) references salesman(snum));

Note: Please Do not use ‘order’ as table name. You can use Orders or order1 etc.

Asst.Prof. Nilam suthar


Pgdca dept Page 3
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)

Asst.Prof. Nilam suthar


Pgdca dept Page 4
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)
How to Insert Data: Salesman Table:
Method 1: (If you want to insert data like data entry form then use this method) insert into
salesman values(&snum,'&sname','&city',&commission);
now Enter Values. If you want to execute same query or previous query again the just type
/ and press enter key.

Method 2: (If you want add data in all fields then use this method) insert into salesman
values (1001,’PIYUSH’,’LONDON’,12);
insert into salesman values (1002,’NIRAJ’,’SURAT’, 13); insert into salesman values
(1003,’MITI’,’LONDON’,11); insert into salesman values (1004,’RAJESH’,’BARODA’,15);
insert into salesman values (1005,’ANAND’,’NEW DELHI’,10); insert into salesman values
(1006,’RAM’,’PATAN’,10);
insert into salesman values (1007,’LAXMAN’,’BOMBAY’,09);

Method 3: (If you want to add data in selected fields then use this method)

Insert into salesman(snum,sname,city,commission) values(1001,’PIYUSH’,’LONDON’,12);

Customer Table:

Method 1:
insert into customer values(&cnum,'&cname','&city',&rating,&snum);

Method 2:
insert into customervalues(2001,’HARDIK’,’LONDON’,100,1001); insert into customer
values(2002,’GITA’,’ROME’,200,1003); insert into customer
values(2003,’LAXIT’,’SURAT’,200,1002); insert into
customervalues(2004,’GOVIND’,’BOMBAY’,300,1002);
insert into customer values(2005,’CHANDU’,’LONDON’,100,1001); insert into customer
values(2006,’CHAMPAK’,’SURAT‘,300,1007); insert into customer
values(2007,’PRATIK’,’ROME’,100,1004);

Method 3:
insert into
customer(cnum,cname,city,rating,snum)values(2001,’HARDIK’,’LONDON’,100,1001);

Orders Table:

Method 1:
insert into orders values(&onum,&amount,'&odate',&cnum,&snum);

Method 2:
insert into orders values(3001,18.69,’10/03/99’,2008,1007); insert into orders
values(3002,767.19,’10/03/99’, 2001,1001); insert into orders
values(3003,1900.10,’10/03/99’,2007,1004); insert into orders
values(3004,5160.45,’10/03/99’,2003,1002); insert into orders
values(3005,1098.25,’10/04/99’,2008,1007); insert into orders
values(3006,1713.12,‘10/04/99’,2002,1003); insert into orders
values(3007,75.75,’10/05/99’,2004,1002); insert into orders
values(3008,4723.00,’10/05/99’,2006,1001); insert into orders
values(3009,1309.95,10/05/99,2004,1002); insert into orders
values(3010,9898.87,10/06/99,2006,1001);

Asst.Prof. Nilam suthar


Pgdca dept Page 5
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)

Method 3:
insert into
orders(onum,amount,odate,cnum,snum)values(3001,18.69,’10/03/99’,2008,1007);

Asst.Prof. Nilam suthar


Pgdca dept Page 6
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)
SOLVED SQL QUERIES:-
========================================================
==========

Produce the order no, amount and date of all orders.

ONUM AMOUNT ODATE CNUM SNUM


---------- ---------- -------------- --------- ---------
300 18.69 10- 2008 1007
1 MAR-99
300 767.19 10-MAR- 2001 1001
2 99
300 1900.10 10- 2007 1004
3 MAR-99
300 5160.45 10- 2003 1002
4 MAR-99
300 1098.25 10-APR- 2008 1007
5 99
300 1713.12 10-APR- 2002 1003
6 99
300 75.75 10- 2004 1002
7 MAY-99
300 4723.00 10-MAY- 2006 1001
8 99
300 1309.95 10-MAY- 2004 1002
9 99
301 9898.87 10-JUN- 2006 1001
0 99

SQL> select onum, amount,odate from orders;

ONUM ----------
---------- AMOUNT ---------
ODATE

3001 18.69 10-MAR-99


3002 767.19 10-MAR-99
3003 1900.1 10-MAR-99
3004 5160.45 10-MAR-99
3005 1098.25 10-APR-99
3006 1713.12 10-APR-99
3007 75.75 10-MAY-99
3008 4723 10-MAY-99
3009 1309.95 10-MAY-99
3010 9898.87 10-JUN-99
10 rows selected.

========================================================
==========

Give all the information about all the customers with salesman number 1001.

CNUM CNAME CITY RATING SNUM


---------- ---------- ---------- ---------- ----------
2001 hardik london 100 1001
2002 gita rome 200 1003

Asst.Prof. Nilam suthar


Pgdca dept Page 7
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)
2003 laxit surat 200 1002
2004 govind 300 1002
bombay
2005 chandu 100 1001
london
2006 champak 300 1007
surat
2007 pratik rome 100 1004

SQL> select * from customer where snum=1001; CNUM CNAME CITY RATING
SNUM
---------- ---------- ---------- ---------- ----------
2001 hardik London 100 1001
2005 chandu London 100 1001

========================================================
==========

Display the information in the sequence of city, sname, snum and Commission.

SNUM SNAME CITY COMMISSION

Asst.Prof. Nilam suthar


Pgdca dept Page 8
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)
---------- --------- ---------- ----------
1001 london 12
piyush
1002 Surat 13
niraj
1003 miti London 11
1004 baroda 15
rajesh
1005 new delhi 10
anand
1006 ram Patan 10
1007 9
laxman bombay
SQL> select city, sname, snum, commission from salesman;

CITY SNAME SNUM COMMISSION


---------- ---------- -------- ----------
london 1001 12
piyush
surat niraj 1002 13
london 1003 11
miti
baroda 1004 15
rajesh
new delhi 1005 10
anand
patan 1006 10
ram
Bombay 1007 9
laxman

7 rows selected.

========================================================
==========

List of rating followed by the name of each customer inSurat.

CNUM CNAME CITY RATING SNUM


---------- ---------- ---------- ---------- ----------
2001 hardik london 100 1001
2002 gita rome 200 1003
2003 laxit surat 200 1002
2004 govind 300 1002
bombay
2005 chandu 100 1001
london
2006 champak 300 1007
surat
2007 pratik rome 100 1004
SQL> select cname, ratingfrom customer where city='surat'; CNAME RATING
---------- ----------

Asst.Prof. Nilam suthar


Pgdca dept Page 9
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)
Laxit 200
Champak 300

========================================================
==========

4. List of snum of all salesmen with orders in order table without an duplicates.

ONUM AMOUNTODATE CNUM SNUM


---------- ---------- --------- ---------- ---------- 3001 18.6910-MAR-99 2008 1007
300 767.19 10-MAR- 2001 1001
2 99
300 1900.1 10-MAR- 2007 1004
3 99
300 5160.45 10-MAR- 2003 1002
4 99
300 1098.25 10-APR- 2008 1007
5 99
300 1713.12 10-APR- 2002 1003
6 99
300 75.75 10-MAY-99 2004 1002
7
300 4723 10-MAY-99 2006 1001
8
300 1309.95 10-MAY- 2004 1002
9 99
301 9898.87 10-JUN- 2006 1001
0 99

SQL> select distinct(snum) from orders;

Asst.Prof. Nilam suthar


Pgdca dept Page 10
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)

SNUM
----------
1001
1002
1003
1004
1007

========================================================
==========

List of all orders for more than Rs.1000.

ONUM AMOUNTODATE CNUM SNUM


---------- ---------- --------- ---------- ---------- 3001 18.6910-MAR-99 2008 1007
300 767.19 10-MAR- 2001 1001
2 99
300 1900.1 10-MAR- 2007 1004
3 99
300 5160.45 10-MAR- 2003 1002
4 99
300 1098.25 10-APR- 2008 1007
5 99
300 1713.12 10-APR- 2002 1003
6 99
300 75.75 10-MAY-99 2004 1002
7
300 4723 10-MAY-99 2006 1001
8
300 1309.95 10-MAY- 2004 1002
9 99
301 9898.87 10-JUN- 2006 1001
0 99

SQL> select * from orders where amount>1000; ONUM AMOUNTODATE CNUM


SNUM
---------- ---------- --------- ---------- ---------- 3003 1900.110-MAR-99 2007 1004
300 5160.45 10-MAR- 2003 1002
4 99
300 1098.25 10-APR- 2008 1007
5 99
300 1713.12 10-APR- 2002 1003
6 99
300 4723 10-MAY-99 2006 1001
8
300 1309.95 10-MAY- 2004 1002
9 99
301 9898.87 10-JUN- 2006 1001
0 99

7 rows selected.

Asst.Prof. Nilam suthar


Pgdca dept Page 11
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)

========================================================
==========
List out names and cities of all salesmen in London with commission above10%

SNUMSNAME CITY COMMISSION


---------- ---------- ---------- ----------
1001 London 12
piyush
1002 niraj Surat 13
1003 miti London 11
1004 Baroda 15
rajesh
1005 new delhi 10
anand
1006 ram Patan 10
1007 bombay 9
laxman
SQL>select sname,city,commission from salesman where city='london' and
commission>10;

SNAME CITY COMMISSION


---------- --------------------
piyush london 12
miti london 11

Asst.Prof. Nilam suthar


Pgdca dept Page 12
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)
========================================================
==========

List all customers excluding those with rating <= 100 or they are located in Rome.

CNUMCNAME CITY RATING SNUM


---------- ---------- ---------- ---------- ----------
2001hardik London 100 1001
2002gita rome 200 1003
2003laxit surat 200 1002
2004govind Bombay 300 1002
2005chandu London 100 1001
2006 champak 300 1007
surat
2007pratik rome 100 1004
SQL>select*from customer where not(rating<=100orcity='rome'); CNUMCNAME CITY
RATING SNUM
----- ---------- ---------- ---------- ----------
2003laxit surat 200 1002
2004govind bombay 300 1002
2006champak 300 1007
surat

========================================================
==========
List all order for more than Rs.1000 except the orders of snum,1006 of 10/03/99 ONUM
AMOUNTODATE CNUM SNUM
---------- ---------- --------- ---------- ----------
3001 18.6910-MAR-99 2008 1007
300 767.19 10-MAR- 2001 1001
2 99
300 1900.1 10-MAR- 2007 1004
3 99
300 5160.45 10-MAR- 2003 1002
4 99
300 1098.25 10-APR- 2008 1007
5 99
300 1713.12 10-APR- 2002 1003
6 99
300 75.75 10-MAY-99 2004 1002
7
300 4723 10-MAY-99 2006 1001
8
300 1309.95 10-MAY- 2004 1002
9 99
301 9898.87 10-JUN- 2006 1001
0 99
SQL>select * from orders where amount>1000 and not(snum=1006andodate='10-mar-
99'); ONUM AMOUNTODATE CNUM SNUM
---------- ---------- --------- ---------- ---------- 3003 1900.110-MAR-99 2007 1004
300 5160.45 10-MAR- 2003 1002
4 99

Asst.Prof. Nilam suthar


Pgdca dept Page 13
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)
300 1098.25 10-APR- 2008 1007
5 99
300 1713.12 10-APR- 2002 1003
6 99
300 4723 10-MAY-99 2006 1001
8
300 1309.95 10-MAY- 2004 1002
9 99
301 9898.87 10-JUN- 2006 1001
0 99
========================================================
==========

Asst.Prof. Nilam suthar


Pgdca dept Page 14
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)

List all orders taken on 10th March, April and June 1999.

SQL> select * from orders where odate='10-mar-99' or odate='10-apr-99' or odate='10-


jun-99';
ONUM AMOUNTODATE CNUM SNUM
---------- ---------- --------- ---------- ---------- 3001 18.6910-MAR-99 2008 1007
300 767.19 10-MAR- 2001 1001
2 99
300 1900.1 10-MAR- 2007 1004
3 99
300 5160.45 10-MAR- 2003 1002
4 99
300 1098.25 10-APR- 2 1007
5 99 008
300 1713.12 10-APR- 2002 1003
6 99
301 9898.87 10-JUN- 2006 1001
0 99

Or

SQL> select * from orders where odate in ('10-mar-99','10-apr-99','10-jun-99');

ONUM AMOUNTODATE CNUM SNUM


---------- ---------- --------- ---------- ---------- 3001 18.6910-MAR-99 2008 1007
300 767.19 10-MAR- 2001 1001
2 99
300 1900.1 10-MAR- 2007 1004
3 99
300 5160.45 10-MAR- 2003 1002
4 99
300 1098.25 10-APR- 2 1007
5 99 008
300 1713.12 10-APR- 2002 1003
6 99
301 9898.87 10-JUN- 2006 1001
0 99
========================================================
==========

Asst.Prof. Nilam suthar


Pgdca dept Page 15
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)

List all customers whose names begins with a letter'C'.

CNUMCNAME CITY RATING SNUM


---------- ---------- ---------- ---------- ----------
2001hardik London 100 1001
2002gita rome 200 1003
2003laxit surat 200 1002
2004govind bombay 300 1002
2005chandu london 100 1001
2006 champak 300 1007
surat
2007pratik rome 100 1004
SQL> select * from customer where cname like'c%'; CNUMCNAME CITY RATING
SNUM
---------- ---------- ---------- ---------- ----------
2005chandu london 100 1001
2006 champak surat 300 1007
========================================================
==========
11. List all customers whose names begins with letter 'A' to'E'

SQL> select * from customer where cname like 'a%' or cname like 'b%' or cname like 'c%'
or cname like 'd%' or cname like 'e%' or cname like 'f%' or cname like 'g%';

CNUMCNAME CITY RATING SNUM


---------- ---------- ---------- ---------- ----------
2002gita rome 200 1003
2004govind bombay 300 1002
2005chandu london 100 1001
2006 champak surat 300 1007

4 rows selected

========================================================
==========

12. List all orders with zero or NULL amount.

SQL> select * from orders where amount=0 or amount is NULL; no rows selected

========================================================
==========

Find out the largest orders of salesman 1002 and1007.


SQL> select max(amount) LARGEST_ORDER from orders where snum in(1002,1007);
LARGEST_ORDER
-------------
5160.45

OR
SQL> select snum, max(amount) from orders where snum in(1002,1007) group by snum;

Asst.Prof. Nilam suthar


Pgdca dept Page 16
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)
SNUM MAX(AMOUNT)
---------- ----------- 1002 5160.45
1007 1098.25

OR
SQL>select snum,max(amount)max from orders group by snum having snum=1002 or
snum=1007; SNUM MAX
---------- ----------
1002 5160.45
1007 1098.25

OR
SQL> select snum,max(amount) max from orders where snum=1002 or snum=1007 grop
bysnum; SNUM MAX
---------- ---------- 1002 5160.45
1007 1098.25
========================================================
==========

Asst.Prof. Nilam suthar


Pgdca dept Page 17
305-M.Sc(CA&IT) Grow More Institute of M.Sc(CA&IT)

Count all orders of10-Mar-97.


SQL> select count(odate) as "No. of Orders on 10-MAR-99" from orders where odate='10-
mar-99'; No. of Orders on 10-MAR-99
--------------------------
4
========================================================
==========

14. Calculate the average and sum of amount ordered.


SQL> select avg(amount) as "Total Average Amount" from orders; Total Average Amount
--------------------
2666.537

SQL> select sum(amount) as "Total Amount of Orders" from orders; Total Amount of
Orders
----------------------
26665.37

========================================================
==========

Asst.Prof. Nilam suthar


Pgdca dept Page 18

You might also like