Middle
How to limit the number of records displayed in an SQL query?
sobes.tech AI
Answer from AI
To limit the number of records returned in an SQL query, keywords depending on the DBMS are used:
- In MySQL, PostgreSQL, SQLite,
LIMITis used:
SELECT * FROM users LIMIT 10;
- In SQL Server,
TOPis used:
SELECT TOP 10 * FROM users;
- In Oracle versions before 12c,
ROWNUMis used:
SELECT * FROM users WHERE ROWNUM <= 10;
- In Oracle 12c and above,
FETCH FIRSTcan be used:
SELECT * FROM users FETCH FIRST 10 ROWS ONLY;
Thus, to limit the number of records, you should use the appropriate syntax LIMIT, TOP, or FETCH depending on the database you are using.