Is there an "offset" to go with "limit" for Databricks SQL to enable paging - apache-spark-sql

I'm seeing documentation for "limit" for Databricks/Apache Spark SQL (e.g. https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-limit.html), but I'm not seeing how to do an offset to enable paging? Is there a way to do an offset and a limit to get the second 100 rows back from a query.
Something like this does not work? What would?
%sql
select * from enc
limit 100
offset 100
;
This does work, but now I would like to get the second 100 rows, how do I do the offset?
%sql
select * from enc
limit 100
;

Related

In SQLite3, is there a way to continue a query after row-limiting?

I have a SQL query like this:
SELECT * FROM database WHERE column LIKE ? ORDER BY column LIMIT 128
I would like to be able to obtain, eg, rows 129-256 of the same table. Is there a way to do this with a SQLite query?
(I could redo the same query LIMIT 256 and then skip the first 128 results before further processing, however, I feel like it would be better code to use a SQL query that simply returns the values I actually need to process.)
Add either an OFFSET clause or use two comma-separated values as the arguments of the LIMIT clause, as described in the LIMIT clause section of SQLite's syntax reference.
In other words: ... LIMIT 128 OFFSET 128, or ... LIMIT 128, 128.
Note: in the LIMIT a, b form, a is the offset and b the limit.

Getting random rows in Big Query with different limit?

I have the following queries :
SELECT * FROM `datafusiontest-2897325.mergedquery.test_table LIMIT 10
SELECT * FROM `datafusiontest-2897325.mergedquery.test_table LIMIT 100
SELECT * FROM `datafusiontest-2897325.mergedquery.test_table LIMIT 10000
I am getting a different top result for each query.
As your query is not specifying an order, it is normal for results to be different each time - they are returning random rows from your table which meet the qualifying criteria.
To get the same top n returned you should add an ORDER BY clause, for example:
SELECT *
FROM `datafusiontest-2897325.mergedquery.test_table`
ORDER BY date
LIMIT 10

LIMIT N vs TOP N in SQL Server [duplicate]

This question already has answers here:
How to write a (MySQL) "LIMIT" in SQL Server?
(3 answers)
Closed 4 years ago.
I am starting to use SQL Server. The following works:
SELECT TOP 100 *
FROM SalesRawData
WHERE Title = N'Sriracha'
But the following causes an error:
SELECT *
FROM SalesRawData
WHERE Title = N'Sriracha'
LIMIT 100
Why can't I use the "normal" limit syntax at the end here?
DMBS vendors have proprietary syntax to provide LIMIT functionality. A draft of the ANSI/ISO standards allows a OFFSET and FETCH clauses, which is also implemented in SQL Server. ORDER BY is required (and with TOP also) to provide predictable results.
Below is an alternative:
SELECT * FROM SalesRawData
WHERE Title = N'Sriracha'
ORDER BY Title OFFSET 0 ROWS FETCH FIRST 100 ROWS ONLY;
SQL Server supports two ways to limit the number of results returned from a query. The simplest is TOP, which you have already discovered. The more advanced is FETCH, which also allows for specifying an offset to implement paging. Written using the FETCH method, your query would be SELECT * FROM SalesRawData where Title = N'Sriracha' OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY. You can find the documentation for TOP here and the documentation for FETCH here.

How to use LIMIT in query in access, but not TOP

I would like to display 15 rows from queries but not first 15?
SELECT Abgänge.Vorgang, Abgänge.Date_SW_Drucken FROM Abgänge
WHERE Abgänge.Bezahlung = "Bar" LIMIT 34,15;
How to transform this to access 2010?
Limit:
LIMIT from_record-1, count_record
You can't, because there is no support for an offset in the Microsoft Access SELECT syntax. An option is to use TOP with offset + limit and skip offset rows manually. BTW: Using TOP or LIMIT without an ORDER BY is not advisable as it can lead to inconsistent results.
You could also combine two queries with TOP, first selecting limit + offset, and then selecting only offset, for example
SELECT TOP 15 ...
FROM (
SELECT TOP 49 ....
FROM sometable
ORDER BY somecolumn ASC
) a
ORDER BY somecolumn DESC
The only problem with this solution is that if there are less than 49 results from the subquery, then the offset will be less than 34.
If you need the result in a different order then you may need to add an additional 'layer' that applies that order.

How can I get a specific chunk of results?

Is it possible to retrieve a specific range of results? I know how to do TOP x but the result I will retrieve is WAY too big and will time out. I was hoping to be able to pick say the first 10,000 results then the next 10,000 and so on. Is this possible?
WITH Q AS (
SELECT ROW_NUMBER() OVER (ORDER BY ...some column) AS N, ...other columns
FROM ...some table
) SELECT * FROM Q WHERE N BETWEEN 1 AND 10000;
Read more about ROW_NUMBER() here: http://msdn.microsoft.com/en-us/library/ms186734.aspx
Practically all SQL DB implementations have a way of specifying the starting row to return, as well as the number of rows.
For example, in both mysql and postgres it looks like:
SELECT ...
ORDER BY something -- not required, but highly recommended
LIMIT 100 -- only get 100 rows
OFFSET 500; -- start at row 500
Note that normally you would include an ORDER BY to make sure your chunks are consistent
MS SQL Server (being a "pretend" DB) don't support OFFSET directly, but it can be coded using ROW_NUMBER() - see this SO post for more detail.