0
0
PostgreSQLquery~5 mins

SELECT with PostgreSQL-specific features - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
Which clause allows you to get the affected rows immediately after an INSERT in PostgreSQL?
AWITH ORDINALITY
BRETURNING
CFILTER
DTABLESAMPLE
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
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
Which sampling methods can be used with TABLESAMPLE in PostgreSQL?
ARANDOM and SEQUENTIAL
BHASH and RANGE
CFULL and PARTIAL
DBERNOULLI and SYSTEM
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.