• Home
  • Blog

Archieven

  • mei 2025
  • april 2025
  • oktober 2024
  • september 2024
  • september 2023
  • maart 2022
  • maart 2021
  • september 2020
  • september 2019
  • februari 2019
  • september 2018
  • september 2016
  • september 2015
Davici
  • Home
  • Blog

PostgreSQL powerful LATERAL join

PostgreSQL supports the SQL join type: LATERAL. Useful for creating advanced query’s.

What is a LATERAL join?

In the PostgreSQL documentation:

Subqueries appearing in FROM can be preceded by the key word LATERAL. This allows them to reference columns provided by preceding FROM items. (Without LATERAL, each subquery is evaluated independently and so cannot cross-reference any other FROM item.)

Table functions appearing in FROM can also be preceded by the key word LATERAL, but for functions the key word is optional; the function’s arguments can contain references to columns provided by preceding FROM items in any case.

A LATERAL item can appear at top level in the FROM list, or within a JOIN tree. In the latter case it can also refer to any items that are on the left-hand side of a JOIN that it is on the right-hand side of.

When a FROM item contains LATERAL cross-references, evaluation proceeds as follows: for each row of the FROM item providing the cross-referenced column(s), or set of rows of multiple FROM items providing the columns, the LATERAL item is evaluated using that row or row set’s values of the columns. The resulting row(s) are joined as usual with the rows they were computed from. This is repeated for each row or set of rows from the column source table(s).

Example

We have a table with orders and want to join a related table orderlines to find out the lowest and highest status of the order lines.

SELECT orders.orderid, statuslow, statushigh 
FROM orders
JOIN LATERAL 
	(
	SELECT orderlinestatus.name AS statuslow, orderlines.orderid 
	FROM orderlines JOIN orderlinestatus USING(orderlinestatusid) WHERE orderlines.orderid = orders.orderid 
	GROUP BY orderlines.orderid, orderlinestatus.name, orderlinestatus.weight
	ORDER BY orderlinestatus.weight LIMIT 1
	) 
	AS subquery_statuslow ON (subquery_statuslow.orderid=orders.orderid),
JOIN LATERAL 
	(
	SELECT orderlinestatus.name AS statushigh, orderlines.orderid 
	FROM orderlines JOIN orderlinestatus USING(orderlinestatusid) WHERE orderlines.orderid = orders.orderid 
	GROUP BY orderlines.orderid, orderlinestatus.name, orderlinestatus.weight
	ORDER BY orderlinestatus.weight DESC LIMIT 1
	) 
	AS subquery_statushigh ON (subquery_statushigh.orderid=orders.orderid)

Notice that with the LATERAL join keyword we can reference the orders table in a subselect query which is not possible with standard joins. Depending on the use case this can increase performance. Otherwise we would have need to join the orders and orderlines table and do the aggregation at the end. For large tables this requires a lot of temporarily memory. With the lateral join the work data is reduced to one row in the subselect query.

More information on the PostgreSQL documentation website: https://www.postgresql.org/docs/9.3/static/queries-table-expressions.html

Tags

.NET Azure C# CTO Database DevOps Docker EDI Git HA IT developement director Microservices PostgreSQL SFTP Softwareontwikkeling Visual Studio Windows

Recent Posts

  • SFTP Server with SSH, Chroot, and authorized_keys

  • Technical Debt: een strategische aanpak voor duurzame softwareontwikkeling

  • How to set timezone in a Docker Windows Container

  • How to install fonts in a Docker Windows Container

  • Why WebView2 Applications Face Issues in the Program Files Folder (And How to Fix Them)

Tags: PostgreSQL

Contact

Breda, Noord-Brabant, Nederland
info@davici.nl
K.v.K. 20099979
Twitter
LinkedIn

OVERIGE

  • Algemene Voorwaarden
  • Privacybeleid
  • Cookiebeleid

Copyright © 2025 — Davici. Alle rechten voorbehouden.

Designed by WPZOOM

Beheer toestemming
Om de beste ervaringen te bieden, gebruiken wij technologieën zoals cookies om informatie over je apparaat op te slaan en/of te raadplegen. Door in te stemmen met deze technologieën kunnen wij gegevens zoals surfgedrag of unieke ID's op deze site verwerken. Als je geen toestemming geeft of uw toestemming intrekt, kan dit een nadelige invloed hebben op bepaalde functies en mogelijkheden.
Functioneel Altijd actief
De technische opslag of toegang is strikt noodzakelijk voor het legitieme doel het gebruik mogelijk te maken van een specifieke dienst waarom de abonnee of gebruiker uitdrukkelijk heeft gevraagd, of met als enig doel de uitvoering van de transmissie van een communicatie over een elektronisch communicatienetwerk.
Voorkeuren
De technische opslag of toegang is noodzakelijk voor het legitieme doel voorkeuren op te slaan die niet door de abonnee of gebruiker zijn aangevraagd.
Statistieken
De technische opslag of toegang die uitsluitend voor statistische doeleinden wordt gebruikt. De technische opslag of toegang die uitsluitend wordt gebruikt voor anonieme statistische doeleinden. Zonder dagvaarding, vrijwillige naleving door je Internet Service Provider, of aanvullende gegevens van een derde partij, kan informatie die alleen voor dit doel wordt opgeslagen of opgehaald gewoonlijk niet worden gebruikt om je te identificeren.
Marketing
De technische opslag of toegang is nodig om gebruikersprofielen op te stellen voor het verzenden van reclame, of om de gebruiker op een site of over verschillende sites te volgen voor soortgelijke marketingdoeleinden.
Beheer opties Beheer diensten Beheer {vendor_count} leveranciers Lees meer over deze doeleinden
Bekijk voorkeuren
{title} {title} {title}