I need to list only the first five requests for companies from my database. I have two tables named as Request and Company. There could be any amount of requests for a given company. But, I need to query and list only the first 5 requests of all the companies in my database.
select top 5 *
from Request as r
INNER JOIN Company c
ON r.Company_id=c.Company_ID
I have done the above query but it is returning only the first five from the table not the first five of all members. How can I correct my query?
I think you’re looking for the top 5 requests for each company that exists in your Company table. In that case, you can
rank()each request by company in a subselect and then select where the rank is<= 5: