Imagine you’re running an online furniture store. You regularly need to find your top-selling products — maybe the top 5 items by revenue or the most popular pieces in each category. But what if two products have the same sales numbers as the last one on your list? Should you leave one out just because it happens to appear later in the sort order?
That’s where LIMIT ... WITH TIES comes in.
When you’re analyzing data, ranking products, or listing top performers, it’s common to use LIMIT or FETCH FIRST n ROWS ONLY to display the “Top N” results. But by default, PostgreSQL stops strictly after N rows, even if other rows share the same ranking value. With WITH TIES, you can include those additional rows that “tie” with the last one—ensuring your results are fair and complete.
The Problem with Regular LIMIT
Imagine a table named furniture with the following data:
| id | product_name | price |
| 1 | Wooden Table | 5000 |
| 2 | Office Chair | 4500 |
| 3 | Dining Chair | 4500 |
| 4 | Study Desk | 4000 |
| 5 | Sofa Set | 3500 |
Now, suppose you want the top 3 most expensive products:
SELECT *
FROM furniture
ORDER BY price DESC
FETCH FIRST 3 ROWS ONLY;
Result:
| id | product_name | price |
| 1 | Wooden Table | 5000 |
| 2 | Office Chair | 4500 |
| 3 | Dining Chair | 4500 |
This gives you exactly three rows — but what if another product also costs 4500? That product wouldn’t appear, even though it ties with the last row.
Solution: Use WITH TIES
To include all rows that tie on the ORDER BY value, use the WITH TIES option:
SELECT *
FROM furniture
ORDER BY price DESC
FETCH FIRST 3 ROWS WITH TIES;
Result:
| id | product_name | price |
| 1 | Wooden Table | 5000 |
| 2 | Office Chair | 4500 |
| 3 | Dining Chair | 4500 |
| 4 | Recliner Chair | 4500 |
The WITH TIES clause ensures that no rows with equal sort values are left out — making your top-N results more accurate and fair.
Key Points
- You must include an ORDER BY clause when using WITH TIES — otherwise, PostgreSQL won’t know what defines a “tie.”
- The number after FETCH FIRST (or LIMIT) defines the threshold, not the exact number of rows returned.
- All rows with the same ORDER BY value as the last row are automatically included.
Real-World Use Cases
1. Sales Leaderboard
SELECT product_name, total_sales
FROM monthly_sales
ORDER BY total_sales DESC
FETCH FIRST 5 ROWS WITH TIES;
If multiple products have the same sales as the 5th-ranked one, they’ll all appear — ensuring a fair comparison.
2. Employee Performance Ranking
SELECT employee_name, performance_score
FROM employee_reviews
ORDER BY performance_score DESC
FETCH FIRST 10 ROWS WITH TIES;
All employees who scored the same as the 10th position are included — perfect for transparent rankings or rewards.
Quick Comparison
| Query Type | Description | May Include Extra Rows? |
| LIMIT n | Returns exactly n rows | No |
| FETCH FIRST n ROWS ONLY | Returns exactly n rows | No |
| FETCH FIRST n ROWS WITH TIES | Returns n rows plus any with the same ORDER BY value | Yes |
Key Points
- You must use ORDER BY for WITH TIES to make sense; otherwise, PostgreSQL will complain.
- The number after FETCH FIRST (or LIMIT) defines your cutoff point, not the total rows returned.
- Tied rows are compared using the columns in your ORDER BY clause.
Perfect for dashboards or reports where you don’t want to exclude equally high performers.
FETCH FIRST … WITH TIES is a simple yet powerful SQL feature that helps you:
- Handle ranking ties correctly
- Keep your reports and leaderboards fair
- Maintain accuracy in “Top N” analytics
It’s part of the SQL standard and fully supported in PostgreSQL—concise, readable, and extremely useful for business reporting.
Next time you’re writing a query for top-selling products, best-performing employees, or highest-rated items — remember to add WITH TIES.Because sometimes, being tied still means being at the top.