How can I join this two queries,in one? I have this 2 queries, and I would like to join them

First query

SELECT proveedors.nombre as proveedor, COUNT(productos.id) as distintos
FROM proveedors
INNER JOIN productos ON proveedors.id = productos.proveedor_id
GROUP BY proveedors.id;


Second query

SELECT proveedors.nombre as proveedor, COUNT(productos.id) as instock
FROM proveedors
INNER JOIN productos ON proveedors.id = productos.proveedor_id
WHERE productos.stock &gt; '0'
GROUP BY proveedors.id;
Because they have the same GROUP BY you can just add the other aggregate function to the select list, like this..

SELECT
  proveedors.nombre as proveedor,
  COUNT(productos.id) as distintos,
  COUNT(CASE WHEN productos.stock &gt; '0' THEN productos.id END) as instock
FROM proveedors
INNER JOIN productos ON proveedors.id = productos.proveedor_id
GROUP BY proveedors.id;


Or you can use the new FILTER clause if your rdbms supports it. PostgreSQL does.

SELECT
  proveedors.nombre as proveedor,
  COUNT(productos.id) as distintos,
  COUNT(productos.id) FILTER (WHERE productos.stock &gt; '0') as instock
FROM proveedors
INNER JOIN productos ON proveedors.id = productos.proveedor_id
GROUP BY proveedors.id;


You should probably not be single-quoting the 0, but I have no idea what rdbms you're using and what's going on there.