I have to make a Table that indicates the "name2", "local" and "name" of the animals in greater number in each "name".
SELECT DISTINCT OP.nome , OP.localizacao , AD.nome_cientifico
FROM C AD , A OP, B CP
WHERE OP.id_aquario = CP.id_aquario AND
CP.id_especie = AD.id_especie AND
NOT EXISTS(SELECT *
FROM C AD2
WHERE AD2.id_aquario = CP.id_aquario)
GROUP BY OP.nome;
Tables
-- A (name,Num2, local)
insert into A values ('Favela',1,'WE');
insert into A values ('Lamosa',2,'NA');
insert into A values ('Luz',3,'S0');
-- B (Num1,name,Num2, Num3)
insert into B values (1,'Maria',1,1);
insert into B values (2,'Jorge',2,1);
insert into B values (3,'Teresa',2,1);
insert into B values (4,'Rui',3,1);
insert into B values (1,'Fran',3,3);
insert into B values (2,'Juliett',3,3);
-- C (num5,num3,name2,num4)
insert into C values (2,1,'Cao',9);
insert into C values (1,2,'Gato',8);
insert into C values (1,3,'Golfinho',7);
insert into C values (3,4,'Peixe',11);