What does the PostgreSQL-specific keyword RETURNING do?
The RETURNING clause in PostgreSQL allows you to return values from rows that are inserted, updated, or deleted, without needing a separate SELECT query. It is often used with INSERT, UPDATE, or DELETE statements to get the affected rows immediately.
Click to reveal answer
intermediate
How does the SELECT DISTINCT ON clause work in PostgreSQL?
SELECT DISTINCT ON (expression) returns the first row for each unique value of the expression. It is useful to get one row per group, unlike standard DISTINCT which removes duplicate rows entirely.
Click to reveal answer
intermediate
What is the purpose of the FILTER clause in aggregate functions in PostgreSQL?
The FILTER clause lets you apply a condition inside an aggregate function to include only rows that meet that condition. For example, COUNT(*) FILTER (WHERE status = 'active') counts only rows where status is 'active'.
Click to reveal answer
advanced
Explain the use of the WITH ORDINALITY clause in PostgreSQL.
WITH ORDINALITY adds a column that numbers the rows returned by a set-returning function or a table expression. This is useful when you want to keep track of the order of rows generated by functions like unnest().
Click to reveal answer
advanced
What does the TABLESAMPLE clause do in a PostgreSQL SELECT query?
TABLESAMPLE allows you to retrieve a random sample of rows from a table. It is useful for testing or analysis when you don't need the full dataset. Different sampling methods like BERNOULLI or SYSTEM can be specified.
Click to reveal answer
What does SELECT DISTINCT ON (column) return in PostgreSQL?
AThe first row for each unique value in the specified column
BAll rows with duplicates removed from the entire result
COnly rows where the column is not null
DRows sorted by the specified column
✗ Incorrect
SELECT DISTINCT ON (column) returns the first row for each unique value in that column, unlike standard DISTINCT which removes all duplicate rows.
Which clause allows you to get the affected rows immediately after an INSERT in PostgreSQL?
AWITH ORDINALITY
BRETURNING
CFILTER
DTABLESAMPLE
✗ Incorrect
The RETURNING clause returns rows affected by INSERT, UPDATE, or DELETE statements immediately.
What does the FILTER clause do inside an aggregate function?
AFilters columns to include in aggregation
BFilters the final result set after aggregation
CFilters rows before aggregation based on a condition
DFilters the query plan for optimization
✗ Incorrect
FILTER applies a condition inside an aggregate function to include only rows that meet that condition during aggregation.
What is the purpose of WITH ORDINALITY in PostgreSQL?
AAdds a row number column to rows returned by a set-returning function
BFilters rows based on ordinal values
CSorts rows in ascending order
DLimits the number of rows returned
✗ Incorrect
WITH ORDINALITY adds a column numbering the rows from set-returning functions, helping track their order.
Which sampling methods can be used with TABLESAMPLE in PostgreSQL?
ARANDOM and SEQUENTIAL
BHASH and RANGE
CFULL and PARTIAL
DBERNOULLI and SYSTEM
✗ Incorrect
TABLESAMPLE supports sampling methods like BERNOULLI and SYSTEM to retrieve random rows.
Describe how you would use the RETURNING clause in PostgreSQL and why it is useful.
Think about how to get data from changed rows without extra queries.
You got /4 concepts.
Explain the difference between DISTINCT and DISTINCT ON in PostgreSQL SELECT queries.
Focus on how each handles duplicates and grouping.
You got /4 concepts.
Practice
(1/5)
1. What does the LIMIT clause do in a PostgreSQL SELECT query?
easy
A. It combines rows from two tables.
B. It restricts the number of rows returned by the query.
C. It removes duplicate rows from the result.
D. It sorts the rows in ascending order.
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 B
Quick Check:
LIMIT = restrict rows [OK]
Hint: LIMIT caps rows returned, like a page size [OK]
Common Mistakes:
Confusing LIMIT with ORDER BY
Thinking LIMIT removes duplicates
Mixing LIMIT with JOIN functionality
2. Which of the following is the correct syntax to select distinct city names from a table called customers?
easy
A. SELECT city FROM DISTINCT customers;
B. SELECT city DISTINCT FROM customers;
C. SELECT DISTINCT city FROM customers;
D. SELECT city FROM customers DISTINCT;
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 C
Quick Check:
SELECT DISTINCT = correct syntax [OK]
Hint: DISTINCT always follows SELECT keyword [OK]
Common Mistakes:
Placing DISTINCT after column names
Using DISTINCT after FROM clause
Misordering keywords causing syntax errors
3. Given the table products with columns id, name, and price, what will this query return?
SELECT name FROM products ORDER BY price DESC LIMIT 2;
medium
A. The two products with the lowest prices.
B. An error because LIMIT cannot be used with ORDER BY.
C. All products sorted by price descending.
D. The two products with the highest prices.
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 D
Quick Check:
ORDER BY DESC + LIMIT = top rows [OK]
Hint: ORDER BY DESC + LIMIT picks top items [OK]
Common Mistakes:
Thinking LIMIT returns lowest prices
Believing LIMIT causes syntax error with ORDER BY
Ignoring DESC means descending order
4. Identify the error in this PostgreSQL query:
SELECT DISTINCT name, price FROM products LIMIT;
medium
A. LIMIT requires a number to specify how many rows to return.
B. DISTINCT cannot be used with multiple columns.
C. SELECT must include WHERE clause when using LIMIT.
D. The table name is missing.
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 A
Quick Check:
LIMIT needs a number [OK]
Hint: Always put a number after LIMIT [OK]
Common Mistakes:
Omitting number after LIMIT
Thinking DISTINCT can't use multiple columns
Assuming WHERE is mandatory with LIMIT
5. You want to get the third and fourth highest salaries from the employees table with column salary. Which query correctly achieves this in PostgreSQL?
hard
A. SELECT salary FROM employees ORDER BY salary DESC LIMIT 2 OFFSET 2;
B. SELECT salary FROM employees ORDER BY salary ASC LIMIT 2 OFFSET 3;
C. SELECT DISTINCT salary FROM employees LIMIT 2 OFFSET 3 ORDER BY salary DESC;
D. SELECT salary FROM employees LIMIT 2 OFFSET 2 ORDER BY salary DESC;
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 A
Quick Check:
OFFSET skips rows, LIMIT picks next [OK]
Hint: Use OFFSET to skip, LIMIT to pick rows [OK]
Common Mistakes:
Using ASC instead of DESC for highest values
Placing ORDER BY after LIMIT
Confusing OFFSET number with row count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.