cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Requesting support for "SELECT TOP n from Table"

SethuSrinivasan
New Contributor II

In notebook, It looks like if I need to select top N rows, I can rely on "LIMIT" keyword. It would be nice if you can support "TOP" as well

The current approach to select 10 rows:

select * from table1 LIMIT 10

Requesting TOP support:

SELECT TOP 10 * from table1

It is quite common for SQL Server users to use TOP clause in their queries. Can you please add this support to your SQL language?

1 REPLY 1

Louis_Frolio
Databricks Employee
Databricks Employee

@SethuSrinivasan , Looks like this one slipped through the cracks โ€” apologies if you've long since moved on, but posting anyway in case it helps someone hitting the same wall.

In Databricks SQL, SELECT TOP n doesn't exist. You get the same result with LIMIT, and if you care about which rows you're getting, pair it with ORDER BY.

Three patterns worth knowing:

Just the first N rows in any order: SELECT * FROM table1 LIMIT 10;

Top N by a column โ€” say, highest salary: SELECT * FROM employees ORDER BY salary DESC LIMIT 10;

Top N per group โ€” say, top 3 orders per customer โ€” you'll want a window function: SELECT * FROM (SELECT o.*, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_total DESC) AS rn FROM orders o) WHERE rn <= 3;

That last one is more flexible than TOP ever was anyway, and it scales well once you start dealing with more complex ranking logic.

Rule of thumb: wherever you'd write SELECT TOP 10 * FROM table1 in SQL Server, swap it for LIMIT 10 in Databricks SQL. Tack on ORDER BY if the order actually matters to you.

Hope this helps, Louis.