How do you return only the first 10 rows from a query in PostgreSQL/MySQL?

Study for the SQL Basics Test. Improve your knowledge with multiple choice questions and detailed explanations. Prepare effectively to master SQL concepts!

Multiple Choice

How do you return only the first 10 rows from a query in PostgreSQL/MySQL?

Explanation:
Limiting the number of rows returned is done with LIMIT. In PostgreSQL and MySQL, adding LIMIT 10 to your query restricts the result to at most ten rows. If you want the first ten in a specific order, include an ORDER BY to define what "first" means, for example: SELECT * FROM table ORDER BY some_column ASC LIMIT 10. Other forms don’t fit here: asking for seven rows would give seven instead of ten; using LIMIT 10 ROWS isn’t valid syntax in these engines; and TOP 10 is SQL Server syntax, not PostgreSQL/MySQL.

Limiting the number of rows returned is done with LIMIT. In PostgreSQL and MySQL, adding LIMIT 10 to your query restricts the result to at most ten rows. If you want the first ten in a specific order, include an ORDER BY to define what "first" means, for example: SELECT * FROM table ORDER BY some_column ASC LIMIT 10.

Other forms don’t fit here: asking for seven rows would give seven instead of ten; using LIMIT 10 ROWS isn’t valid syntax in these engines; and TOP 10 is SQL Server syntax, not PostgreSQL/MySQL.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy