Which keyword should be used in a SELECT statement to retrieve only parts of the result set?

Dani AI

Generated

Short answer: the clause you use depends on the SQL engine. As pointed out for MySQL, the server provides a LIMIT-style clause to return only part of a result set; other engines use different syntax. See the MySQL SELECT manual for details: MySQL SELECT manual.

Practical notes and portability: SQL Server uses the TOP clause (TOP — Microsoft Docs), while many modern RDBMS also support the SQL:2008-standard form (FETCH FIRST n ROWS ONLY) and the traditional LIMIT/OFFSET pattern (PostgreSQL docs: LIMIT and FETCH). Always include an ORDER BY if you need a predictable subset; without it the rows returned are not guaranteed to be in any particular order.

Performance and paging: simple offset-based paging can be expensive for large offsets because the engine must scan and skip rows. For large datasets prefer keyset (cursor) pagination using an indexed ordering column, or use window functions like ROW_NUMBER() for complex paging scenarios (see SQL Server ROW_NUMBER docs: ROW_NUMBER — Microsoft Docs). This expands on 's request for examples and gives guidance for picking the right approach across engines.

Recommended Answers

All 2 Replies

post thread in clearly with example...

you can use limit

select * from table limit 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.