Friday 11 July 2014

Using GROUP BY,HAVING,DISTINCT

Using GROUP BY,HAVING,DISTINCT

I have the data as below:

I need to get the employees who owns multiple cars and multiple distinct cars.
The above data says the cars owned by the employees in a company.An employee has multiple cars.It can be same car or different car.

Multiple Cars:
SELECT ID FROM CARS GROUP BY ID HAVING COUNT(*)>1;
--Gives 2,4,5,6

Multiple Distinct Cars:
SELECT ID FROM CARS GROUP BY ID HAVING COUNT(DISTINCT CAR)>1;
--Gives 2 and 4

No comments:

Post a Comment