SQL min function for subquery

32 Views Asked by At

I want to find company name which has the lowest amount of trip_no

how do I find the lowest amount of trip_no

select t1.Id_comp, company.name,
count(t1.trip_no)
from trip t1
    inner join company on t1.ID_comp=Company.ID_comp
group by t1.Id_comp, company.name
having count(t1.trip_no) = (select min((select t2.ID_comp ,count(t2.trip_no)
                                            from trip t2 
                                            group by t2.ID_comp)))
1

There are 1 best solutions below

1
Er. Mohammad Shuaib On
SELECT * FROM (
    SELECT *, RANK() OVER(PARTITION BY Id_comp ORDER BY trip_no DESC) AS r 
FROM trip INNER JOIN company on trip.ID_comp=Company.ID_comp) sub
WHERE r = 1;