Vous êtes sur la page 1sur 4

CIS 631 RA/SQL HWK Dr. H.

Assadipour Queries:
1. Get the names of parts that cost less than 20.00. SQL> SELECT * 2 FROM part 3 WHERE price<20; PNO PNAME QOH PRICE PLEVEL ---------- ------------------------------ ---------- ---------- ---------10506 Land Before Time I 200 19.99 20 10507 Land Before Time II 156 19.99 20 10508 Land Before Time III 190 19.99 20 10509 Land Before Time IV 60 19.99 20 10701 When Harry Met Sally 120 19.99 30 10800 Dirty Harry 140 14.99 30 6 rows selected. RA:

Re sults pname ,etc. ( prics < 20 ( part ))


2. Get the pairs of customer number values of customers having the same zip code.

SQL> l 1 SELECT c1.cno, c2.cno 2 FROM customer c1, customer c2 3 WHERE c1.zip = c2.zip 4* AND c1.cno < c2.cno SQL> / CNO CNO ---------- ---------1111 2222 RA:

Re sults c1. zip =c 2. zip ( c1. zip =c 2. zip (customer _ c1


c1.cno < c 2.cno c1.cno < c 2.cno

customer _ c 2))

3. Get the names of customers who have ordered parts ONLY from employees living in Wichita. SQL> SELECT DISTINCT cname 2 FROM customer c, order o, employee e, zipcode z 3 WHERE c.cno = o.cno and o.eno = e.eno and e.zip = z.zip 4 AND z.city ='Wichita'; CNAME -------------------Barbara

Charles

RA:

R1 ename,eno ( city 'Wichita ' (employee _ e Re sults c.cname ( c.cno = o.ono customer _ c

e. zip = z . zip

zipcode _ z ))
o .eno = R1.eno

order _ o

R1)

4. Get the names of employees along with their total sales for the year 1995. To obtain the employee names and sales for all times and years: SQL> BREAK ON ename; SQL> SELECT ename, od.qty*p.price 2 FROM employee e, order o, odetail od, part p 3* WHERE e.eno=o.eno and o.ono=od.ono and od.pno=p.pno ENAME OD.QTY*P.PRICE --------------- -------------Jones 19.99 19.99 39.98 59.97 14.99 24.99 99.96 Smith 24.99 19.99 9 rows selected. To obtain the names of employees along with their total sales for 1995: SQL> SELECT ename , SUM(od.qty*price) 2 FROM employee e, order o, odetail od, part p 3 WHERE e.eno=o.eno and o.ono=od.ono and od.pno=p.pno, and to_char(received, YY)=95 4* GROUP BY ename SQL> / ENAME SUM(OD.QTY*PRICE) --------------- ----------------Jones 99.96 Smith 44.98 RA:

Re sults ename , sum ( qty* price ) ( YY ='95' (employee _ ee.eno =o.eno


o .ono = od .ono

order _ o)

o det ails _ od

od . pno = p . pno

part _ p ))

5. Get the names of customers who have placed the highest number of orders. The following query finds the count for customer orders: SQL> CREATE VIEW maxcounter as 1 SELECT cname, COUNT(*) as counts 2 FROM customer c, order o 3 WHERE c.cno=o.cno 4 GROUP BY cname;

SQL> View created. SQL> SELECT * FROM maxcounter; CNAME COUNT(*) -------------------- ---------Barbara 1 Bertram 1 Charles 2 SQL> SELECT cname, m.counts 2 FROM maxcounter m 3 WHERE m.counts = (SELECT MAX(m1.counts) 4 FROM maxcounter m1); CNAME COUNTS -------------------- ---------Charles 2

RA:

max counter (cno, no_of_orders) ((cno

COUNT ono) MAX no_of_orders)) customer _ c)

Re sults cname ( c.cno= mx.cno max counter _ mx

6. Get the names of parts that have been ordered the most (in terms of quantity ordered, not number of orders)

Once again, I have created a view of the total quantity for each part and then used the MAX function to obtain the maximum value from the totals. This can be combined into one query. SQL> CREATE VIEW maxqty as SELECT pname, sum(od.qty) as total FROM part p, order o, odetail od WHERE p.pno=od.pno and od.ono=o.ono GROUP BY pname; SQL> View created. SQL> SELECT * FROM maxqty;
PNAME TOTAL ------------------------------ ---------Dirty Harry 1 Dr. Zhivago 1 Land Before Time I 1 Land Before Time II 1 Land Before Time III 2 Land Before Time IV 3 Sleeping Beauty 5 When Harry Met Sally 1 8 rows selected.

SQL> SELECT pname, total 2 FROM maxqty 3 WHERE maxqty.total = (SELECT MAX(total) 4* FROM maxqty) PNAME TOTAL ------------------------------ ---------Sleeping Beauty 5

R1( pno, no _ of _ qty ) ( pno SUM (qty )) ( MAX no_of_orders)) Re sults cname ( p. pno =T 1. pno R1 part _ p )

Vous aimerez peut-être aussi