Sobes.tech
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, LIMIT is used:
SELECT * FROM users LIMIT 10;
  • In SQL Server, TOP is used:
SELECT TOP 10 * FROM users;
  • In Oracle versions before 12c, ROWNUM is used:
SELECT * FROM users WHERE ROWNUM <= 10;
  • In Oracle 12c and above, FETCH FIRST can 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.