Questions tagged [sql-limit]

SQL clause to limit number of returned rows

The LIMIT clause is used to specify a maximum of rows to be returned in a SELECT query. The various SQL dialects use different syntax elements for that purpose. LIMIT x OFFSET y, is understood by MySQL, PostgreSQL, SQLite and some others RDBMS.
Example:

SELECT * FROM tbl LIMIT 10;

The SQL:2008 standard defines:

FETCH FIRST n ROWS ONLY

SQL Server uses TOP n, Oracle its rownum feature.
There is a comprehensive list on Wikipedia.

315 questions
0
votes
2 answers

What are the benefits of using LIMIT ALL in a subquery?

While researching an unrelated topic, I noticed the use of LIMIT ALL in an example on the IBM Knowledge Center website for Netezza. I'm unclear about the benefits of specifying LIMIT ALL here, and I am seeking clarification of the explanation…
Allan
  • 15
  • 3
0
votes
1 answer

sqLite OFFSET query does not work with WHERE clause?

Can someone explain why this sqlite3 query will work and return my 1000 records: SELECT * FROM fmsdata LIMIT 1000 OFFSET (SELECT COUNT(*) FROM fmsdata) - 1000 but the minute I drop in the where clause WHERE valve=3 it returns nothing? SELECT * FROM…
simgineer
  • 1,754
  • 2
  • 22
  • 49
0
votes
1 answer

PostgreSQL get nested rows with limit

I got a self referencing table that's only 1 deep: comments and replies. A reply is just a comment with a parent id: Comments (simplified): - comment_id - parentCommentId Users have to scroll through the comments and replies and typically…
Elger Mensonides
  • 6,930
  • 6
  • 46
  • 69
0
votes
2 answers

COUNT returns total rows ignoring LIMIT in MySQL, why?

Today while I was writing a complex query, accidently I found that even I have set LIMIT in my query MySQL server returns the total number of rows for COUNT. Example: SELECT COUNT(*) FROM `log` LIMIT 10; Output: 5219 But if I run the query…
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
0
votes
1 answer

Top 50 records by DATALENGTH of a field in SQL Server

Azure SQL Server 2016 - I'm having trouble with the syntax to combine TOP, MAX and DATALENGTH to get a list of the Top 50 records from a table by DATALENGTH of a specific field. The field I need to do the DATALENGTH on is called Text. I don't…
Stpete111
  • 3,109
  • 4
  • 34
  • 74
0
votes
3 answers

Simplify SQL query of multiple SELECT statements, each with UNION and LIMIT

Need assistance with simplifying this SQL query to possibly a single SELECT: (SELECT * FROM `deals` WHERE category_id = 1 ORDER BY id desc LIMIT 10) UNION (SELECT * FROM `deals` WHERE category_id = 2 ORDER BY id desc LIMIT 10) UNION (SELECT * FROM…
0
votes
1 answer

PHP PDO with IN clause and LIMIT

I'm trying to execute statement with IN clause using PDO and with LIMIT. Following this tutorial https://phpdelusions.net/pdo#in I've managed to build code that is working, but doesn't retrieve data as it should $inStmt = DB::prepare("SELECT…
EyeMaze
  • 143
  • 2
  • 9
0
votes
0 answers

Highcharts limit of records

I do not want to set the limit for this chart. How to delete it to work? Code: $sql = "(SELECT (UNIX_TIMESTAMP(timestamp)*1000) AS time ,IFNULL(hodnota,'null')AS temperature FROM arduino_data ORDER BY timestamp DESC LIMIT 60)…
0
votes
0 answers

Treating multiple columns as a single entity with Google Sheets QUERY function

The code below inside the QUERY() spreadsheet function doesn't seem to work as expected! =QUERY(...., "Select E, I, count(H) group by E, I order by count(H) desc limit 10") Result: Overall Top 10 rows based on Count(H) Expected/Looking for: Top 10…
0
votes
1 answer

SQLite limit is a string

The Android API docs appear to suggest that the limit clause to provide when querying a SQLite database is a string. This does not make much sense to me. Presumably, it is converted internally to an integer? Or are there other issues involved here?
DroidOS
  • 8,530
  • 16
  • 99
  • 171
0
votes
2 answers

mysql limit result to first phrase per row

I'm trying to limit the result of a column value to only the first line/phrase. I know there is an option like SUBSTRING(), but this is counting the characters, which does not do the wanted job. Is there a way to cut the data on the first line-break…
George
  • 111
  • 1
  • 7
0
votes
2 answers

MySQL - SELECT equal amount of each in UNION by total LIMIT

In a MySQL database I have 3 tables: customers, projects and tasks. For a search that I am implementing, I want to search through all 3 tables and select found matches. The problem is that I would like to equally limit the amount of results returned…
DylanVB
  • 187
  • 1
  • 14
0
votes
0 answers

Results are different with MySQL 5.7.18 versus 5.7.16

We are in the process of upgrading our version of MySQL from 5.7.16 to 5.7.18. We have an existing process that does not seem to be compatible with the newer version of MySQL. It is a paging process that updates up to 100,000 rows by doing 500 at a…
0
votes
2 answers

Select first 50 rows then order

Is it possible to select the first 50 rows in Postgres with select * from yellow_tripdata_staging fetch first 50 rows only and after that sort the results by column? If so, how? edit: the table is really big, and is not really important which rows…
0
votes
4 answers

Fetch entries in table from last using LIMIT a,b or limit offset

I want to fetch latest entries in a table that is containing more than 1,000,000 entries. I am using this query for an instance SELECT id FROM tablea WHERE flag = "N" ORDER BY id LIMIT 510045,200; and it gives me entries starting from 510045 and…