0
0
PostgreSQLquery~5 mins

LATERAL subqueries in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a LATERAL subquery in PostgreSQL?
A LATERAL subquery is a special kind of subquery that can refer to columns of tables that appear before it in the FROM clause. It allows the subquery to use values from preceding tables, enabling row-by-row processing.
Click to reveal answer
intermediate
How does a LATERAL subquery differ from a regular subquery in the FROM clause?
A regular subquery in the FROM clause cannot access columns from tables listed before it. A LATERAL subquery can access those columns, making it possible to join each row of the preceding table with a set of rows returned by the subquery.
Click to reveal answer
intermediate
Write a simple example of a LATERAL subquery to find the top 2 orders per customer.
SELECT c.customer_id, o.order_id, o.amount FROM customers c, LATERAL (SELECT * FROM orders WHERE customer_id = c.customer_id ORDER BY amount DESC LIMIT 2) o;
Click to reveal answer
advanced
Why is LATERAL useful for queries involving functions returning sets?
LATERAL allows you to call set-returning functions for each row of a preceding table, using that row's data as input. This enables dynamic and row-specific function calls within a query.
Click to reveal answer
intermediate
Can you use LATERAL with JOIN syntax? If yes, how?
Yes, you can use LATERAL with JOIN. For example: SELECT * FROM customers c LEFT JOIN LATERAL (SELECT * FROM orders WHERE customer_id = c.customer_id LIMIT 1) o ON true; This joins each customer with their first order if any.
Click to reveal answer
What does the LATERAL keyword allow a subquery to do?
AAccess columns from preceding tables in the FROM clause
BRun faster than regular subqueries
CReturn only one row
DExecute outside the main query
Which of the following is a valid use of LATERAL in PostgreSQL?
ASELECT * FROM table1 WHERE LATERAL (SELECT * FROM table2);
BSELECT * FROM table1, LATERAL (SELECT * FROM table2 WHERE table2.id = table1.id);
CSELECT LATERAL * FROM table1;
DLATERAL SELECT * FROM table1;
What happens if you omit LATERAL when the subquery needs to reference a preceding table?
AThe query will fail with an error
BThe subquery will run independently without error
CThe subquery will automatically become LATERAL
DThe query will return empty results
Which JOIN type can be combined with LATERAL subqueries?
AJOIN cannot be used with LATERAL
BLEFT JOIN only
CINNER JOIN only
DAny JOIN type including INNER, LEFT, RIGHT
In the query: SELECT c.id, o.* FROM customers c, LATERAL (SELECT * FROM orders WHERE customer_id = c.id LIMIT 1) o; what does the subquery do?
AReturns all orders for all customers
BReturns orders unrelated to customers
CReturns one order per customer using the customer's id
DReturns no rows
Explain in your own words what a LATERAL subquery is and why it is useful.
Think about how subqueries normally work and what LATERAL changes.
You got /3 concepts.
    Describe a scenario where using a LATERAL subquery would solve a problem that a regular subquery cannot.
    Consider queries that need to join each row with a dynamic set of rows.
    You got /3 concepts.