The SELECT statement lets you get data from a database. PostgreSQL adds special features to make selecting data easier and more powerful.
SELECT with PostgreSQL-specific features
SELECT [DISTINCT | ALL] column1, column2, ... FROM table_name [WHERE condition] [ORDER BY column [ASC|DESC], ...] [LIMIT number] [OFFSET number] [FETCH {FIRST | NEXT} [number] {ROW | ROWS} ONLY];
DISTINCT removes duplicate rows.
LIMIT and OFFSET help control how many rows you get and where to start.
SELECT DISTINCT city FROM customers;
SELECT * FROM orders ORDER BY order_date DESC LIMIT 5;
SELECT name, price FROM products WHERE price > 100 OFFSET 10 LIMIT 5;
SELECT COUNT(*) FROM employees;
This example creates a fruits table, adds some fruits, then shows how to get unique fruit names and how to get a few fruits sorted by color.
CREATE TABLE fruits (id SERIAL PRIMARY KEY, name TEXT, color TEXT); INSERT INTO fruits (name, color) VALUES ('Apple', 'Red'), ('Banana', 'Yellow'), ('Grape', 'Purple'), ('Apple', 'Green'), ('Banana', 'Yellow'); SELECT DISTINCT name FROM fruits ORDER BY name ASC; SELECT name, color FROM fruits ORDER BY color DESC LIMIT 3 OFFSET 1;
PostgreSQL supports many special functions you can use inside SELECT, like string or date functions.
Use LIMIT with OFFSET to paginate results, like pages in a book.
ORDER BY can sort by multiple columns, each ascending or descending.
SELECT gets data from tables with many options.
PostgreSQL adds features like DISTINCT, LIMIT, OFFSET, and ORDER BY for flexible queries.
Use these features to get exactly the data you want, in the order and amount you need.
Practice
LIMIT clause do in a PostgreSQL SELECT query?Solution
Step 1: Understand the purpose of LIMIT
The LIMIT clause is used to specify the maximum number of rows to return from a query result.Step 2: Compare with other options
Sorting is done by ORDER BY, removing duplicates by DISTINCT, and combining tables by JOIN, so these are not correct.Final Answer:
It restricts the number of rows returned by the query. -> Option BQuick Check:
LIMIT = restrict rows [OK]
- Confusing LIMIT with ORDER BY
- Thinking LIMIT removes duplicates
- Mixing LIMIT with JOIN functionality
customers?Solution
Step 1: Recall correct DISTINCT syntax
In PostgreSQL, DISTINCT comes immediately after SELECT and before the column list.Step 2: Validate each option
SELECT DISTINCT city FROM customers; correctly places DISTINCT after SELECT. Options A, B, and D misuse DISTINCT placement causing syntax errors.Final Answer:
SELECT DISTINCT city FROM customers; -> Option CQuick Check:
SELECT DISTINCT = correct syntax [OK]
- Placing DISTINCT after column names
- Using DISTINCT after FROM clause
- Misordering keywords causing syntax errors
products with columns id, name, and price, what will this query return?SELECT name FROM products ORDER BY price DESC LIMIT 2;
Solution
Step 1: Understand ORDER BY with DESC
ORDER BY price DESC sorts products from highest to lowest price.Step 2: Apply LIMIT 2
LIMIT 2 restricts the output to only the first two rows after sorting, so the two most expensive products.Final Answer:
The two products with the highest prices. -> Option DQuick Check:
ORDER BY DESC + LIMIT = top rows [OK]
- Thinking LIMIT returns lowest prices
- Believing LIMIT causes syntax error with ORDER BY
- Ignoring DESC means descending order
SELECT DISTINCT name, price FROM products LIMIT;
Solution
Step 1: Check LIMIT usage
LIMIT must be followed by a number indicating how many rows to return; here it is missing.Step 2: Validate other parts
DISTINCT can be used with multiple columns, WHERE is optional, and table name is present as products.Final Answer:
LIMIT requires a number to specify how many rows to return. -> Option AQuick Check:
LIMIT needs a number [OK]
- Omitting number after LIMIT
- Thinking DISTINCT can't use multiple columns
- Assuming WHERE is mandatory with LIMIT
employees table with column salary. Which query correctly achieves this in PostgreSQL?Solution
Step 1: Understand OFFSET and LIMIT
OFFSET 2 skips the first two highest salaries, LIMIT 2 returns the next two rows.Step 2: Check ORDER BY direction
ORDER BY salary DESC sorts salaries from highest to lowest, so OFFSET 2 skips top two highest.Step 3: Validate options
SELECT salary FROM employees ORDER BY salary DESC LIMIT 2 OFFSET 2; correctly orders DESC, uses LIMIT 2 and OFFSET 2. SELECT salary FROM employees ORDER BY salary ASC LIMIT 2 OFFSET 3; orders ASC (wrong direction), C misplaces ORDER BY, D places ORDER BY after LIMIT (syntax error).Final Answer:
SELECT salary FROM employees ORDER BY salary DESC LIMIT 2 OFFSET 2; -> Option AQuick Check:
OFFSET skips rows, LIMIT picks next [OK]
- Using ASC instead of DESC for highest values
- Placing ORDER BY after LIMIT
- Confusing OFFSET number with row count
Ready to prove it?
Pass the test and this pattern moves unknown
