1.- Llista dels venedors (nom) amb la quantitat de clients representats per cada un d'ells, però només dels venedors que no es diguin Smith. Ordenat per nom de venedor. select nombre, count(distinct clie) from repventas, pedidos where rep = num_empl and nombre not like '%Smith%' group by rep,nombre order by nombre; nombre | count ---------------+------- Bill Adams | 2 Dan Roberts | 3 Larry Fitch | 3 Mary Jones | 1 Nancy Angelli | 2 Paul Cruz | 1 Sam Clark | 2 Tom Snyder | 1 (8 rows) 2.- Llista de les comandes que s'han fet cada dia ordenades per quantitat (número de productes) de major a menor. Només volem veure els dies en que ha hagut una venta superior a 20.000 $. select fecha_pedido, count(*) as num_condades, sum (cant) as quantitat from pedidos group by fecha_pedido having sum(importe)> 20000 order by quantitat desc; fecha_pedido | num_condades | quantitat --------------+--------------+----------- 1989-12-17 | 2 | 35 1990-03-02 | 1 | 22 1990-02-02 | 2 | 15 1989-12-31 | 1 | 11 1990-01-30 | 1 | 9 (5 rows) 3.- Els directors d'oficines volen enviar felicitacions de Nadal a tots els clients (siguin seus o no) que hagin fet alguna compra abans de finalitzar l'any 1989 ('12-31-89'). Cal un llistat de tots els possibles (i diferents) parells director/client. Ordenat per directors i, dintre de cada director, per client. select distinct nombre, empresa from repventas, clientes, pedidos where num_empl in (select dir from oficinas) and num_clie = clie and fecha_pedido <= '12-31-89' order by nombre,empresa; nombre | empresa -------------+------------------ Bill Adams | Acme Mfg. Bill Adams | First Corp. Bill Adams | Fred Lewis Corp. Bill Adams | JCP Inc. Bill Adams | J.P. Sinclair Bill Adams | Midwest Systems Bill Adams | Orion Corp Bob Smith | Acme Mfg. Bob Smith | First Corp. Bob Smith | Fred Lewis Corp. Bob Smith | JCP Inc. Bob Smith | J.P. Sinclair Bob Smith | Midwest Systems Bob Smith | Orion Corp Larry Fitch | Acme Mfg. Larry Fitch | First Corp. Larry Fitch | Fred Lewis Corp. Larry Fitch | JCP Inc. Larry Fitch | J.P. Sinclair Larry Fitch | Midwest Systems Larry Fitch | Orion Corp Sam Clark | Acme Mfg. Sam Clark | First Corp. Sam Clark | Fred Lewis Corp. Sam Clark | JCP Inc. Sam Clark | J.P. Sinclair Sam Clark | Midwest Systems Sam Clark | Orion Corp (28 rows) 4.- Llista dels clients que tenen alguna comanda amb un valor superior a 5000$. Volem informació del nom de l'empresa del client i ordenades per límit de crèdit de menor a major. select empresa,limite_credito from clientes where exists (select * from pedidos where clie=num_clie and importe > 5000) order by limite_credito asc; empresa | limite_credito -------------------+---------------- Orion Corp | 20000.00 Ian & Schmidt | 20000.00 Chen Associates | 25000.00 Ace International | 35000.00 J.P. Sinclair | 35000.00 Acme Mfg. | 50000.00 Zetacorp | 50000.00 Holm & Landis | 55000.00 (8 rows) 5.- LLista els productes dels que la quantitat de totes les seves comandes no superin el 30% de les existencies d'aquell producte. select id_fab,id_producto,descripcion from productos where (existencias *.3) >= ALL (select cant from pedidos where id_fab = fab and id_producto = producto); id_fab | id_producto | descripcion --------+-------------+------------------- rei | 2a45c | V Stago Trinquete bic | 41672 | Plate aci | 41003 | Articulo Tipo 3 aci | 41004 | Articulo Tipo 4 imm | 887p | Perno Riostra qsa | xk48 | Reductor fea | 112 | Cubierta imm | 887h | Soporte Riostra bic | 41089 | Retn aci | 41001 | Articulo Tipo 1 qsa | xk48a | Reductor imm | 773c | Riostra 1/2-Tm imm | 887x | Retenedor Riostra (13 rows) Solució amb EXISTS : (Redefinim l'enunciat) Llista els productes pels que no existeixi cap comanda amb una quantitat superior al 30% de la seva existència. select id_fab,id_producto,descripcion from productos where not exists ( select * from pedidos where id_fab = fab and id_producto = producto and (existencias*.3) < cant); 6.- Quantitat de productes demanats per cada venedor, però només dels venedors que tinguin una quantitat de productes demanats superior a la mitja de la quantitat de productes demanats en totes les comandes. Ordenat per noms de venedors. select nombre,rep,sum(cant) from pedidos,repventas where num_empl = rep group by nombre,rep having sum(cant) > (select avg(cant) from pedidos) order by nombre; nombre | rep | sum ---------------+-----+----- Bill Adams | 105 | 134 Dan Roberts | 101 | 45 Larry Fitch | 108 | 50 Mary Jones | 109 | 13 Nancy Angelli | 107 | 33 Paul Cruz | 103 | 30 Sam Clark | 106 | 13 Sue Smith | 102 | 38 Tom Snyder | 110 | 17 (9 rows)