Está en la página 1de 13

Ejercicios de lgebra Relacional.

Dadas las siguientes tablas:


H( NH , Edad)
M( NM , Edad)
HG ( NM, NH)
es.

Hombre
Tabla de nombres y edades de Hombres
Mujer
Tabla de nombres y edades de Mujeres
HombreGusta Tablas con los nombres de mujeres y

hombr

Ej (Mara, Jos) a
Mara le gusta Jos.
MG ( NM, NH)
.
MA( NM, NH)

MujerGusta .

Ej (Juana, Mario) a Mario le gusta Juana

Matrimonios .

Obtener las siguientes tablas auxiliares:


Mujeres
Mujeres
Hombres
Hombres

Casadas:
Solteras
Casados:
Solteros

MC
MS
HC
HS

create table "H"("NH"character(30),"Edad" int)


insert
insert
insert
insert
insert
insert
insert
insert

into
into
into
into
into
into
into
into

"H"
"H"
"H"
"H"
"H"
"H"
"H"
"H"

("NH","Edad")
("NH","Edad")
("NH","Edad")
("NH","Edad")
("NH","Edad")
("NH","Edad")
("NH","Edad")
("NH","Edad")

values
values
values
values
values
values
values
values

('Mariano','32')
('Emanuel','28')
('Juan','30')
('Lautaro','30')
('Leandro','27')
('Julio','27')
('Franco','28')
('Gonzalo','32')

create table "M"("NM"character(30),"Edad" int)


insert
insert
insert
insert
insert
insert
insert
insert

into
into
into
into
into
into
into
into

"M"
"M"
"M"
"M"
"M"
"M"
"M"
"M"

("NM","Edad")
("NM","Edad")
("NM","Edad")
("NM","Edad")
("NM","Edad")
("NM","Edad")
("NM","Edad")
("NM","Edad")

values
values
values
values
values
values
values
values

('Merli','27')
('Jaque','28')
('Julia','30')
('Ana','30')
('Maria','28')
('Marcia','27')
('Lyla','32')
('Romina','32')

create table "HG"("NM"character(30),"NH"character (30))


insert
insert
insert
insert
insert

into
into
into
into
into

"HG"
"HG"
"HG"
"HG"
"HG"

("NM","NH")
("NM","NH")
("NM","NH")
("NM","NH")
("NM","NH")

values
values
values
values
values

('Merli','Mariano')
('Jaque','Emanuel')
('Julia','Juan')
('Ana','Lautaro')
('Maria','Leandro')

create table "MG"("NH"character(30),"NM"character (30))


insert into "MG" ("NH","NM") values ('Mariano','Merli')
insert into "MG" ("NH","NM") values ('Emanuel','Jaque')

insert into "MG" ("NH","NM") values ('Juan','Julia')


insert into "MG" ("NH","NM") values ('Gonzalo','Romina')
insert into "MG" ("NH","NM") values ('Franco','Lyla')
create table "MA"("NM"character(30),"NH"character (30))
insert
insert
insert
insert

into
into
into
into

"MA"
"MA"
"MA"
"MA"

("NM","NH")
("NM","NH")
("NM","NH")
("NM","NH")

values
values
values
values

('Merli','Mariano')
('Jaque','Emanuel')
('Julia','Juan')
('Ana','Lautaro')

Obtener las tablas auxiliares:


"MC"= mujeres casadas
select "NM" into "MC" from "M"
intersect
select "NM" from "MA"

"MS"= mujeres solteras


select "NM" into "MS" from "M"
except
select "NM" from "MA"
"HC"= hombres casados
select "NH" into "HC" from "H"
intersect
select "NH" from "MA"
"HS"= hombres solteros
select "NH" into "HS" from "H"
except
select "NH" from "MA"

1.

Todos los hombres y mujeres que se gustan mutuamente.


select "HG"."NM","HG"."NH" from "H"
inner join "MG"
on "MG"."NM"= "HG"."NM" and "MG" and "MG"."NH" = "MG"."NH"
NM
1 Merli
2 Jaque
3 Julia

NH
Mariano
Emanuel
Juan

2.

Matrimonios que se gustan mutuamente.


select * from "Ma"

3.

Matrimonios donde el hombre no le gusta a su mujer.

select * from "MA"


except
select * from "MG"
NM
1 Ana

4.

NH
Lautaro

Matrimonios donde ninguno gusta del otro.


Para que se cumpla esta consulta inserto un dato nuevo en la tabla "MA"
insert into "MA"("NM","NH") values ('Carla','Goku')
select * from "MA"
except
select * from "MG"
except
select * from "HG"
NM
1 Carla

5.

NH
Goku

Mujeres solteras a las que les gusta algn casado.

Insertar en la tabla "MG" el nombre de una "MS" ej Maria que gusta de un


casado ej Mariano
insert into "MG" ("NH","NM") values ('Mariano','Maria')
select "MS"."NM" from "MS"
inner join "MG"
on "MS"."NM" = "MG"."NM"
inner join "HC"
on "MG"."NH" = "HC"."NH"
NM
1 Maria

6.

Hombres casados a los que le gusta una mujer casada que no sea la suya
insert into "HG" ("NM","NH") values ('Julia','Mariano')
select "HC"."NH" from "HC"
inner join "HG"
on "HC"."NH" = "HG"."NH"
inner join "MA"
on "HG"."NM" = "MA"."NH"
ver de esta forma
select "HC"."NH" from "HC"
inner join "HG"
on "HC"."NH" = "HG"."NH"
inner join "MA"
on "HG"."NM" = "MA"."NM"
Mariano
Mariano
Lautaro
Emanuel
Juan

7.
e 30.

Mujeres casadas de ms de 40 aos a las que les gusta algn soltero de menos d
//inserto datos a la tabla "M":
insert into "M"("NM","Edad") values ('Celia','41')
//Agrego columna "MC" Edad
alter table "MC" add column "Edad" int
//inserto datos a la tabla "H":
insert into "H"("NH","Edad") values ('Coco','39')
//inserto a la tabla "MA":
insert into "MA"("NM","NH") values ('Celia','Coco')
//inserto en la tabla "MG":
insert into "MG"("NH","NM") values ('Leandro','Celia')
insert into "MG"("NH","NM") values ('Julio','Celia')
insert into "MG"("NH","NM") values ('Franco','Celia')
//asi da la subconsulta si preguntamos por todas

select * from "MG"


where exists (select *
from "MC"
where "NM" >= '40') //
// este es el resultado
select "NM","Edad" from "MC"
where exists (select "NH"
from "HS"
where "Edad" >= '40') ok
// resultado
celia 41

8.

Hombres casados a los que le gusta una mujer casada de menos de 20 aos.

//inserto datos en las tablas "M","H","MA","HG","HC","MC":

insert into "H"("NH","Edad") values ('Hugo','22')


insert into "M"("NM","Edad") values ('Macarena','19')
insert into "MA"("NM","NH") values ('Macarena','Hugo')
insert into "HG"("NM","NH") values ('Macarena','Lautaro')
insert into "HC"("NH") values ('Hugo')
insert into "MC"("NM") values ('Macarena')
insert into "MC"("NM","Edad") values ('Macarena','19')

select "NM","Edad" from "MC"


where exists (select "NH"
from "HC"
where "Edad"<'20')
resultado: Macarena 19
9.

Hombres casados a los que slo les gustan mujeres casadas.

select "HC"."NH" from "HC"


inner join "HG"
on "HC"."NH" = "HG"."NH"
inner join "MC"
on "HG"."NM" = "MC"."NM"

"Mariano
"Mariano
"Lautaro
"Lautaro
"Lautaro
"Emanuel
"Juan
insert into "HG"("NM","NH") values ('Maria','Mariano')
select DISTINCT "HC"."NH" from "HC"
inner join "HG"
on "HC"."NH" = "HG"."NH"
inner join "MC"
on "HG"."NM" = "MC"."NM"
WHERE "HC"."NH" NOT IN
(SELECT
"HC"."NH"
FROM
public."HC",
public."HG",
public."MS"
WHERE
"HC"."NH" = "HG"."NH" AND
"HG"."NM" = "MS"."NM");
OTRA FORMA
select DISTINCT "HC"."NH" from "HC"
inner join "HG"
on "HC"."NH" = "HG"."NH"
inner join "MC"
on "HG"."NM" = "MC"."NM"
EXCEPT
(SELECT
"HC"."NH"
FROM
public."HC",
public."HG",
public."MS"
WHERE
"HC"."NH" = "HG"."NH" AND
"HG"."NM" = "MS"."NM");

//tambien puede ser asi: pero tengo q agragar una columna a "HC" "Edad"
alter table "HC" add column "Edad" int
select * from "HC"
except
select * from "MC"
"Coco
"Mariano
"Emanuel
"Hugo
"Juan
"Lautaro
10.
40.

Mujeres solteras de menos de 30 a las que slo les gustan solteros de ms de

//agrego la columna "Edad" a "HS"


alter table "HS" add column "Edad" int

//cargo datos de "Edad" a "HS" y "MS" para hacer esta consulta


update "HS"
set "NH" = 'Leandro', "Edad" ='27'
where "NH" = 'Leandro'
//cargo datos de "H" y "HS" para hacer esta consulta
insert into "H"("NH","Edad") values ('Mario','41')
insert into "HS"("NH","Edad") values ('Mario','41')
// cargo datos a la tabla "MG"
insert into "MG"("NH","NM") values ('Mario','Marcia')

select "NM","Edad" from "MS"


where exists (select "NH"
from "HS"
where "Edad">='40')
and "Edad"<'30'
Resultado = Maria 28
Marcia 27

11.
Mujeres solteras a las que les gustan hombres casados a los que les gust
an mujeres solteras.
select "MS"."NM" from "MS"
inner join "MG"
on "MS"."NM" = "MG"."NM"
inner join "MA"
on "MG"."NH" = "MA"."NH"
Resultado= Maria ("MS") por que gusta de Mariano ("HC")

12.
Mujeres solteras a las que slo les gustan hombres casados a los que les g
ustan mujeres soleras.
select "MS"."NM" from "MS"
inner join "MG"
on "MS"."NM" = "MG"."NM"
inner join "HC"
on "MG"."NH" = "HC"."NH"
13.
Mujeres solteras a las que slo les gustan hombres casados a los que slo le
s gustan mujeres soleras.

select "MS"."NM" from "MS"


inner join "MG"
on "MS"."NM" = "MG"."NM"
inner join "HC"
on "MG"."NH" = "HC"."NH"
where exists (select "NM"
from "MS"
where "Edad"<='30')

14.

Mujeres solteras a las que les gustan hombres a los que ellas no gustan.

15.
an.

Mujeres solteras a las que slo les gustan hombres a los que ellas no gust

16.
Mujeres solteras de menos de 30 a las que a las que les gustan hombres s
olteros y ningn casado.
17.
Mujeres casadas a las que les gustan hombres casados pero no el suyo.
18.
.

Hombres a los que les gustan todas las mujeres casadas y ninguna soltera

19.
Mujeres solteras a las que les gustan todos los casados a los que les gu
stan todas las solteras.
20.

Hombres casados a los que les gustan todas las casadas menos su mujer.

21.

Mujeres a las que les gustan todos los hombres 10 aos ms jvenes que ellas.

SELECT DISTINCT
"M"."NM",
"M"."Edad",
"H"."NH",
"H"."Edad"
FROM
public."H",
public."M",
public."MG"
WHERE
"M"."NM" = "MG"."NM" AND
"MG"."NH" = "H"."NH";
resultado
"Romina
"Julia
"Merli
"Lyla
"Jaque

";32;"Gonzalo
";30;"Juan
";27;"Mariano
";32;"Franco
";28;"Emanuel

";32
";30
";32
";28
";28

COMO NO TENGO NINGUN CASO LOS CREO


insert into "H" ("NH","Edad") values ('Pedro','20');
insert into "H" ("NH","Edad") values ('Ariel','22');
insert into "MG" ("NH","NM") values ('Pedro','Julia')
insert into "MG" ("NH","NM") values ('Ariel','Romina')
insert into "MG" ("NH","NM") values ('Ariel','Lyla')
AHORA el CONJUNTO ES
"Romina
"Julia
"Merli
"Julia
"Lyla
"Lyla
"Romina
"Jaque
SI HAGO:
SELECT DISTINCT
"M"."NM",
"M"."Edad",
"H"."NH",

";32;"Gonzalo
";30;"Juan
";27;"Mariano
";30;"Pedro
";32;"Ariel
";32;"Franco
";32;"Ariel
";28;"Emanuel

";32
";30
";32
";20
";22
";28
";22
";28

"H"."Edad"
FROM
public."H",
public."M",
public."MG"
WHERE
"M"."NM" = "MG"."NM" AND
"MG"."NH" = "H"."NH" AND
"M"."Edad" >= "H"."Edad" + 10
"Julia
"Lyla
"Romina

22.

";30;"Pedro
";32;"Ariel
";32;"Ariel

";20
";22
";22

Hombres a los que les gustan ms de 3 mujeres.

SELECT
"HG"."NH" FROM public."HG"
GROUP BY "NH"
HAVING COUNT("NH") >3
23.

Mujeres que se casaron ms de 2 veces.

24.

El hombre con menor edad.

SELECT "H"."NH",
WHERE "Edad" =

"H"."Edad" FROM

( SELECT MIN("Edad") FROM

public."H"

public."H" )

select "NH","Edad" from "H"


where "Edad"<'27'
group by "NH","Edad"
order by "NH","Edad"
//Resultado Hugo 22
25.

Mujer con la segunda mayor edad.


select "NM","Edad" from "M"
where "Edad">'20'
group by "NM","Edad"
order by "NM","Edad"

UN EJEMPLO DE DIVISION
SELECT * FROM "MG";
"Mariano

";"Merli

"

"Emanuel
"Juan
"Gonzalo
"Franco
"Pedro
"Ariel
"Ariel

";"Jaque
";"Julia
";"Romina
";"Lyla
";"Julia
";"Romina
";"Lyla

"
"
"
"
"
"
"

SELECT * FROM "HS";


"Gonzalo
"Julio
"Leandro
"Franco

"
"
"
"

entoces inserto 3 nuevos asi puedo obtener una mujer que le guste todos los solt
eros
insert into "MG" ("NH","NM") values ('Julio','Romina');
insert into "MG" ("NH","NM") values ('Leandro','Romina');
insert into "MG" ("NH","NM") values ('Franco','Romina');
HACEMOS LOS EJEMPLOS DE DIVISION A/B
A ES MG MUJER GUSTA Y B ES HOMBRE SOLTEROS

ENTONCES:
1)
SELECT DISTINCT x
FROM A
EXCEPT SELECT x FROM
(SELECT x, y FROM
( SELECT x FROM A ) as C,
( SELECT y FROM B ) as D
EXCEPT
SELECT x, y FROM A ) as E
SELECT DISTINCT "NM" FROM "MG"
EXCEPT
SELECT "NM" FROM
( SELECT "NM","NH" FROM
(SELECT "NM" FROM "MG" ) AS C,
(SELECT "NH" FROM "HS") AS D
EXCEPT
SELECT "NM","NH" FROM "MG") AS E

2)
SELECT DISTINCT x FROM A as H

WHERE NOT EXISTS


( SELECT y FROM B
WHERE NOT EXISTS
(
SELECT * FROM A as G
WHERE G.y = B.y
AND G.x = H.x )
)
SELECT DISTINCT "NM" FROM "MG" AS H
WHERE NOT EXISTS
( SELECT "NH" FROM "HS"
WHERE NOT EXISTS
( SELECT * FROM "MG" AS G
WHERE G."NH" = "HS"."NH" AND
G."NM" = H."NM" )
)
3)
SELECT DISTINCT x FROM A as G
WHERE NOT EXISTS (
( SELECT y FROM B )
EXCEPT
( SELECT B.y
FROM B, A
WHERE A.y = B.y And
A.X = G.x
)
SELECT DISTINCT "NM" FROM "MG" AS G
WHERE NOT EXISTS
( SELECT "NH" FROM "HS"
EXCEPT
( SELECT "HS"."NH" FROM "MG","HS"
WHERE "MG"."NH" = "HS"."NH" AND
"MG"."NM" = G."NM" )
)

4)

SELECT DISTINCT x
FROM A, B
WHERE A.y = B.y
GROUP BY A.x
HAVING COUNT(DISTINCT A.y) =
( SELECT COUNT(DISTINCT y)
SELECT DISTINCT "NM" FROM "MG", "HS"
WHERE "MG"."NH" = "HS"."NH"
GROUP BY "MG"."NM"
HAVING COUNT(DISTINCT "MG"."NH")=
( SELECT COUNT(DISTINCT "NH" ) FROM "HS" )

FROM B)