Junior — Middle
How can you limit the number or select specific records when executing a SELECT statement?
sobes.tech AI
Answer from AI
To limit the number of records or select specific records when executing a SELECT statement in SQL, the following keywords and constructs are used:
- LIMIT — limits the number of rows returned. For example,
SELECT * FROM users LIMIT 10;will return the first 10 records. - WHERE — filters records based on a condition. For example,
SELECT * FROM orders WHERE status = 'completed';will select only completed orders. - OFFSET — skips a specified number of rows before fetching. For example,
SELECT * FROM products LIMIT 10 OFFSET 20;will return 10 records starting from the 21st.
Example:
SELECT id, name FROM employees WHERE department = 'Sales' LIMIT 5;
This query will select the first 5 employees from the sales department.