Skip to content

LATERAL

SELECT product, sale
FROM products
LATERAL (SELECT sum(quantity) as sale FROM sales WHERE products.product_id = sales.product_id) as subquery;
| product | sale |
| ----------- | ----------- |
| Product A | 1500 |
| Product B | 1320 |
| Product C | 3430 |

In the specified SQL query, LATERAL is used to allow subquery in FROM clause to refer to columns of preceding FROM items. The query returns a table with columns product and sale. Here, sale is a sum of quantities for each product from the sales table where the product_id matches between the sales and products tables.