codegurukool.com · Free coding tutorials & online compiler

Home › SQL Tutorial › SQL ORDER BY and LIMIT

SQL ORDER BY and LIMIT

Sort results and take the first few.

ORDER BY sorts ascending (ASC, the default) or descending (DESC). LIMIT n returns only n rows (MySQL, PostgreSQL, SQLite). SQL Server uses SELECT TOP n, and Oracle uses FETCH FIRST n ROWS ONLY.

Example code

SELECT name, country FROM Customers
ORDER BY country ASC, name DESC
LIMIT 3;

Run this code in the online editor

← SQL WHERESQL AND, OR, NOT →