Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Table

CREATE TABLE Salespeople


(
SNUM NUMBER( 10) PRIMARY KEY ,
SNAME VARCHAR2(40)NOT NULL
CITY CHAR (40,
COMM NUMBER(10,2)
);

CREATE TABLE Customers


(
CNUM INTEGER PRIMARY KEY,
CNAME CHAR(20,) NOT NULL,
CITY CHAR(20),
rating INTEGER,
SNUM integer REFERENCES SALESPEOPLE
);

CREATE TABLE Orders


(
ONUM INTEGER PRIMARY KEY,
AMT DECIMAL,
ODATE DATE NOT NULL,
CNUM INTEGER NOT NULL,
SNUM INTEGER NOT NULL,
FOREIGN KEY (SNUM) REFERENCES SALEPEOPLE,
FOREIGN KEY (CNUM) REFERENCES CUSTOMERS
);

INSERT INTO Salespeople VALUES (1001,'Peel','London',.12);


INSERT INTO Salespeople VALUES (1002,'Serres','San Jose',.13);
INSERT INTO Salespeople VALUES (1004,'Monika','London',.11);
INSERT INTO Salespeople VALUES (1007,'Rifkin','Barcelona',.15);
INSERT INTO Salespeople VALUES (1003,'Axelrod','New York',.10);
INSERT INTO Salespeople VALUES (1002,'Fans','London',.26);

INSERT INTO Customers VALUES (2001,' Hoffman ','London',100,1001);


INSERT INTO Customers VALUES (2002,'Giovanni','Rome',200,1003);
INSERT INTO Customers VALUES (2003,'Liu','San Jose',200,1002);
INSERT INTO Customers VALUES (2004,'Grass','Berlin',300,1002);
INSERT INTO Customers VALUES (2006,'Clemens','London',100,1001);
INSERT INTO Customers VALUES (2008,'Cisneros','San Jose',300,1007);
INSERT INTO Customers VALUES (2007,'Pereira','Rome',100,1004);
INSERT INTO orders VALUES (3001,18.69,'03/oct/96 12:30',2008,1007);
INSERT INTO orders VALUES (3003,767.19,'03/oct/9613:45',2001,1001);
INSERT INTO orders VALUES (3002,1900.10,'03/oct/96 12:05 ',2007,1004);
INSERT INTO orders VALUES (3005,5160.45,'03/oct/96 14:00',2003,1002);
INSERT INTO orders VALUES (3006,1098.16,'03/oct/96 13:37',2008,1007);
INSERT INTO orders VALUES (3009,1713.23,'04/oct/96 15:21',2002,1003);
INSERT INTO orders VALUES (3007,75.75,'04/oct/96 15:21',2002,1003);
INSERT INTO orders VALUES (3008,4723.00,'05/oct/96 12:07',2006,2001);
INSERT INTO orders VALUES (3010,1309.95,'06/oct/96 13:12',2004,1002);
INSERT INTO orders VALUES (3011,9891.88,'06/oct/96 13:09',2006,1001);
INSERT INTO orders VALUES (3012,3455.78,'04/oct/96 15:21',2002,1003);
INSERT INTO orders VALUES (3013,1245.98,'04/oct/96 16:32',2002,1001);
INSERT INTO orders VALUES (3014,3721.53,'05/oct/96 12:45',2006,2001);
INSERT INTO orders VALUES (3015,734.50,'06/oct/96 13:16',2004,1002);
INSERT INTO orders VALUES (3016,1729.67,'06 /oct/96 13:07',2006, 1001 );

2) select cname from customers where cname between 'A' and 'G' order by cname asc;

3) select amt from orders 2 where amt > (select avg(amt) from orders where odate in(select odate from
orders group by odate having odate='04-OCT-96'));

4)select sum(amt) from orders group by odate order by sum(amt) desc;

5)Write a select command that produces the rating followed by the name of each customer in San Jose

select rating,cname from customers where city in(select city from salespeop le1 where city='San Jose');

6)Find all orders with amounts smaller than any amount for a customer in San Jo se

select onum,amt from orders


where amt <(select max(amt) from orders
3 where amt in(select orders.amt from customers,orders where customers.cnum=o
rders.cnum and customers.city='San Jose'));

7) Find all orders with above average amounts for their customers

select onum from orders where amt >(select avg(amt) from orders);

8)Write a query that calculates the amount of the salesperson s commission on ea ch order by a
customer with a rating above 100.00.

select sname,comm*amt from salespeople1,orders where snum in(select snum from customers where
rating > 100);

9)Count the customers with ratings above San Jose s average.


select count(city) from customers where rating >(select avg(rating) from c ustomers where city='San
Jose');
10)Write a query that produces all pairs of salespeople with themselves as wel l as duplicate rows with
the order reversed

select cnum,cname,rating from customers where rating in(select max(ratin from customers group by
city);

11) Find all salespeople that are located in either Barcelona or London.

select sname from salespeople1 where city='Barcelona' or city='London';

12) Find all salespeople with only one customer.

select snum, sname from salespeople1 where snum in (select snum from customers where cnum
in(select cnum from orders having count(cnum)=1 group by cnum));

13) Write a query that joins the Customer table to itself to find all pairs of customers served by a single
salesperson

select c1. cname,c2.cname from customers c,customers c2 where c.snum=c2.snum and c.cname!
=c2.cname;

14) Write a query that will give you all orders for more than $1000.00

select onum from orders where amt>1000;

15) Write a query that lists each order number followed by the name of the customer who made that
order.

select orders. onum, customers.cname from customers,orders where customers.cn um=orders. Cnum;

17) Write a query that selects all customers whose ratings are equal to or gre ater than ANY (in the SQL
sense) of Serres ?

select cname from customers where rating >= ANY (select rating from customers where snum =(select
snum from salespeople1 where sname='Serres'));

18) Write 2 queries that will produce all orders taken on October 3 or October 4
select onum from orders where odate='03-oct-96' or odate='04-oct-96';
19) remaning

20) .Find only those customers whose ratings are higher than every customer in Rome

select cname from customers where rating >(select max(rating) from customers where city='Rome');

21) remaining

22) Find all rows from the Customers table for which the salesperson number is 1001

select * from customers where snum=1001;

23) .Find the total amount in Orders for each salesperson for whom this total is g reater than the
amount of the largest order in the table.

select sum(amt),snum from orders having sum(amt)>max(amt) group by snum;

24) Write a query that selects all orders save those with zeroes or NULLs in the amount field.

select onum from orders where amt is NULL or amt=0;

25) remaining

26) List all Salespeople s names and the Commission they have earned.

select salespeople1.sname, sum(comm*amt*0.01) from salespeople1, orders where


salespeople.snum=orders. snum group by sname;

26) remaining

28) Find all salespeople for whom there are customers that follow them in alphabetical order.

select s. sname, c. cname from customers c, salespeople1 s where s. snum= c.snum group by
s.sname,c.cname having c.cname >= s.sname;

29) Write a query that produces the names and ratings of all customers of all who have above average
orders

select cname, rating from customers where rating > (select avg(rating) from customers);
30)
30) .Find the SUM of all purchases from the Orders table.

select sum(amt) from orders;

31) Write a SELECT command that produces the order number, amount and date for al l rows in the
order table.
select onum, amt, odate from orders;

32) .Count the number of non NULL rating fields in the Customers table

select count(rating) from customers;

33)remaining
34)remaining
35)remaining
36)remaining
37)remaining

38) Count the number of salespeople registering orders for each day (If a sales person has more than
one order on a given day, he or she should be counted only once.)

select count(distinct snum),odate from orders group by odate;

39) .Find all orders attributed to salespeople in London.

select onum from orders where snum in (select snum from salespeople where city='London');

40) remaining
Find all salespeople who have customers with more than one current order

select distinct snum from orders where cnum in


(select cnum from orders
group by cnum
having count(cnum)>1);

45)
select snum from customers
group by snum
having count(snum)>1;
43) begins with c
select cname from customers where cname like 'C%';
44)
select city,max(rating) from customers group by city;

45)

select distinct snum from orders;

46)
select rating,cname,cnum from customers order by rating desc;

47) select avg(comm*amt*0.01) from salespeople1,orders where city='London';

48) remaining

49)
select sname from salespeople where comm>=10 and comm<=12;

50) remaining
51) remaining

52)cust smallest order

select cnum,min(amt) from orders group by cnum;

53)

select cname from customers where cname like 'G%' and rownum=1;

54)

select count( distinct city) from customers;

55) avg amount

select avg(amt) from orders;

56)remaining
57) remaining
58)
select cname from customers where city!='San Jose' and rating > 200;

You might also like