Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Suppose I have a table with CarID column and each id is rented to a a customer when they come to rent it. What I want to be able to find out is through SQL query that what are the 5 most rented out cars like which cars does the customer like and is rents the most and which cars are rented out the 5 least rented out cars.

so how do I do it with an sql query

Please reply ASSP
Thank you.

What I have tried:

CREATE PROCEDURE dbo.SPCarRental_GetRentedCarsTop5
AS
SELECT *
FROM [CarRental]
WHERE
(
CarID IN
(
SELECT TOP (5) CarID
FROM [CarRental] as table1
GROUP BY CarID
ORDER BY CarID DESC
)
)
Posted
Updated 24-Nov-18 5:09am
v2

Break down the sentences and write sub queries until you get what you want.

"5 most rented cars" -> count the cars by id and sort descending

"which cars the customer likes" -> count the cars by id sort descending having the customer's id
 
Share this answer
 
Without your db schema it is kind of hard to answer this; as we can only assume what data you have.

You have referenced the table CarRental in your query. I am guessing that these are distinct reservations for a particular car, identified by the column CarID. If this were my (normalized) DB, I would also have a table for Car with a PK of CarID, as referenced by the rental table.

If all of this is true; the query you have presented would provide you details about the reservations and not so much about the cars themselves. Even if that is what you wanted you are not going to get the 5 most popular as your aggregation is only on the CarID (essentially doing nothing) and sorting by CarID. It is a reasonable assumption then that the subquery is going to always respond with the last 5 CarIDs in descending order
What you would want to do is to keep the aggregate based on CarID, however; you want to do the order on the Count of those CarIDs, descending
SQL
SELECT   *
FROM     CarRental
WHERE    CarID IN (
   SELECT   TOP (5) CarID
   FROM     CarRental
   GROUP BY CarID
   ORDER BY Count(CarID) DESC
)

If I was right on my db schema, that you have a Car table; and you wanted the Car details instead- the query is an easy edit to have the main query reference that table and keeping the subquery as is:
SQL
SELECT   *
FROM     Car
WHERE    CarID IN (
   SELECT   TOP (5) CarID
   FROM     CarRental
   GROUP BY CarID
   ORDER BY Count(CarID) DESC
)
 
Share this answer
 
Comments
Ron23111 24-Nov-18 14:12pm    
Yes I do have a car table
MadMyche 24-Nov-18 14:18pm    
It's one of those important things to do in the original question; letting us know what you have and being explicit in what you want- All the rentals or just the list of cars; and possibly how many times each has been rented
Ron23111 24-Nov-18 14:20pm    
Thank you it worked for me and If I want to show the 5 least rented trucks than
MadMyche 24-Nov-18 14:25pm    
.. then you would change the ORDER BY to ASC in the subquery
Ron23111 24-Nov-18 18:43pm    
Ok thank you soo much for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900