The LIMIT clause restricts the number of rows returned by the query to a specified number.
Click to reveal answer
beginner
How does OFFSET work in a SQL query?
OFFSET skips a specified number of rows before starting to return rows from the query result.
Click to reveal answer
intermediate
Write a SQL query to get 5 rows starting from the 11th row in a table named 'products'.
SELECT * FROM products LIMIT 5 OFFSET 10;
Click to reveal answer
beginner
Why is LIMIT and OFFSET useful for pagination?
They allow you to fetch a small part of the data at a time, like pages in a book, making it easier to display and manage large data sets.
Click to reveal answer
beginner
What happens if you use LIMIT without OFFSET?
The query returns the first set of rows up to the LIMIT number, starting from the beginning of the result set.
Click to reveal answer
What does the SQL clause OFFSET 20 do?
ASkips the first 20 rows
BLimits the result to 20 rows
CReturns rows starting from the 21st row
DReturns the first 20 rows
✗ Incorrect
OFFSET skips the first 20 rows before returning results.
Which SQL clause limits the number of rows returned?
ALIMIT
BOFFSET
CWHERE
DGROUP BY
✗ Incorrect
LIMIT restricts the number of rows returned by the query.
How do you get rows 11 to 20 from a table named 'users'?
ASELECT * FROM users OFFSET 10 LIMIT 10;
BSELECT * FROM users LIMIT 20 OFFSET 10;
CSELECT * FROM users LIMIT 10 OFFSET 11;
DSELECT * FROM users LIMIT 10 OFFSET 10;
✗ Incorrect
LIMIT 10 OFFSET 10 skips first 10 rows and returns next 10 rows (11 to 20).
What is the default OFFSET if it is not specified?
A1
B0
CLIMIT value
DNo rows are skipped
✗ Incorrect
If OFFSET is not specified, it defaults to 0, meaning no rows are skipped.
Why might you use LIMIT and OFFSET together?
ATo filter rows by a condition
BTo sort rows alphabetically
CTo paginate results by showing a subset of rows
DTo join two tables
✗ Incorrect
LIMIT and OFFSET together help show a specific page of results by selecting a subset of rows.
Explain how LIMIT and OFFSET work together to paginate query results.
Think of pages in a book and how you skip pages to get to the one you want.
You got /3 concepts.
Write a SQL query to fetch the third page of results if each page shows 15 rows.
Page 3 means skip first 30 rows (2 pages * 15 rows).
You got /3 concepts.
Practice
(1/5)
1. What does the LIMIT clause do in a MySQL query?
easy
A. It controls how many rows the query returns.
B. It controls the order of the rows returned.
C. It filters rows based on a condition.
D. It changes the database schema.
Solution
Step 1: Understand the purpose of LIMIT
The LIMIT clause restricts the number of rows returned by a query.
Step 2: Compare with other options
Ordering is done by ORDER BY, filtering by WHERE, and schema changes by DDL commands, not LIMIT.
Final Answer:
It controls how many rows the query returns. -> Option A
Quick Check:
LIMIT = number of rows [OK]
Hint: LIMIT sets max rows returned, like page size [OK]
Common Mistakes:
Confusing LIMIT with ORDER BY
Thinking LIMIT filters rows by condition
Assuming LIMIT changes table structure
2. Which of the following is the correct syntax using the OFFSET keyword to get 10 rows starting from the 7th row in MySQL?
easy
A. SELECT * FROM table LIMIT 6, 10;
B. SELECT * FROM table LIMIT 10 OFFSET 6;
C. SELECT * FROM table OFFSET 6 LIMIT 10;
D. SELECT * FROM table LIMIT 10, 6;
Solution
Step 1: Understand LIMIT and OFFSET syntax
MySQL supports both LIMIT count OFFSET offset and LIMIT offset, count syntax.
Step 2: Identify correct syntax for 10 rows starting at 7th row
OFFSET 6 means skip first 6 rows, then LIMIT 10 means get next 10 rows. SELECT * FROM table LIMIT 10 OFFSET 6; uses this correctly.
Final Answer:
SELECT * FROM table LIMIT 10 OFFSET 6; -> Option B
Quick Check:
LIMIT count OFFSET offset = correct syntax [OK]
Hint: Use LIMIT count OFFSET offset for clarity [OK]
Common Mistakes:
Swapping LIMIT and OFFSET values
Using OFFSET before LIMIT incorrectly
Confusing LIMIT offset,count with LIMIT count OFFSET offset
3. Given the table users with rows numbered by id from 1 to 20, what rows will this query return? SELECT * FROM users ORDER BY id ASC LIMIT 5 OFFSET 10;
medium
A. Rows with id 1 to 5
B. Rows with id 6 to 10
C. Rows with id 11 to 15
D. Rows with id 16 to 20
Solution
Step 1: Understand OFFSET and LIMIT effect on rows
OFFSET 10 skips first 10 rows (id 1 to 10), LIMIT 5 returns next 5 rows.
Step 2: Identify which rows are returned
After skipping 10 rows, rows with id 11 to 15 are returned.
Final Answer:
Rows with id 11 to 15 -> Option C
Quick Check:
OFFSET 10 + LIMIT 5 = rows 11-15 [OK]
Hint: OFFSET skips rows, LIMIT picks next rows [OK]
Common Mistakes:
Counting OFFSET as inclusive row
Mixing up OFFSET and LIMIT order
Assuming OFFSET starts at 1 instead of 0
4. Identify the error in this query intended to get 5 rows starting from the 3rd row: SELECT * FROM products LIMIT OFFSET 3, 5;
medium
A. Syntax error: LIMIT should come before OFFSET without comma.
B. OFFSET cannot be used with LIMIT in this way.
C. No error; query is correct.
D. LIMIT and OFFSET are reversed in order.
Solution
Step 1: Check correct syntax for LIMIT and OFFSET
Correct syntax is either LIMIT count OFFSET offset or LIMIT offset, count without the word OFFSET in the second form.
Step 2: Identify error in given query
The query uses both LIMIT and OFFSET keywords together incorrectly with a comma, causing syntax error.
Final Answer:
Syntax error: LIMIT should come before OFFSET without comma. -> Option A
Quick Check:
LIMIT offset,count or LIMIT count OFFSET offset [OK]
Hint: Use either LIMIT offset,count or LIMIT count OFFSET offset [OK]
Common Mistakes:
Mixing LIMIT and OFFSET keywords with commas
Using OFFSET before LIMIT
Adding OFFSET keyword with comma syntax
5. You want to display page 4 of a product list, showing 15 products per page, ordered by created_at descending. Which query correctly uses LIMIT and OFFSET?
hard
A. SELECT * FROM products ORDER BY created_at DESC LIMIT 45 OFFSET 15;
B. SELECT * FROM products ORDER BY created_at DESC LIMIT 60 OFFSET 15;
C. SELECT * FROM products ORDER BY created_at DESC LIMIT 15, 45;
D. SELECT * FROM products ORDER BY created_at DESC LIMIT 15 OFFSET 45;
Solution
Step 1: Calculate OFFSET for page 4 with 15 items per page
Page 1 shows rows 0-14, page 2 rows 15-29, page 3 rows 30-44, so page 4 starts at offset 45.
Step 2: Write query with LIMIT and OFFSET
LIMIT 15 means 15 rows per page, OFFSET 45 skips first 45 rows. SELECT * FROM products ORDER BY created_at DESC LIMIT 15 OFFSET 45; matches this.
Final Answer:
SELECT * FROM products ORDER BY created_at DESC LIMIT 15 OFFSET 45; -> Option D