Skip to content
Advertisement

SQL JOIN: add custom constraint in JOIN clause

I want to select the preferred language if it exists and the default language otherwise.

The v_program_items table looks as :

I use two JOIN (on the same table) and one CASE/WHEN.
Is it possible to use only one JOIN ?

Advertisement

Answer

The joins you have are fine and perform very well with an index – should be a UNIQUE index (or PK):

Use COALESCE in the SELECT list, like “PM 77-1” suggested in a comment:

In Postgres 11, and only if your table v_program_items is big, consider a covering index:

Related:

Either way, while selecting only a single row (or a few), lowly correlated subqueries should be even faster. Simple, too:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement