15
Pràctiques de SQL Base de Dades – Grau en Informàtica MARC CAMACHO CATEURA u1910611 21 de novembre de 2011

Exercicis SQL

Embed Size (px)

Citation preview

Page 1: Exercicis SQL

Pràctiques de SQL

Base de Dades – Grau en Informàtica

MARC CAMACHO CATEURA u1910611

21 de novembre de 2011

Page 2: Exercicis SQL

1

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

Pràctiques de SQL Base de Dades – Grau en Informàtica

1 . T E O R I A

T I P U S D E D A D E S DECLARACIÓ VALOR QUE POT PRENDRE LONGITUD Màx.

VARCHAR2(Size) Qualsevol valor alfanumèric. 2000 caràcters

CHAR (Size) Qualsevol valor alfanumèric. 255 caràcters

LONG Qualsevol valor alfanumèric. 2 gigabytes

NUMBER(P,S) Qualsevol valor numèric, on P és el total de dígits i S el nombre de decimals.

22 bytes

DATE (YYYY/mm/dd) Qualsevol data. SYSDATE (dóna l’actual). 7 bytes

RAW (Size) Admeten dades de longitud variable. 255 bytes

LONG RAW Admeten dades de longitud variable. 2 gigabytes

T I P U S D E R E S T R I C C I O N S DECLARACIÓ DESCRIPCIÓ

“PRIMARY KEY” Clau primària

“NOT NULL” No pot obtenir un valor nul.

“DEFAULT” Valors per defecte.

“UNIQUE” Valors únics.

“FOREIGN KEY” Claus foranes.

C R E A C I Ó D E T A U L E S . Per crear una taula utilitzem la següent crida:

CREATE TABLE <nom_taula> (nom_columna1 tipus_dades_1 restricció, nom_columna2 tipus_dades_2 restricció, );

M O D I F I C A C I Ó D E T A U L E S .

• Afegir una nova columna:

ALTER TABLE nom_taula ADD (definició columna);

Page 3: Exercicis SQL

2

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

• Modificar el tipus de dades d’una columna:

ALTER TABLE nom_taula MODIFY (definició columna);

• Afegir restriccions:

ALTER TABLE nom_taula ADD CONSTRAINT nom_constraint TIPUS_RESTRICCIÓ (nom_columna);

En el cas que sigui una restricció de clau forana, caldrà indicar-li de quina taula procedeix amb un: REFERENCES nom_taula(nom_columna);

E L I M I N A R T A U L E S . Per eliminar una taula farem servir la comanda:

DROP TABLE nom_taula; Un cop eliminada la taula amb DROP TABLE s’elimina la seva definició i les seves dades per tant no podem recuperar-ne les dades.

I N S E R I R R E G I S T R E S . Per inserir un registre farem servir una de les següents comandes:

Si hem d’introduir valors nous.

INSERT INTO nom_taula [(columna1, columna2..)] VALUES (Valor1, Valor2...);

Si hem de buscar els valors en altres llocs.

INSERT INTO nom_taula [(columna1, columna2..)] <select_per_obtenir_dades>;

A C T U A L I T Z A R R E G I S T R E S . Per actualitzar un registre farem servir la comanda:

UPDATE nom_taula SET columna1=valor1, columna2=valor2... WHERE condició

E S B O R R A R R E G I S T R E S . La comanda DELETE és la comanda que s’utilitza per esborrar files d’una taula. La sintaxi és:

DELETE FROM nom_taula WHERE condició;

T R A C T A M E N T D E V I S T E S

Page 4: Exercicis SQL

1

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

C O N S U L T A R D A D E S A M B S Q L DESC nom_taula; à Obtenim el llistat d’atributs de l’entitat.

Per seleccionar totes les columnes de la taula cal fer un:

SELECT * from nom_taula; Per seleccionar una columna en concret cal fer un:

SELECT nom_columna1, nom_columna2.. from nom_taula; Si volem afegir condicions cal afegir un WHERE. Els criteris de cerca són pràcticament il·limitats i podem fer servir tan operadors lògics, de comparació (aritmètics, lògics i de caràcters), per dates… Els podrem posar entre condicions o dintre la mateixa condició. L’usarem de la següent manera:

WHERE condició1 op.lògic condició2...; Si volem ordenar farem cal fer servir el següent amb condició ASC o DESC segons si volem que ens ordeni de manera ascendent o descendent:

ORDER BY nom_columna1, nom_columna2.. tipus_ordenació; Si obtenim noms de columnes massa llargs es poden canviar creant un àlies. Per fer això l’únic que faríem seria després de seleccionar la columna indicar-li, entre cometes, l’àlies que volem.

A més, podem fer servir la clàusula DISTINCT per eliminar files duplicades del conjunt.

SELECT DISTINCT nom_columna from nom_taula; Podem tenir conjunts que s’agrupen segons els valors que s’indiquen al GROUP BY. La clàusula HAVING s’utilitza per controlar quin dels conjunts es visualitza i sobre quin operar. Tots els camps que apareixen al SELECT apareixen al GROUP BY. Un exemple de selecció de conjunts seria:

SELECT nom_columna1 “àlies1”, columna2, count(*) from nom_taula WHERE nom_taula condició1 GROUP BY nom_columna1, nom_columna2 ORDER BY nom_columna1;

Q U È P O D E M U S A R ?

Page 5: Exercicis SQL

2

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

J O I N S Els JOINS són una manera de treballar amb combinacions de taules. Hi ha dues maneres de fer servir un join:

• Sense restricció. Llavors estem parlant del producte cartesià dels camps d’ambdues taules. Això vol dir que ens apareix, en una sola taula tots els elements, de les columnes expressades en el SELECT. Així doncs la sintaxi seria la següent:

SELECT columna1, columna2.. FROM taula1, taula2;

• Amb restricció. Llavors el que afegim són diverses condicions dintre el WHERE que fan que no apareixin totes les dades de les columnes expressades en el SELECT. Així doncs la sintaxi seria la següent.

SELECT columna1, columna2.. FROM taula1, taula 2 WHERE condició1 op.lògic condició2;

S U B C O N S U L T E S Quan volem relacions el valor d’una columna amb una constant o un grup de constants que desconeixem utilitzem les subconsultes.

Una subconsulta és un SELECT entre parèntesis que s’executa primera i, posteriorment, el valor s’introdueix a la consulta principal. El valor de comparació pot ser un valor simple o un conjunt de valors. El segon SELECT és una subconsulta anidada.

• Quan vulguem que una subconsulta retorni UN sol valor, farem servir la següent estructura:

SELECT nom_columna1, nom_columna2.. FROM taula1, taula2.. WHERE nom_columnaX op.aritmètic(>,<,=,>=,<=..) ( SELECT nom_columna1, nom_columna2.. FROM taula1, taula2.. WHERE condició1);

• Quan vulguem que ens retorni una llista de valors, utilitzarem l’operador IN. Així doncs l’estructura quedarà de la següent manera:

Page 6: Exercicis SQL

3

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

SELECT nom_columna1, nom_columna2.. FROM taula1, taula2.. WHERE nom_columnaX IN (

SELECT nom_columna1, nom_columna2.. FROM taula1, taula2.. WHERE condició1);

2 . E X E R C I C I S

M O D E L E N T I T A T / R E L A C I Ó

M O D E L R E L A C I O N A L

Page 7: Exercicis SQL

4

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

1 . Q u i n s c a m p s t é l a t a u l a L L O G U E R ? desc LLOGUER

2 . O b t e n i r t o t e s l e s d a d e s d e l a t a u l a M O D E L . select * from MODEL;

3 . S e l e c c i o n e u l a m a t r í c u l a , e l c o m b u s t i b l e i e l c o l o r d e t o t e s e l s v e h i c l e s .

select VEHICLE_MATRICULA “Matricula”, VEHICLE_COMBUSTIBLE “Combustible”, VEHICLE_COLOR “Color” from VEHICLE;

4 . S e l e c c i o n e u l ’ e m a i l d e l s c l i e n t s q u e t e n e n e l c o m p t e a h o t m a i l o a y a h o o .

select CLIENT_EMAIL “Email” from CLIENT where CLIENT_EMAIL like ‘%hotmail%’ or CLIENT_EMAIL like ‘%yahoo%’;

5 . S e p a r e u e l N I F d e l s c l i e n t s e n d u e s p a r t s : l a n ú m e r o i l a l l e t r a .

select substr(CLIENT_DNI,0,8)) “DNI”, substr(CLIENT_DNI,9,9) “LLETRA” from CLIENT;

6 . N o m i s o u d e l s v e n e d o r s q u e g u a n y e n e n t r e 8 0 0 i 1 0 0 0€ . select VENEDOR_NOM “Nom”, VENEDOR_SOU “Sou” from VENEDOR where VENEDOR_SOU>799 and VENEDOR_SOU<1001;

7 . D o n e u l a d a t a d e c o m p r a i e l v a l o r d e l ’ a s s e g u r a n ç a d e l s v e h i c l e s d e g a s o l i n a . O r d e n e u - h o s e g o n s l a d a t a .

select VEHICLE_DATA_COMPRA “Data de compra”, VEHICLE_ASSEGURANCA “Preu de l’assegurança” from VEHICLE where VEHICLE_COMBUSTIBLE=’Gasolina’ order by VEHICLE_DATA_COMPRA;

8 . N o m i e m a i l d e l s c l i e n t s q u e t e n e n c o r r e u e n u n d o m i n i . c a t .

select CLIENT_NOM “Nom”, CLIENT_EMAIL “Email” from CLIENT where CLIENT_EMAIL like ‘%.cat%’;

9 . D i g u e u e l n o m i l a p a g a e x t r a ( u n 5 0 % d ’ a u g m e n t ) d e N a d a l d e l s v e n e d o r s v e t e r a n s , q u e v a n e n t r a r a b a n s d e l ’ a n y 2 0 0 0 . F e u s e r v i r t o _ d a t e ( ' 0 1 / 0 1 / 2 0 0 0 ' , ' d d / m m / Y Y Y Y ' ) p e r c o n v e r t i r a d a t a .

select VENEDOR_NOM, VENEDOR_SOU*1.5 “Paga extra” from VENEDOR where VENEDOR_ALTA>=to_date(‘01/01/2000’);

1 0 . O b t e n i u e l n o m i l ’ a n t i g u i t a t e n a n y s d e c a d a v e n e d o r . select VENEDOR_NOM “Nom”, to_char(SYSDATE, ‘dd/mm/YYYY’)-TO_CHAR(VENEDOR_ALTA, ‘dd/mm/YYYY’) “Antiguitat” from venedor;

1 1 . O b t e n i u e l n o m i e l s o u d e l s v e n e d o r s . P e l s v e n e d o r s d e l a d e l e g a c i ó 4 p e r r a o n s d e s e g u r e t a t o c u l t e u - n e e l s o u i p o s e u - h i a s t e r i s c s .

Page 8: Exercicis SQL

5

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

select VENEDOR_NOM “Nom”, VENEDOR_SOU “Sou” from VENEDOR where VENEDOR_CODI_DELEGACIO=4, to_char(VENEDOR_SOU,’****’);

1 2 . D e l a t a u l a d e l l o g u e r s , o b t e n i u - n e e l c o d i , e l m e s , e l n o m b r e d e d i e s d e l l o g u e r i l a m i t j a n a d i à r i a d e k m p e l s l l o g u e r s f e t s a p a r t i r d e l j u n y d e 2 0 0 7 . O r d e n e u - h o s e g o n s l a d a t a d ’ i n i c i . F o r m a t s p e r t o _ c h a r : ‘ M O N T H ’ , ‘ Y Y ’ , ‘ m m ’ ;

select LLOGUER_CODI “Codi”, to_char(LLOGUER_DATAF,’mm’) “Mes”, to_number(LLOGUER_DATAF-LLOGUER_DATAI) “Dies”, floor((LLOGUER_KMF-LLOGUER_KMI)/to_number(LLOGUER_DATAF-LLOGUER_DATAI)) “KM/Dia” from LLOGUER where LLOGUER_DATAI>=’01/06/07’ order by LLOGUER_DATAI;

1 3 . D o n e u l a p a g a e x t r a q u e t e n e n e l s v e n e d o r s : e l s o u m é s 1 5 c è n t i m s d ’ e u r o p e r k m q u e h a f e t , o r d e n a t s e g o n s l a p a g a .

select VENEDOR_NOM “Nom venedor”, VENEDOR_SOU+(0.15*VENEDOR_KM) “Paga” from VENEDOR order by “Paga”;

1 4 . E l c o r r e u e l e c t r ò n i c d e l s v e n e d o r s e s c o n s t r u e i x a i x í : a m b e l n o m m é s u n p r e f i x q u e d e p è n d e l a d e l e g a c i ó ( 1 é s B B , 2 é s B A , 3 é s A C , 4 é s D A ) m é s e l d o m i n i , l l o g a c o t x e s . c a t . C o n s t r u ï u p e r c a d a v e n e d o r e l s e u c o r r e u .

select decode(VENEDOR_DELEGACIO_CODI, ‘1’, VENEDOR_NOM || ‘BB’ || ‘@llogacotxes.cat’, ‘2’, VENEDOR_NOM || ‘BA’ || ‘@llogacotxes.cat’, ‘3’, VENEDOR_NOM || ‘AC’ || ‘@llogacotxes.cat’, ‘4’, VENEDOR_NOM || ‘DA’ || ‘@llogacotxes.cat’) “Emails” from venedor;

1 5 . O b t e n i r e l p r e u m i g d e l ’ a s s e g u r a n ç a d e l s v e h i c l e s s e g o n s e l s e u c o l o r , p e l s v e h i c l e s c o m p r a t s a p a r t i r d e l 2 0 0 7 .

select VEHICLE_COLOR “Color”, avg(VEHICLE_ASSEGURANCA) from VEHICLE where to_char(VEHICLE_DATA_COMPRA,’YYYY’)>=’2007’ group by VEHICLE_COLOR;

1 6 . Q u a n t s l l o g u e r s , i d e q u a n t s d i e s d e m i t j a n a , h a f e t c a d a v e n e d o r ? L a m i t j a n a , a m b 2 d e c i m a l s d e p r e c i s i ó .

select LLOGUER_VENEDOR_CODI “Codi del venedor”, count(*) “Nombre”, round(to_number(avg(LLOGUER_DATAF-LLOGUER_DATAI)),2) “Dies” from LLOGUER group by LLOGUER_VENEDOR_CODI;

1 7 . O b t e n i r e l p r e u m i g d e l ’ a s s e g u r a n ç a d e l s v e h i c l e s s e g o n s e l s e u c o l o r , p e l s v e h i c l e s c o m p r a t s a p a r t i r d e l 2 0 0 7

select VEHICLE_COLOR “Color”, avg(VEHICLE_ASSEGURANCA) from VEHICLE where to_char(VEHICLE_DATA_COMPRA,’YYYY’)>=’2007’

Page 9: Exercicis SQL

6

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

group by VEHICLE_COLOR;

1 8 . Q u a n t s v e h i c l e s d e c a d a t i p u s d e c o m b u s t i b l e h e m c o m p r a t c a d a a n y ?

select VEHICLE_COMBUSTIBLE “Tipus de combustible”, to_char(VEHICLE_DATA_COMPRA, ‘YYYY’), count(*) “Quants” from VEHICLE group by VEHICLE_COMBUSTIBLE, to_char(VEHICLE_DATA_COMPRA, ‘YYYY’);

1 9 . P e l s v e h i c l e s q u e t e n e n m é s d ’ u n a c c e s s o r i , q u i n é s e l p r e u m à x i m d e l s a c c e s s o r i s q u e t e n e n ? I q u a n s u m e n t o t s , p e r c a d a v e h i c l e ?

select VA_VEHICLE_CODI “Codi Accessori”, max(COST) “Màxim”, sum(COST) “Suma” from VEHICLE_ACCESSORI group by VA_VEHICLE_CODI having count(*)>1;

2 0 . L l i s t a r t o t s e l s v e h i c l e s a m b m a t r í c u l a , d a t a d e c o m p r a i c o l o r .

select VEHICLE_MATRICULA “Matricula”, VEHICLE_DATA_COMPRA “Data de compra”, VEHICLE_COLOR “Color del Vehicle” from VEHICLE;

2 1 . C a l c u l a r l ' i m p o r t m i t j à d e l ' a s s e g u r a n ç a d e l s v e h i c l e s i l a s u m a t o t a l a p a g a r .

select round(avg(VEHICLE_ASSEGURANCA),2) “Mitjana cost assegurança”, round(sum(VEHICLE_ASSEGURANCA), 0) “ Suma total” from VEHICLE;

2 2 . C a l c u l a r e l n o m b r e d e v e n e d o r s q u e t e n i m a l ' e m p r e s a , q u i n é s e l s e u s o u m à x i m i q u i n e l m í n i m .

select count(VENEDOR_CODI) “Quants”, max(VENEDOR_SOU) “Sou màxim”, min(VENEDOR_SOU) “Sou mínim” from VENEDOR;

2 3 . L l i s t a r l e s d i f e r e n t s t e r m i n a c i o n s ( s e n s e r e p e t i c i o n s ) d e l e s l l e t r e s d e l e s m a t r i c u l e s d e l s v e h i c l e s .

select distinct(substr(VEHICLE_MATRICULA, 6,8)) from VEHICLE;

2 4 . S e l e c c i o n a r e l n ú m d e v e h i c l e s p e r c o l o r q u e s i g u i n D i e s e l , d e s c a r t a n t e l s c o l o r s a m b m e n y s d e 2 v e h i c l e s .

select VEHICLE_COLOR “Color”, count(*) “Número” from VEHICLE where VEHICLE_COMBUSTIBLE=’Diesel’ group by VEHICLE_COLOR having count(*)>=2;

2 5 . S e l e c c i o n a r l a m a t r i c u l a , e l c o l o r , e l c o m b u s t i b l e d e t o t s e l s v e h i c l e s D i e s e l c o m p r a t s d e s p r é s d e l 2 0 0 6 . O r d e n a r e l r e s u l t a t p e r m a t r í c u l a .

select VEHICLE_MATRICULA “Matr”, VEHICLE_COLOR “Color”, VEHICLE_COMBUSTIBLE “Combus” from VEHICLE where VEHICLE_COMBUSTIBLE=’Diesel’ and to_char(VEHICLE_DATA_COMPRA, ‘YYYY’)>=2006 order by VEHICLE_MATRICULA;

2 6 . B u s c a r e l n o m , p o b l a c i ó i D N I d e t o t s e l s c l i e n t s q u e a l s e u D N I h a g i ' 2 ' a q u a l s e v o l p o s i c i ó i c o m e n ç i p e r ' 4 '

Page 10: Exercicis SQL

7

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

select CLIENT_NOM “Nom”, CLIENT_POBLACIO “Població”, CLIENT_DNI “DNI” from CLIENT where CLIENT_DNI like ‘%2%’ and substr(CLIENT_DNI,1,1)=’4’;

2 7 . T r o b a r q u a n t s c l i e n t s t e n i m a c a d a p o b l a c i ó , o r d e n a r p e r p o b l a c i ó .

select CLIENT_POBLACIO “Poblacio”, count(*) from CLIENT group by CLIENT_POBLACIO order by CLIENT_POBLACIO;

2 8 . S i e l s c o t x e s s ' h a n d e c a n v i a r c a d a 3 a n y s , v o l e m s a b e r l a m a t r i c u l a i d a t a d e r e n o v a c i ó d e t o t s e l s v e h i c l e s .

select VEHICLE_MATRICULA “Matr”, add_Months(VEHICLE_DATA_COMPRA,36) from VEHICLE;

2 9 . T r o b a r e l p r e u m à x i m d e t a r i f a i e l p r e u m í n i m d e t a r i f a p e r a c a d a g r u p d e m o d e l .

select MODEL_GRUP “Grup”, max(MODEL_TARIFA_DIA) “Tarifa Màxima”, min(MODEL_TARIFA_DIA) “Tarifa mínima” from MODEL group by MODEL_GRUP;

3 0 . C a l c u l a r s o u q u e h e m d e p a g a r c a d a m e s a c a d a d e l e g a c i ó , o r d e n a t d e m e n y s a m é s s o u .

select VENEDOR_DELEGACIO_CODI, sum(VENEDOR_SOU) “Suma” from VENEDOR group by VENEDOR_DELEGACIO_CODI order by “Suma”;

3 1 . V o l e m s a b e r e l c o d i d e l l o g u e r , d i e s i k i l ò m e t r e s r e c o r r e g u t s d e l s l l o g u e r s d e m é s d e 1 0 d i e s o m é s d e 4 . 0 0 0 k i l ò m e t r e s .

select LLOGUER_CODI, to_number(LLOGUER_DATAF-LLOGUER_DATAI) “Dies”, (LLOGUER_KMF-LLOGUER_KMI) “Kilometres” from LLOGUER where to_number(LLOGUER_DATAF-LLOGUER_DATAI)>10 or (LLOGUER_KMF-LLOGUER_KMI)>4000 order by LLOGUER_CODI;

3 2 . Q u a n t s c o t x e s t e n í e m l l o g a t s e l d i a 1 5 / 0 5 / 2 0 0 7 p e r c a d a v e n e d o r ?

select “LLOGUER_VENEDOR_CODI”, count(*) from LLOGUER where LLOGUER_DATAF>=to_date(‘15/05/2007’) and LLOGUER_DATAI<=to_date(‘15/05/2007’) group by LLOGUER_VENEDOR_CODI order by count(*);

3 3 . N o m i a d r e ç a d e l s c l i e n t s d e G i r o n a , c a n v i a n t e l t e x t d e l ' a d r e ç a ' C / ' p e r ' C a r r e r ' .

select CLIENT_NOM “Client”, replace(CLIENT_ADRECA,’C/’,’Carrer’) “Adreça” from CLIENT where CLIENT_POBLACIO=’Girona’;

Page 11: Exercicis SQL

8

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

3 4 . C a l c u l a r l ' i m p o s t e c o l ò g i c p e r a c a d a c o t x e d e l 2 0 0 8 . 3 0€ a n u a l s i 5 0€ a s u m a r a l t o t a l s i é s B e n z i n a 1 0 0€ a s u m a r a l t o t a l s i é s D i e s e l .

select VEHICLE_CODI, decode(VEHICLE_COMBUSTIBLE, ‘Gasolina’, 80, ‘Diesel’, 130) “Impost” from VEHICLE;

3 5 . M o s t r a r e l n ú m e r o d e c o t x e s q u e h i h a d e c a d a c o l o r i e l p r e u m i g d e l a s e v a a s s e g u r a n ç a . S i d ' a l g u n c o l o r n o m é s h i h a u n v e h i c l e , n o m o s t r a r - l o .

select VEHICLE_COLOR, count(*), round(avg(VEHICLE_ASSEGURANCA),2) from VEHICLE group by VEHICLE_COLOR having count(*)>1

3 6 . L l i s t a r e l n ú m e r o d e c o t x e s c o m p r a t s c a d a a n y p e r t i p u s d e c o m b u s t i b l e . P e r c a d a a n y m o s t r a r l a d a t a m é s a l t a d e c o m p r a i l a m é s b a i x a .

select to_char(VEHICLE_DATA_COMPRA,’YYYY’) “Any”, VEHICLE_COMBUSITBLE “Combustible”,count(*) ”Número” , min(VEHICLE_DATA_COMPRA) “Data mínima”, max(VEHICLE_DATA_COMPRA) “Data màxima” from VEHICLE group by to_char(VEHICLE_DATA_COMPRA,’YYYY’),VEHICLE_COMBUSTIBLE order by to_char(VEHICLE_DATA_COMPRA,’YYYY’);

3 7 . Q u i n a é s l a m i t j a n a d e k i l ò m e t r e s d e l s l l o g u e r s ? Q u i n a é s l a m i t j a n a d e d i e s d e l s l l o g u e r s ?

select avg(LLOGUER_KMF-LLOGUER_KMI) “KM” from LLOGUER; select avg(to_number(LLOGUER_DATAF-LLOGUER_DATAI)) “Dies” from LLOGUER;

3 8 . M o s t r a r m a t r í c u l a , e l s a n y s q u e t e n e n i e l c o l o r d e l s c o t x e s a m b c a s t e l l à , d e t o t s e l s c o t x e s b l a n c s , b l a u s i n e g r e s .

select VEHICLE_MATRICULA “Matricula”, round(to_date(‘21/11/2011’)-VEHICLE_DATA_COMPRA/365,0) “Edat”, VEHICLE_COLOR, decode(VEHICLE_COLOR, ‘Blau’, ‘Azul’, ‘Blanc’, ‘Blanco’, ‘Negre’, ‘Negro’) “Color castellà” from VEHICLE where VEHICLE_COLOR=’Blanc’ or VEHICLE_COLOR=’Blau’ or VEHICLE_COLOR=’Negre’;

3 9 . M o s t r a r c o d i d e v e h i c l e i k i l ò m e t r e s q u e t e n i a e l v e h i c l e a l ’ ú l t i m l l o g u e r . N o m o s t r a r e l s v e h i c l e s q u e t e n e n m e n y s d e 2 5 . 0 0 0 k m . O r d e n a t p e r c o d i d e v e h i c l e .

select LLOGUER_VEHICLE_CODI “Codi vehicle”, max(LLOGUER_KMF) “Kilometres” from LLOGUER where LLOGUER_KMF>=25000 group by LLOGUER_VEHICLE_CODI order by LLOGUER_VEHICLE_CODI;

4 0 . S e l e c c i o n a e l n o m i e l c o d i d e l s v e n e d o r s q u e h a n l l o g a t v e h i c l e s d u r a n t e l m e s d e j u n y d e l 2 0 0 7 . E l l l o g u e r t a n t p o t c o m e n ç a r c o m a c a b a r a l m e s d e j u n y .

select VENEDOR_CODI, VENEDOR_NOM from VENEDOR, LLOGUER

Page 12: Exercicis SQL

9

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

where to_char(LLOGUER_DATAI,’MM/YY’)=’06/07’ or to_char(LLOGUER_DATAF,’MM/YY’)= ‘06/07’ and LLOGUER_VENEDOR_CODI=VENEDOR_CODI group by VENEDOR_CODI, VENEDOR_NOM order by VENEDOR_CODI;

4 1 . O b t e n i r u n l l i s t a t a m b e l n o m d e l m o d e l d e v e h i c l e i l a q u a n t i t a t d e v e h i c l e s q u e h i h a d e c a d a u n d e l s m o d e l s .

select MODEL_NOM, count(*) “Quants” from MODEL, VEHICLE where VEHICLE_MODEL_CODI=MODEL_CODI group by MODEL_NOM order by MODEL_NOM;

4 2 . L l i s t a e l s n o m s d e l s v e n e d o r s i d e l e s r e s p e c t i v e s p a r e l l e s , d e l s v e n e d o r s d e l a d e l e g a c i ó d e B l a n e s . O r d e n a e l r e s u l t a t a l f a b è t i c a m e n t p e l n o m d e l a p a r e l l a .

select VENEDOR_NOM, VENEDOR_PARELLA from VENEDOR, DELEGACIO where VENEDOR_DELEGACIO_CODI=DELEGACIO_CODI and DELEGACIO_NOM=’Blanes’ order by VENEDOR_PARELLA;

4 3 . Q u a n t s q u i l ò m e t r e s f a d e m i t j a n a u n v e h i c l e l l o g a t a u n v e n e d o r d e l a d e l e g a c i ó d ' O l o t .

select round(avg(LLOGUER_KMF-LLOGUER_KMI),1) “Mitjana” from LLOGUER, VENEDOR, DELEGACIO where LLOGUER_VENEDOR_CODI=VENEDOR_CODI and DELEGACIO_NOM=’Olot’ and VENEDOR_DELEGACIO_CODI=DELEGACIO_CODI;

4 4 . A p a r t i r d e l ’ e x e r c i c i a n t e r i o r , f e s u n l l i s t a t c o n j u n t p e r a c a d a u n a d e l e s d e l e g a c i o n s . O r d e n a ' l d e f o r m a d e s c e n d e n t p e l v a l o r d e l a m i t j a n a d e q u i l ò m e t r e s .

select DELEGACIO_NOM, round(avg(LLOGUER_KMF-LLOGUER_KMI),1) “Mitjana” from LLOGUER, VENEDOR, DELEGACIO where LLOGUER_VENEDOR_CODI=VENEDOR_CODI and VENEDOR_DELEGACIO_CODI=DELEGACIO_CODI group by DELEGACIO_NOM order by “Mitjana” desc;

4 5 . D e q u i n e s d e l e g a c i o n s s ó n i q u i n é s e l n o m d e l s v e n e d o r s a m b 3 o m é s o p e r a c i o n s d e l l o g u e r d e v e h i c l e s .

select DELEGACIO_NOM, VENEDOR_NOM from DELEGACIO, VENEDOR where VENEDOR_DELEGACIO_CODI=DELEGACIO_CODI and VENEDOR_CODI in (

select LLOGUER_VENEDOR_CODI from LLOGUER group by LLOGUER_VENEDOR_CODI having count(*)>=3)

order by DELEGACIO_NOM desc;

4 6 . O b t e n i r e l n o m d e l c l i e n t , e l s e u D N I i l a m a t r í c u l a d ' a l g u n d e l s v e h i c l e s , p e r a l s c l i e n t s q u e h a n r e a l i t z a t 3 l l o g u e r s o m é s . D i g u e s t a m b é e l m o d e l d e v e h i c l e q u e h a n l l o g a t .

Page 13: Exercicis SQL

10

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

select CLIENT_NOM, CLIENT_DNI, min(VEHICLE_MATRICULA) “Matr” from CLIENT, VEHICLE, LLOGUER where LLOGUER_CLIENT_CODI=CLIENT_NUM and LLOGUER_VEHICLE_CODI=VEHICLE_CODI and CLIENT_NUM in (

select LLOGUER_CLIENT_CODI from LLOGUER group by LLOGUER_CLIENT_CODI having count(*)>=3)

group by CLIENT_NOM, CLIENT_DNI order by CLIENT_DNI desc;

4 7 . O b t e n i r e l n o m i c o g n o m s d e l s c l i e n t s a m b u n c o g n o m q u e c o m e n c i p e r l a l l e t r a F .

select CLIENT_NOM “Nom del client”, MODEL_NOM “Nom del Model” from CLIENT, MODEL, VEHICLE, LLOGUER where CLIENT_NOM like ‘%F%’ and CLIENT_NUM=LLOGUER_CLIENT_CODI and LLOGUER_VEHICLE_CODI=VEHICLE_CODI and VEHICLE_MODEL_CODI=MODEL_CODI group by CLIENT_NOM, MODEL_NOM order by CLIENT_NOM;

4 8 . L l i s t a , p e r a c a d a m o d e l i v e h i c l e , e l s a c c e s s o r i s q u e i n c o r p o r e n . L e s d a d e s a l l i s t a r s ó n : n o m d e l m o d e l , m a t r í c u l a i c o l o r d e l v e h i c l e i l a d e s c r i p c i ó d e l ' a c c e s s o r i . O r d e n a e l l l i s t a t .

select MODEL_NOM, VEHICLE_MATRICULA “Matr”, VEHICLE_COLOR, ACCESSORI_DESCRIPCIO from MODEL, VEHICLE, ACCESSORI, VEHICLE_ACCESSORI where MODEL_CODI=VEHICLE_MODEL_CODI and VEHICLE_CODI=VA_ACCESSORI_CODI and VA_ACCESSORI_CODI=ACCESSORI_CODI group by MODEL_NOM, VEHICLE_MATRICULA, VEHICLE_COLOR, ACCESSORI_DESCRIPCIO order by MODEL_NOM, VEHICLE_MATRICULA;

4 9 . S e l e c c i o n a e l s d i f e r e n t s n o m s d ' a c c e s s o r i s d e l s v e h i c l e s l l o g a t s p e r a l g u n v e n e d o r .

select distinct ACCESSOR_DESCRIPCIO from MODEL, VEHICLE, ACCESSORI, VEHICLE_ACCESSORI

where MODEL_CODI=VEHICLE_MODEL_CODI and VA_ACCESSORI_CODI=ACCESSORI_CODI and VEHICLE_CODI=VA_VEHICLE_CODI;

5 0 . Q u a n t s v e n e d o r s h i h a a c a d a u n a d e l e s d e l e g a c i o n s . select DELEGACIO_NOM, count(*) “Quants” from DELEGACIO, VENEDOR where DELEGACIO_CODI=VENEDOR_DELEGACIO_CODI group by DELEGACIO_NOM;

5 1 . S u p o s a n t q u e u n c l i e n t p e r t a n y a u n a d e l e g a c i ó s i h i e f e c t u a u n l l o g u e r , q u a n t s c l i e n t s t é c a d a u n a d e l e s d e l e g a c i o n s .

select DELEGACIO_NOM, count(*) “Quants” from DELEGACIO, VENEDOR, CLIENT, LLOGUER where LLOGUER_CLIENT_CODI=CLIENT_NUM and LLOGUER_VENEDOR_CODI=VENEDOR_CODI and VENEDOR_DELEGACIO_CODI=DELEGACIO_CODI

Page 14: Exercicis SQL

11

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

group by DELEGACIO_NOM;

5 2 . Q u i n s v e n e d o r s t e n e n u n s o u q u e e s t à p e r s o t a d e l a m i t j a n a ? L l i s t a t d e n o m s .

select VENEDOR_NOM from VENEDOR where VENEDOR_SOU < (

select avg(VENEDOR_SOU) from VENEDOR);

5 3 . C o m e s d i u e l v e n e d o r a m b e l s o u m é s e l e v a t . D i g u e s t a m b é q u i n é s a q u e s t s o u .

select VENEDOR_NOM, VENEDOR_SOU from VENEDOR where VENEDOR_SOU= (

select max(VENEDOR_SOU) from VENEDOR);

5 4 . Q u i n é s e l n o m d e l a d e l e g a c i ó q u e h a r e a l i t z a t m é s l l o g u e r s .

select DELEGACIO_NOM from DELEGACIO, LLOGUER, VENEDOR where LLOGUER_VENEDOR_CODI=VENEDOR_CODI and VENEDOR_DELEGACIO_CODI=DELEGACIO_CODI group by DELEGACIO_NOM having count(*)=(

select max(count(*)) from DELEGACIO, VENEDOR, LLOGUER where DELEGACIO_CODI=LLOGUER_VENEDOR_CODI and VENEDOR_CODI=LLOGUER_VENEDOR_CODI group by DELEGACIO_NOM);

5 5 . Q u i n é s e l n o m d e l a d e l e g a c i ó q u e t é m é s v e n e d o r s a m b u n s o u i n f e r i o r a l a m i t j a n a g l o b a l .

select DELEGACIO_NOM from DELEGACIO, VENEDOR where DELEGACIO_CODI=( select VENEDOR_DELEGACIO_CODI from VENEDOR where VENEDOR_SOU<( select avg(VENEDOR_SOU) from VENEDOR) group by VENEDOR_DELEGACIO_CODI having count(*)=( select max(count(*)) from VENEDOR where VENEDOR_SOU<( select avg(VENEDOR_SOU) from VENEDOR) );

5 6 . L l i s t a e l c o d i d e l a d e l e g a c i ó a m b m é s v e n e d o r s . select VENEDOR_DELEGACIO_CODI from VENEDOR group by VENEDOR_DELEGACIO_CODI having count(*)=(

select max( count(*)) from VENEDOR group by VENEDOR_DELEGACIO_CODI);

5 7 . Q u i n e s s ó n l e s m a t r í c u l e s d e l s v e h i c l e s q u e i n c o r p o r e n e l m a j o r n o m b r e d ’ a c c e s s o r i s .

select VEHICLE_MATRICULA from VEHICLE, MODEL where MODEL_CODI=VEHICLE_MODEL_CODI and VEHICLE_CODI=(

select VA_VEHICLE_ACCESSORI from VEHICLE_ACCESSORI

Page 15: Exercicis SQL

12

Pràc

tique

s de

SQ

L |

21

de n

ovem

bre

de 2

011

group by VA_VEHICLE_ACCESSORI having count(*)( select max(count(*)) from VEHICLE_ACCESSORI group by VEHICLE_ACCESSORI)

);

5 8 . Q u i n é s e l n o m d e l m o d e l i l a m a t r í c u l a d e l s v e h i c l e s q u e i n c o r p o r e n e l m a j o r n o m b r e d’ a c c e s s o r i s .

select MODEL_NOM, VEHICLE_MATRIUCLA from VEHICLE_MODEL where MODEL_CODI=VEHICLE_MODEL_CODI and VEHICLE_CODI=( select VA_VEHICLE_ACCESSORI from VEHICLE_ACCESSORI group by VA_VEHICLE_ACCESSORI having count(*)=( select max(count(*)) from VEHICLE_ACCESSORI group by VEHICLE_ACCESSORI) );

5 9 . Q u i n s s ó n e l s n o m s d e l s v e n e d o r s i q u i n é s e l n o m d e l a s e v a d e l e g a c i ó , d e l s v e n e d o r s q u e h a n l l o g a t c o t x e s a m b ' A i r e c o n d i c i o n a t‘ .

select DELEGACIO_NOM, VENEDOR_NOM from DELEGACIO, VENEDOR where VENEDOR_DELEGACIO_CODI=DELEGACIO_CODI and VENEDOR_CODI in( select LLOGUER_VEHICLE_CODI from VEHICLE, LLOGUER where LLOGUER_VEHICLE_CODI=VEHICLE_CODI and VEHICLE_CODI in( select VA_ACCESSORI_DESCRIPCIO from VEHICLE_ACCESSORI where VA_ACCESSORI_CODI=( select ACCESSORI_CODI from ACCESSORI

where ACCESSORI_DESCRPCIO=’Aire condicionat’ )

) );