Q:

SELECTS only selects one element

I'm having some problems with this select. I wanna determine the "pegada ecológica" of every product with the type "lar".
With this select I was able to get the first product with the type "lar", but I want every product. I don't know how this happened.
SELECT DISTINCT P.codigo as "Código Produto", 
       SUM(Compra.quantidade)*SUM(Composto.percentagem)*SUM(E.pegadaEcologica) as "Pégada Ecológica"
FROM Produto P, Elemento E, composto Composto, compra Compra
WHERE P.tipo = 'lar' AND P.codigo = Composto.produto 
       AND Composto.elemento = E.codigo AND P.marca = Composto.prodMarca

My tables:
Create table  Elemento  (
  codigo    char(3),
  nome  varchar(25) not null,
  pegadaEcologica   int(2) not null,
  saude int(2) not null,
  constraint pk_Elemento     primary key (codigo)
);

Create table  Produto  (
  codigo    int(6),
  marca int(7),
  nome  varchar(50) not null,
  tipo  char(10),
  comercioJusto char(1),
  constraint Produto_tipo_RI004 check (tipo in ('alimentac','lar','jardim','automov','viagem','electrodom')),
  constraint Produto_comercioJusto_RI005     check (comercioJusto in ('A','B','C','D')),
  constraint fk_Produto_marca    foreign key (marca) references Marca(numero) on delete cascade,
  constraint pk_Produto  primary key (codigo,marca)
);

Create table  compra  (
  produto   int(6),
  prodMarca int(7),
  consumidor    int(9),
  quantidade    decimal(10,3)   not null,
  constraint compra_quantidade_RI006    check (quantidade>0),
  constraint fk_compra_produto   foreign key (produto,prodMarca) references Produto(codigo,marca) on delete cascade,
  constraint fk_compra_consumidor    foreign key (consumidor) references Consumidor(numero) on delete cascade,
  constraint pk_compra   primary key (produto,prodMarca,consumidor)
);

Create table  composto  (
  produto   int(6),
  prodMarca int(7),
  elemento  char(3),
  percentagem   decimal(4,1)    not null,
  constraint composto_percentagem_RI007 check (percentagem>0 and percentagem<=100),
  constraint fk_composto_produto     foreign key (produto,prodMarca) references Produto(codigo,marca) on delete cascade,
  constraint fk_composto_elemento    foreign key (elemento) references Elemento(codigo) on delete cascade,
  constraint pk_composto     primary key (produto,prodMarca,elemento)
);

NOTE: If you need INSERTs, I have them, let me know and I'll post those too.

A:

You are aggregating some data using an aggregation function like sum(), avg(), max(), group_concat() or similar. In your case: 
SUM(Compra.quantidade)*SUM(Composto.percentagem)*SUM(E.pegadaEcologica)

You are also selecting a field upon which you are grouping the aggregate data. 
P.codigo as "Código Produto", 

For this you MUST specify that non-aggregated field in a GROUP BY clause at the end of your query. This clause tells the database to aggregate up to the level of each distinct combo of fields in your GROUP BY. 
SELECT P.codigo as "Código Produto", 
       SUM(Compra.quantidade)*SUM(Composto.percentagem)*SUM(E.pegadaEcologica) as "Pégada Ecológica"
FROM Produto P, Elemento E, composto Composto, compra Compra
WHERE P.tipo = 'lar' AND P.codigo = Composto.produto 
       AND Composto.elemento = E.codigo AND P.marca = Composto.prodMarca
GROUP BY P.codigo

You must, as I have done, remove the DISTINCT keyword. Distinct is similar to GROUP BY but it is only correct to use it when you are not using any aggregate functions. 
Lastly, a change has been made to the default set up of MySQL in 5.7 and newer to bring it in line with most other RDBMS products. You must, in the newer versions, include your non-aggregated fields in a GROUP BY clause otherwise the parser will reject your query. As such, it's encouraged that you always include all of your non-aggregated fields in your GROUP BY. It helps future proof your SQL and makes it more portable (As the same statement would fail if you ported this over to Postgres or Oracle or SQL Server or nearly any other database).

