What is the role of the 'LIMIT' clause in SQL?
Instruction: Using PostgreSQL, return the five customers with the smallest customer_id values.
Use customers(customer_id INTEGER PRIMARY KEY). For the example, it contains IDs 1 through 7. Return customer_id in ascending order and explain what happens when fewer than five rows exist.
Updated
Official Answer
For PostgreSQL, define “first” using a stable ordering:
SELECT customer_id
FROM customers
ORDER BY customer_id
LIMIT 5;
Given unique IDs 1 through 7, this returns 1, 2, 3, 4, 5. With fewer than five customers it returns all available rows; an empty table returns no rows. Without ORDER BY, which five rows appear is unspecified.
LIMIT caps returned rows, not necessarily all work performed. Sorting or aggregation may still require processing many input rows. The appropriate index and query plan determine the execution cost.