Loading topic
FROM clause looks like it should be able to use the other tables in that clause. In PostgreSQL it cannot, unless you mark it LATERAL. This chapter shows what fails, why the rule exists, and which workarounds exist. It also shows where those workarounds stop working, which is th7 min readSELECT queries with WHERE, ORDER BY and LIMIT. It also assumes you know how INNER JOIN and LEFT JOIN behave, how to put a subquery in the FROM clause, and how to give tables aliases. It uses PostgreSQL 12 or later, but LATERAL has been available sin7 min readCROSS JOIN LATERAL queries, including the top-N-per-group pattern, and that you know how LEFT JOIN fills missing columns with NULL. It builds on the previous chapters and doesn't repeat them. The goal here is to keep every row from the left side of a lateral j6 min readCROSS JOIN LATERAL and LEFT JOIN LATERAL ... ON true to return the newest N rows for each group. It assumes you can write SELECT, WHERE, ORDER BY and LIMIT, and that you know inner and left joins, subqueries in FROM, and table aliases. It also assumes you have read th7 min readSELECT, JOIN and LEFT JOIN, and that you have seen CROSS JOIN LATERAL and LEFT JOIN LATERAL ... ON true in the earlier chapters. It uses PostgreSQL 12 or later.8 min readCROSS JOIN LATERAL and LEFT JOIN LATERAL ... ON true queries, as covered earlier in this tutorial. It also assumes you know the basic jsonb operators -> and ->>.7 min readSELECT, WHERE, ORDER BY and LIMIT, and that you know subqueries in FROM and table aliases. It also assumes you have seen CROSS JOIN LATERAL from the earlier chapters. Here you will use a lateral subquery for a different purpose. It doesn't fetch rows fr6 min readLATERAL item refer to the tables beside it, and which ones reject the reference. I assume you can write CROSS JOIN LATERAL and LEFT JOIN LATERAL ... ON true, and that you know how inner and outer joins behave. The examples use two tables:7 min readLATERAL join, which index makes it fast, and how to confirm that in EXPLAIN output.7 min readDISTINCT ON, correlated scalar subqueries, and plain joins with aggregates. It ends with criteria for choosing between them.7 min read