Mohan Sharma
Mohan Sharma

Reputation: 165

select top 5 rows returned by particular query sql server

I want to retrieve top 5 rows returned by this query. How to do this

select COUNT(trippackageID), trippackageid 
from tbl_holiday_booking
group by trippackageID

Upvotes: 1

Views: 466

Answers (5)

Piskvor left the building
Piskvor left the building

Reputation: 92792

SELECT COUNT(trippackageID) AS tpi_c, trippackageid 
  FROM tbl_holiday_booking 
  GROUP BY trippackageID 
  ORDER BY tpi_c DESC 
  LIMIT 5

Upvotes: 0

zebediah49
zebediah49

Reputation: 7611

select COUNT(trippackageID) as cnt, trippackageid from tbl_holiday_booking group by trippackageID ORDER BY cnt DESC LIMIT 0,5

Assuming you do, in fact, want to order by the count, descending (large to small). LIMIT 0,5 starts at row 0, and returns the next 5 rows.

Upvotes: 0

Chris Diver
Chris Diver

Reputation: 19842

You are not specifying the order, do you want the package with the most bookings or the least?

SELECT TOP 5 COUNT(truppackageID) Num, trippackageid FROM tbl_holiday_booking GROUP BY trippackageID ORDER BY Num DESC 

Upvotes: 2

Hiyasat
Hiyasat

Reputation: 8926

You Can use

limit 5

Upvotes: 0

gillyb
gillyb

Reputation: 8910

select top 5 * from....

Upvotes: 0

Related Questions