Introduction
Result control helps you get exactly the data you want from a database. It makes sure your answers are clear and useful.
Result control helps you get exactly the data you want from a database. It makes sure your answers are clear and useful.
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column1 ASC|DESC LIMIT number OFFSET number;
WHERE to filter rows based on conditions.ORDER BY to sort the results.LIMIT to restrict how many rows you get.OFFSET to skip a number of rows before returning results.SELECT name, price FROM products WHERE price > 100 ORDER BY price DESC LIMIT 3;
SELECT DISTINCT city FROM customers ORDER BY city ASC;
SELECT * FROM orders WHERE order_date >= '2024-01-01' ORDER BY order_date DESC LIMIT 5 OFFSET 10;
This creates a simple products table, adds some items, then selects the top 3 products costing more than 10, sorted by price from high to low.
CREATE TABLE products ( id SERIAL PRIMARY KEY, name TEXT, price NUMERIC ); INSERT INTO products (name, price) VALUES ('Pen', 1.20), ('Notebook', 2.50), ('Backpack', 45.00), ('Calculator', 15.00), ('Desk Lamp', 22.00); SELECT name, price FROM products WHERE price > 10 ORDER BY price DESC LIMIT 3;
Always use ORDER BY when using LIMIT to get predictable results.
Without WHERE, you get all rows, which might be too much data.
Using DISTINCT helps remove duplicates from your results.
Result control helps you get clear, useful data from your database.
Use filtering, sorting, and limiting to shape your results.
Good result control saves time and avoids confusion.
employees with columns id, name, and salary, what will this query return?SELECT name FROM employees ORDER BY salary DESC LIMIT 3;
SELECT * FROM sales WHERE amount > 100 LIMIT 10 OFFSET 5;
employees(department, name, salary). Which approach best controls the result to achieve this?Pass the test and this pattern moves unknown