Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL joining two tables and counting

I want to retrieve all the customers who own more than 1 car.

I have this code:

SELECT c.fname, 
       c.lname, 
       c.cid, 
       Count(v.cid) AS nmbrofvehicle 
FROM   customer c 
LEFT JOIN vehicle v 
       ON c.cid = v.cid 
GROUP  BY c.fname; 

But it returns this error:

ORA-00979: not a GROUP BY expression

like image 552
Emile Arab Avatar asked Nov 28 '25 14:11

Emile Arab


1 Answers

select c.fname, c.lname, c.cid, count(v.cid) as nmbrofvehicle
from customer c left join vehicle v on c.cid = v.cid 
group by c.fname, c.lname, c.cid
having count(*) > 1;
like image 115
Peter M. Avatar answered Dec 01 '25 11:12

Peter M.