Sql – Oracle SQL query count, group by with an innerjoin

countoraclesql

Basicly I have a bunch of Cars that belong to dealers and the dealers have a group.
The dealer table has the GROUP.ID and the Group table has the group name.
A Group has multiple dealers

So I want to count how many cars each group has.

I am using this atm

select  
  (select GROUP_NAME from "GROUP" where "GROUP".GROUP_ID = "DEALER"."GROUP_ID" ),
  "DEALER"."GROUP_ID" as "DEALER GROUP ID",
  "DEALER"."DEALER_NAME" as "DEALER DEALER NAME",
  "CAR"."CAR_DEALER" as "CAR DEALER"         
from 
  "CAR"
INNER JOIN 
  DEALER
ON 
  "DEALER"."DEALER_NAME" ="CAR"."CAR_DEALER"

I tried using group_by and count but I can't seem to get it to work

Best Answer

select
  g.GROUP_NAME,
  g.GROUP_ID,
  count(*) as CAR_COUNT
from
  GROUP g
  inner join DEALER d on d.GROUP_ID = g.GROUP_ID
  inner join CAR c on c.DEALER_ID = d.DEALERID
group by
  /* Also add here all field you want to select from GROUP */
  g.GROUP_NAME,
  g.GROUPID