Você está na página 1de 5

CREATE TABLE customer(

cid integer PRIMARY KEY,


name varchar,
ctype varchar,
city varchar
);

CREATE TABLE orders(


oid integer PRIMARY KEY,
odate date,
cid integer REFERENCES customer(cid)
);
CREATE TABLE parts(
pid integer PRIMARY KEY,
name varchar,
colour varchar,
weight integer
);
CREATE TABLE orderline(
oid integer REFERENCES orders(oid),
pid integer REFERENCES parts(pid) ,
qty integer,
PRIMARY key (oid,pid)
);
INSERT INTO customer (cid,name,ctype,city)
VALUES (1,'Smith','wholesale','London');
INSERT INTO customer (cid,name,ctype,city)
VALUES (2,' Jones',' business',' Cardiff');
INSERT INTO customer (cid,name,ctype,city)
VALUES (3,' Lee',' individual',' Liverpool');
INSERT INTO customer (cid,name,ctype,city)
VALUES (4,'Tan',' business',' London');
INSERT INTO customer (cid,name,ctype,city)
VALUES (5,'Sly',' business',' Oxford');
INSERT INTO customer (cid,name,ctype,city)
VALUES (6,'Patel', 'individual',' Cardiff');

INSERT INTO orders VALUES (1,'03-NOV-1991',1);


INSERT INTO orders VALUES(2,'03-NOV-1991',2);
INSERT INTO orders VALUES (3,'04-DEC-1991',1);
INSERT INTO orders VALUES (4,'05-DEC-1991',5);
INSERT INTO orders VALUES (5,'05-DEC-1991',2);
INSERT INTO orders VALUES(6,'03-JAN-1992',3);
INSERT INTO orders VALUES (7,'05-FEB-1992',1);
INSERT INTO orders VALUES (8,'05-JAN-1992',4);
INSERT INTO orders VALUES (9,'11-FEB-1992',2);
INSERT INTO orders VALUES (10,'29-FEB-1992',1);
INSERT INTO orders VALUES (11,'01-APR-1992',4);
INSERT INTO parts VALUES (1,'widget','red',10);
INSERT INTO parts VALUES (2,'loofa','puce',20);
INSERT INTO parts VALUES(3,'pike','blue',30);
INSERT INTO parts VALUES (4,'thingie','green',25);
INSERT INTO parts VALUES(5,'whatsit','red',15);
INSERT INTO parts VALUES (6,'foobar','green',20);
INSERT INTO orderline VALUES ( 1,1,100);
INSERT INTO orderline VALUES (1,2,80);
INSERT INTO orderline VALUES (1,3,70);
INSERT INTO orderline VALUES (1,6,80);
INSERT INTO orderline VALUES (2,1,90);
INSERT INTO orderline VALUES (2,2,60);
INSERT INTO orderline VALUES (2,3,50);
INSERT INTO orderline VALUES (2,4,60);
INSERT INTO orderline VALUES (2,5,10);
INSERT INTO orderline VALUES (2,6,20);
INSERT INTO orderline VALUES (3,1,10);
INSERT INTO orderline VALUES (3,2,10);
INSERT INTO orderline VALUES (3,5,10);
INSERT INTO orderline VALUES (4,3,10);
INSERT INTO orderline VALUES (4,1,40);
INSERT INTO orderline VALUES (5,1,50);
INSERT INTO orderline VALUES (5,2,80);
INSERT INTO orderline VALUES (5,3,45);
INSERT INTO orderline VALUES (5,4,60);
INSERT INTO orderline VALUES (5,5,70);
INSERT INTO orderline VALUES (5,6,60);
INSERT INTO orderline VALUES (6,5,10);
INSERT INTO orderline VALUES (7,4,20);
INSERT INTO orderline VALUES (7,6,30);
INSERT INTO orderline VALUES (8,3,25);
INSERT INTO orderline VALUES (9,5,10);
INSERT INTO orderline VALUES (10,2,20);
INSERT INTO orderline VALUES (10,4,20);
INSERT INTO orderline VALUES (10,5,50);
INSERT INTO orderline VALUES (10,6,20);
INSERT INTO orderline VALUES (11,1,20);
INSERT INTO orderline VALUES (11,2,10);
INSERT INTO orderline VALUES (11,4,50);
INSERT INTO orderline VALUES (11,6,25);
/*1*/
SELECT name FROM parts
WHERE colour='red';
/*2*/
SELECT name,ctype
FROM customer
WHERE city!='London';
/*3*/
SELECT city
FROM customer
WHERE (ctype='business') or (ctype='wholesale');
/*4*/
SELECT orders.odate
FROM orders,customer
WHERE (customer1.cid) and (customer1.cid=orders.cid);
/*5*/
SELECT p.name
FROM parts as p, orders as o,orderline as ol
WHERE (o.cid=1) and (o.oid=ol.oid) and (ol.pid=p.pid);
/*6*/
SELECT DISTINCT p.name
FROM parts as p, orders as o, orderline as ol
WHERE ((o.cid=1) and (o.oid=ol.oid) and (ol.pid=p.pid)) or (p.colour='blue');
/*7*/
SELECT p.name
FROM parts as p,orders as o,orderline as ol
WHERE (o.cid=1) and (o.oid=ol.oid) and (ol.pid=p.pid) and (p.colour="blue");
/*8*/
SELECT c.name
FROM customer as c,orders as o
WHERE (o.date='29-FEB-1992') and (o.cid=c.cid);
/*9*/
SELECT DISTINCT c.name
FROM customer as c,parts as p,orderline as ol, orders as o
WHERE ((p.pid=2) or (p.pid=4)) and ((p.pid=ol.pid) and (ol.oid=o.oid) and (o.cid
=c.cid));
/*10*/
SELECT p.colour
FROM parts as p,orders as o,orderline as ol
WHERE (o.odate='29-FEB-1992') and (o.oid=ol.oid) and (ol.pid=p.pid);
/*11*/
SELECT DISTINCT c1.name
FROM customer as c1,customer as c2
WHERE (c1.ctype='individual') and (c2.ctype='business') and c1.city not in
(
SELECT city
FROM customer
WHERE ctype='business');
/*12*/
SELECT DISTINCT p.name
FROM parts as p,orders as o,customer as c
WHERE o.cid=c.cid;
/*13*/
SELECT DISTINCT p.name
FROM parts as p,orders as o,customer as c,orderline as ol
WHERE o.cid in
(
SELECT c.cid
FROM customer
WHERE ctype='business' AND p.pid in
(
SELECT pid
FROM orderline ) and ol.oid=o.oid);
/*14*/
SELECT DISTINCT CUSTOMER.CID, CUSTOMER.NAME
FROM CUSTOMER
WHERE (((CUSTOMER.CID) Not In (SELECT ORDERS.CID FROM ORDERS)));
/*15*/
SELECT DISTINCT C.NAME, P.NAME
FROM CUSTOMER AS C, PARTS AS P, ORDERS AS O, ORDERLINE AS OL
WHERE C.CID=O.CID AND O.OID=OL.OID AND OL.PID=P.PID
ORDER BY C.NAME;
/*16*/
SELECT DISTINCT CITY
FROM CUSTOMER
GROUP BY CITY
HAVING COUNT(CITY)=1;
/*17*/
SELECT DISTINCT CUSTOMER.CITY
FROM CUSTOMER
GROUP BY CUSTOMER.CITY
HAVING (((Count(CUSTOMER.CTYPE))>0));
/*18*/
SELECT DISTINCT C.NAME, P.NAME
FROM CUSTOMER AS C, PARTS AS P, ORDERS AS O, ORDERLINE AS OL
WHERE C.CID=O.CID AND O.OID=OL.OID AND OL.PID=P.PID AND OL.QTY>10;
/*19*/
SELECT DISTINCT C.NAME
FROM ORDERLINE AS OL, PARTS AS P, CUSTOMER AS C, ORDERS AS O
WHERE C.CTYPE='business' AND C.CID=O.CID AND O.OID=OL.OID AND OL.PID NOT IN (SEL
ECT PID FROM PARTS);

/*20*/
SELECT DISTINCT C.NAME
FROM CUSTOMER AS C
WHERE C.CITY IN (SELECT C.CITY FROM CUSTOMER AS C, ORDERS AS O, ORDERLINE AS OL,
PARTS AS P WHERE C.CID=O.CID AND O.OID=OL.OID AND OL.PID=P.PID AND P.PID=2);
/*21*/
SELECT DISTINCT C.NAME
FROM CUSTOMER AS C
WHERE C.CITY IN
(SELECT C.CITY
FROM CUSTOMER AS C, ORDERS AS O, ORDERLINE AS OL, PARTS AS P
WHERE C.CID=O.CID AND O.OID=OL.OID AND OL.PID=P.PID AND P.PID=2);
/*22*/
SELECT DISTINCT O.CID, COUNT(O.OID) AS NUMBER_OF_ORDERS_MADE, COUNT(OL.PID) AS N
UMBER_OF_DIFFERENT_PARTS_ORDERED
FROM ORDERS AS O, ORDERLINE AS OL
GROUP BY O.CID,O.OID,OL.PID
HAVING O.OID=OL.PID;
/*23*/
SELECT DISTINCT P.PID, COUNT(O.CID)
FROM PARTS AS P, ORDERLINE AS OL, ORDERS AS O
GROUP BY P.PID,OL.PID,OL.OID,O.OID
HAVING P.PID=OL.PID AND OL.OID=O.OID;

/*24*/
SELECT name FROM customer
WHERE
SELECT DISTINCT C.NAME,
FROM CUSTOMER AS C,ORDERS AS O
WHERE C.CID='2' AND O.CID=C.CID
(
SELECT

Você também pode gostar