Bird
Raised Fist0
SQLquery~5 mins

TOP vs LIMIT across databases in SQL

Choose your learning style10 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.

Practice

(1/5)
1. Which SQL clause is used to limit the number of rows returned in SQL Server?
easy
A. LIMIT
B. TOP
C. FETCH FIRST
D. ROWNUM

Solution

  1. Step 1: Understand SQL Server syntax

    SQL Server uses the TOP clause immediately after SELECT to limit rows.
  2. Step 2: Compare with other databases

    Other databases like MySQL and PostgreSQL use LIMIT at the end, not SQL Server.
  3. Final Answer:

    TOP -> Option B
  4. Quick Check:

    SQL Server = TOP [OK]
Hint: Use TOP right after SELECT in SQL Server [OK]
Common Mistakes:
  • Using LIMIT in SQL Server
  • Placing TOP at the end of the query
  • Confusing FETCH FIRST with TOP
2. Which of the following is the correct syntax to get the first 5 rows in MySQL?
easy
A. SELECT * FROM table_name LIMIT 5;
B. SELECT TOP 5 * FROM table_name;
C. SELECT * FROM table_name FETCH FIRST 5 ROWS;
D. SELECT * FROM table_name WHERE ROWNUM <= 5;

Solution

  1. Step 1: Recall MySQL syntax for limiting rows

    MySQL uses LIMIT at the end of the query to restrict rows.
  2. Step 2: Check each option

    SELECT * FROM table_name LIMIT 5; uses LIMIT 5 correctly; SELECT TOP 5 * FROM table_name; uses SQL Server syntax; Options C and D are for other databases.
  3. Final Answer:

    SELECT * FROM table_name LIMIT 5; -> Option A
  4. Quick Check:

    MySQL = LIMIT at end [OK]
Hint: Use LIMIT at the end in MySQL queries [OK]
Common Mistakes:
  • Using TOP in MySQL
  • Placing LIMIT before FROM
  • Using ROWNUM which is Oracle syntax
3. Given the table Employees with columns Id and Name, what will this query return in SQL Server?
SELECT TOP 3 Name FROM Employees ORDER BY Id DESC;
medium
A. The first 3 employees ordered by Id ascending
B. The last 3 employees ordered by Id descending
C. Syntax error due to TOP placement
D. The top 3 employees ordered by Id descending

Solution

  1. Step 1: Understand TOP with ORDER BY in SQL Server

    The query selects the top 3 rows after ordering by Id descending, so highest Ids first.
  2. Step 2: Analyze the result

    The result is the 3 employees with the highest Id values, ordered descending.
  3. Final Answer:

    The top 3 employees ordered by Id descending -> Option D
  4. Quick Check:

    TOP 3 + ORDER BY DESC = highest 3 rows [OK]
Hint: TOP limits rows after ORDER BY sorting [OK]
Common Mistakes:
  • Thinking TOP returns first rows before ORDER BY
  • Confusing ascending and descending order
  • Assuming syntax error with TOP
4. Identify the error in this PostgreSQL query:
SELECT TOP 10 * FROM Customers;
medium
A. TOP is not valid in PostgreSQL
B. Missing ORDER BY clause
C. LIMIT should be placed before FROM
D. Syntax is correct

Solution

  1. Step 1: Recall PostgreSQL syntax for limiting rows

    PostgreSQL uses LIMIT at the end, not TOP.
  2. Step 2: Check the query

    The query uses TOP 10, which is invalid syntax in PostgreSQL and causes an error.
  3. Final Answer:

    TOP is not valid in PostgreSQL -> Option A
  4. Quick Check:

    PostgreSQL = LIMIT, not TOP [OK]
Hint: PostgreSQL uses LIMIT, not TOP [OK]
Common Mistakes:
  • Using TOP in PostgreSQL
  • Placing LIMIT before FROM
  • Assuming ORDER BY is mandatory for LIMIT
5. You want to write a query that returns the top 5 highest salaries from an Employees table. Which query works correctly in both SQL Server and MySQL?
hard
A. Use UNION of SQL Server and MySQL queries
B. SELECT Salary FROM Employees ORDER BY Salary DESC LIMIT 5;
C. SELECT Salary FROM Employees ORDER BY Salary DESC FETCH FIRST 5 ROWS ONLY;
D. SELECT TOP 5 Salary FROM Employees ORDER BY Salary DESC;

Solution

  1. Step 1: Understand syntax differences

    SQL Server uses TOP after SELECT; MySQL uses LIMIT at the end.
  2. Step 2: Check each option for cross-DB compatibility

    SELECT TOP 5 Salary FROM Employees ORDER BY Salary DESC; works only in SQL Server; SELECT Salary FROM Employees ORDER BY Salary DESC LIMIT 5; only in MySQL; Use UNION of SQL Server and MySQL queries doesn't provide a single query that parses in both; SELECT Salary FROM Employees ORDER BY Salary DESC FETCH FIRST 5 ROWS ONLY; is standard SQL supported by both.
  3. Final Answer:

    SELECT Salary FROM Employees ORDER BY Salary DESC FETCH FIRST 5 ROWS ONLY; -> Option C
  4. Quick Check:

    FETCH FIRST = cross-DB standard [OK]
Hint: Use FETCH FIRST ROWS ONLY for cross-DB compatibility [OK]
Common Mistakes:
  • Using LIMIT (MySQL only)
  • Using TOP (SQL Server only)
  • Proposing UNION which fails parsing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard