0
0
SQLquery~5 mins

TOP vs LIMIT across databases in SQL

Choose your learning style9 modes available
Introduction

We use TOP or LIMIT to get only a few rows from a big list. This helps us see just what we need quickly.

When you want to see the first 5 customers from a big customer list.
When you need to check the latest 10 orders made in a store.
When you want to test a query but only want a small sample of data.
When you want to show a preview of data on a website without loading everything.
Syntax
SQL
SELECT TOP number column_names FROM table_name;  -- Used in SQL Server, MS Access

SELECT column_names FROM table_name LIMIT number;  -- Used in MySQL, PostgreSQL, SQLite

TOP is placed right after SELECT.

LIMIT is placed at the end of the query.

Examples
This gets the first 3 employee names in SQL Server.
SQL
SELECT TOP 3 name FROM employees;
This gets the first 3 employee names in MySQL or PostgreSQL.
SQL
SELECT name FROM employees LIMIT 3;
Gets the latest 5 orders in SQL Server.
SQL
SELECT TOP 5 * FROM orders ORDER BY order_date DESC;
Gets the latest 5 orders in MySQL or PostgreSQL.
SQL
SELECT * FROM orders ORDER BY order_date DESC LIMIT 5;
Sample Program

This example creates a small table of employees and then selects the first 2 names using LIMIT.

SQL
CREATE TABLE employees (id INT, name VARCHAR(20));
INSERT INTO employees VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Carol'), (4, 'Dave');

-- Using LIMIT (MySQL/PostgreSQL/SQLite style)
SELECT name FROM employees LIMIT 2;
OutputSuccess
Important Notes

TOP is mostly used in Microsoft SQL Server and MS Access.

LIMIT is common in MySQL, PostgreSQL, and SQLite.

Some databases like Oracle use different methods (e.g., ROWNUM) to limit rows.

Summary

Use TOP right after SELECT in SQL Server to limit rows.

Use LIMIT at the end of the query in MySQL and PostgreSQL.

Both help you get a small part of data quickly.