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.

258 questions
-1
votes
1 answer

ORDER BY ASC DESC & LIMIT

I would to do something such as: SELECT Position INTO lastPosition FROM ranking ORDER BY ranking.Time DESC ranking.Position DESC LIMIT 1; but i get this error: and: and: The goal is to select last row (limit 1) of ranking ordered by descendent…
shogitai
  • 1,348
  • 14
  • 30
-1
votes
2 answers

mysql result with order by is different from order by and limit

I have a table A with columns id, age. Two queries below return different result and I don't know why: SELECT distinct a.id id FROM A a ORDER BY a.age DESC SELECT distinct a.id id FROM A a ORDER BY a.age DESC LIMIT 10 OFFSET 0 Any ideas? I would…
julius_am
  • 1,332
  • 2
  • 19
  • 38
-1
votes
1 answer

SQL LIMIT doesn't work with define

I am writing an SQL request in PHP. the request worked fine like this: $testQuery = "SELECT `title` FROM `bibliography` Where title LIKE '%$search%'"; I need to add a limit. If I add a LIMIT with a number it works fine: $testQuery = "SELECT `title`…
-1
votes
1 answer

How to select only 10 records from the table in jsp?

I'm try to select only 10 row from a table by using limit but it gives me an error, My query is SELECT * FROM table_name ORDER BY CUSTOMER LIMIT 10 It gives an error : ORA-00933: SQL command not properly ended Can anyone guide me.
User
  • 69
  • 1
  • 12
-1
votes
2 answers

PHP Query LIMIT DESC not working properly

I am trying to get a list of highscores (top 10) of players that played my game. Everything works except for one thing, the list isn't right. The first 10 people are correct, but when i go to page 2 the list isn't further going done. Variables…
Jean-Paul
  • 371
  • 2
  • 7
  • 26
-1
votes
2 answers

MySQL Getting last 5 entries for each id in a SELECT - WHERE IN statement

I would like to select last 5 entries for each of the id in a SELECT - WHERE IN statement. //How to get last 5 entries for each id SELECT * FROM table1 WHERE id IN (111,222,333,.....) ORDER BY date DESC LIMIT 5 EDIT: Example of how data…
Cryssie
  • 2,587
  • 8
  • 41
  • 70
-1
votes
1 answer

How to add records to query

I've got query. SELECT * FROM '.PRFX.'sell WHERE draft = "0" '.$e_sql.' AND ID NOT IN (SELECT id_ FROM '.PRFX.'skipped WHERE uid = "'.$u.'") AND ID NOT IN (SELECT id_ FROM '.PRFX.'followed WHERE uid = "'.$u.'") ORDER BY raised DESC…
-1
votes
1 answer

SQL limit gives bad results

I have a problem with using SQL LIMIT statement. I have 21 records stored in database and I want to get results piecewise. I have written this query: SELECT * FROM table JOIN another_table ON XXX = YYY WHERE XXX = ? ORDER BY col DESC LIMIT ?,…
JARDA001
  • 15
  • 4
-2
votes
2 answers

Displaying highest valued transaction

I want to write a query to display all information about the Customer who made the highest transaction. It is expected that the newly created amount due column be used to identify the highest transaction Top Table is my Customer table and the bottom…
-2
votes
1 answer

Mysql Select Sum but last three records

I have table with fields Customer date and amount I want to sum Amount grouped by customer except the last two amounts of every customer by date sample data customer date amount a 2020-10-1 100 a …
-2
votes
2 answers

select COUNT(Id) AS Total from `euro` where (N1 = $i or N2 = $i or N3 = $i or N4 = $i or N5 = $i) LIMIT 50

select COUNT(Id) AS Total from `euro` where (N1 = $i or N2 = $i or N3 = $i or N4 = $i or N5 = $i) LIMIT 50 How to count only 50 results?? I've tryied this with no sucess $sql= "SELECT * FROM euro where (N1 = $i or N2 = $i or N3 = $i or N4 = $i…
-2
votes
2 answers

placing SQL order by (simple)

I'm trying to order the following results on reference. Using Order by and desc limit 100. Can someone help me where to place it. $stm = $pdo->prepare("SELECT `reference`, SUM( `stockdifference` ) AS 'stockdifference' FROM `results1` WHERE…
-2
votes
2 answers

How to take specific rows from postgresql database?

If I need select 20 to 30 rows from PostgreSQL, how can I retrieve that ? SELECT column FROM table LIMIT 10 This is fetching top 10 rows only. Can we do this in PostgreSQL ?
Shesha
  • 1,385
  • 2
  • 14
  • 26
-2
votes
1 answer

Where would the Limit statement go? Postgresql

Short Question, where would the limit clause go in Postgresql? I want to limit the amount of rows to 10, but it is giving me an error when I do; From this_table x join this_table y on x.no = y.no where x.no = '7' Limit 10 group by…
Padagomez
  • 844
  • 4
  • 11
  • 30
-2
votes
3 answers

Return only one row of a select that would return a set of rows

I have a table like: create table myTab( id integer primary key, is_available boolean not null default true ); I need to do a query that returns only the first encountered row that has is_available set to false.
giozh
  • 8,808
  • 27
  • 89
  • 163
1 2 3
17
18