0
0
SQLquery~30 mins

TOP vs LIMIT across databases in SQL - Hands-On Comparison

Choose your learning style9 modes available
Using TOP and LIMIT to Control Query Results
📖 Scenario: You work in a small bookstore's database team. The manager wants to see only a few top-selling books from the sales records to decide which books to promote.
🎯 Goal: Build SQL queries that show how to get the first 3 rows from a sales table using both TOP and LIMIT clauses, demonstrating their use in different databases.
📋 What You'll Learn
Create a table called sales with columns book_title (text) and copies_sold (integer).
Insert 5 rows with exact book titles and copies sold values.
Write a query using TOP to select the first 3 rows ordered by copies_sold descending.
Write a query using LIMIT to select the first 3 rows ordered by copies_sold descending.
💡 Why This Matters
🌍 Real World
Bookstores and many businesses often want to see only the top few records from large datasets to make quick decisions.
💼 Career
Knowing how to limit query results is essential for database developers and analysts to optimize performance and deliver relevant data.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns book_title (text) and copies_sold (integer). Then insert these exact rows: ('The Alchemist', 150), ('1984', 200), ('The Hobbit', 180), ('Pride and Prejudice', 120), ('To Kill a Mockingbird', 170).
SQL
Need a hint?

Use CREATE TABLE to make the table and INSERT INTO with multiple rows to add data.

2
Set up ordering for queries
Create a variable or comment to remind yourself to order the sales by copies_sold in descending order before selecting top rows.
SQL
Need a hint?

Ordering helps to get the top selling books first.

3
Write a query using TOP to get top 3 books
Write a SQL query using SELECT TOP 3 to get the first 3 rows from sales ordered by copies_sold descending.
SQL
Need a hint?

Use TOP 3 right after SELECT to get the first 3 rows.

4
Write a query using LIMIT to get top 3 books
Write a SQL query using LIMIT 3 at the end to get the first 3 rows from sales ordered by copies_sold descending.
SQL
Need a hint?

Use LIMIT 3 at the end of the query to get the first 3 rows.