Search code examples
sql-serverinner-join

Inner join with Count in SQL and group by


I have this table called Equipos.

id  idType
1   1
3   2
4   3
5   4
6   4

And this other table which I want to Inner Join with (the catalogue with descriptions or ids).

id  descripcion
1   Macbook
2   iMac
3   Dell Lap
4   Dell Lap OP

I want something like

descripcion   count
Macbook       1
iMac          1
Dell Lap      1
Dell Lap OP   2

This is what Im trying so far.

select tipoId, count(tipoId) 
from Equipos eq 
group by tipoId 
inner join TipoEquipo tip on tip.id=eq.idType 

But to no avail.


Solution

  • This is indeed basic SQL, but I would prefer to use left outer join for this.

    select tip.descripcion, count(eq.idType) 
    from TipoEquipo tip  
    left outer join Equipos eq on tip.id=eq.idType 
    group by tip.descripcion