+ 1

Sql question (homework 2)

Greetings, I hope teach don't mind I'm surfing In the Internet in search for an answer. I have a list of cars in which I also have the number(N°) of company to which they belong. How do I sort companies by decreasing number of cars belonging to it? So far my code looks like : SELECT Company.nom, COUNT (Car.num) FROM Cars, Company WHERE Company.num = Car.numCompany GROUP BY Company.nom; I would really appreciate your help, thanks.

30th Sep 2017, 9:48 PM
назік іванюк
назік іванюк - avatar
6 Answers
+ 3
add this code after GROUP BY clause ORDER BY company.nom DSEC
30th Sep 2017, 10:23 PM
Sayid Yarmohammadi
Sayid Yarmohammadi - avatar
+ 3
SELECT Company.Num,Cars.Num,Count(Cars.Num) as CountOfCars FROM Company INNER JOIN Cars ON Cars.Num=Company.Num GROUP BY Company.Num ORDER BY CountOfCars DSEC I think this was true
30th Sep 2017, 11:02 PM
Sayid Yarmohammadi
Sayid Yarmohammadi - avatar
+ 3
select d.dname, e.deptno,count (e.empno) from emp e, dept d where e.deptno=d.deptno group by e.deptno,d.dname order by (count(e.empno)) ; it is working SQL on emp and dept table
1st Oct 2017, 7:15 AM
Azam
Azam - avatar
+ 2
you should use nested queries
1st Oct 2017, 1:12 AM
Azam
Azam - avatar
+ 1
^ I tried it before, but my task is to sort them by number of cars they currently have. from company with the biggest amount of cars, to the company with the smallest amount.
30th Sep 2017, 10:38 PM
назік іванюк
назік іванюк - avatar
+ 1
Nope, didn't work. I guess you require more info to make this work than I've already gave. Thanks anyway, I guess just gonna ask teacher next Friday.
30th Sep 2017, 11:20 PM
назік іванюк
назік іванюк - avatar