2ndQuadrant is now part of EDB

Bringing together some of the world's top PostgreSQL experts.

2ndQuadrant | PostgreSQL
Mission Critical Databases
  • Contact us
  • EN
    • FR
    • IT
    • ES
    • DE
    • PT
  • Support & Services
  • Products
  • Downloads
    • Installers
      • Postgres Installer
      • 2UDA – Unified Data Analytics
    • Whitepapers
      • Business Case for PostgreSQL Support
      • Security Best Practices for PostgreSQL
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
  • Postgres Learning Center
    • Webinars
      • Upcoming Webinars
      • Webinar Library
    • Whitepapers
      • Business Case for PostgreSQL Support
      • Security Best Practices for PostgreSQL
    • Blog
    • Training
      • Course Catalogue
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
    • Books
      • PostgreSQL 11 Administration Cookbook
      • PostgreSQL 10 Administration Cookbook
      • PostgreSQL High Availability Cookbook – 2nd Edition
      • PostgreSQL 9 Administration Cookbook – 3rd Edition
      • PostgreSQL Server Programming Cookbook – 2nd Edition
      • PostgreSQL 9 Cookbook – Chinese Edition
    • Videos
    • Events
    • PostgreSQL
      • PostgreSQL – History
      • Who uses PostgreSQL?
      • PostgreSQL FAQ
      • PostgreSQL vs MySQL
      • The Business Case for PostgreSQL
      • Security Information
      • Documentation
  • About Us
    • About 2ndQuadrant
    • 2ndQuadrant’s Passion for PostgreSQL
    • News
    • Careers
    • Team Profile
  • Blog
  • Menu Menu
You are here: Home1 / Blog2 / 2ndQuadrant3 / Oracle to PostgreSQL: ANSI outer join syntax in PostgreSQL
Kirk Roybal

Oracle to PostgreSQL: ANSI outer join syntax in PostgreSQL

April 29, 2020/4 Comments/in 2ndQuadrant, Kirk’s PlanetPostgreSQL, PostgreSQL /by Kirk Roybal

We find ourselves at the third article in the Oracle migration series. This time, we look at those strange operators that modify the WHERE clause criteria in Oracle (+). Like everything else, PostgreSQL has a solution for that.

RIGHT JOIN

Oracle supports, and many developers use, ANSI outer JOIN syntax using operators to the qualifications clause.

Typically, that looks something like this:

SELECT *
FROM person, places
WHERE person.id = places.person_id(+)

The objective of this syntax is a right outer join. In set theory terms, this is the subset including all places, regardless of person.

 

The result of a small sample would look like this:

id last_name first_name id location person_id
1 (NULL) (NULL) 1 Dallas (NULL)
2 Roybal Kirk 2 London 2
3 Riggs Simon 3 Paris 3

This syntax is unsupported in PostgreSQL.

To achieve the same result, you would use the standard SQL syntax for outer joins.

SELECT *
FROM persons
RIGHT JOIN places
ON persons.id = places.person_id;

SQL also provides a clarifying adverb OUTER. This clarifier is completely optional, as any RIGHT JOIN is by definition an OUTER join.

FULL JOIN

Similarly, using the Oracle syntax for a full join does not work in PostgreSQL.

SELECT *
FROM persons, places
WHERE persons.id(+) = places(+);

The objective of this syntax is a full list of persons and places whether a person is associated with a place or not.

 

The result would like like this:

id last_name first_name** id location person_id
1 (NULL) (NULL) 1 Dallas (NULL)
2 Roybal Kirk 2 London 2
3 Riggs Simon 3 Paris 3
4 Andrew Dunstan (NULL) (NULL) (NULL)

Using PostgreSQL syntax, the query would be written thusly:

SELECT *
FROM persons
FULL JOIN places
ON persons.id = places.person_id;

Again, the OUTER keyword is completely optional.

CROSS JOIN

One distinct advantage of the approach to using keywords rather than implicit relationships is that you are not able to accidentally create a cross product.

The syntax:

SELECT *
FROM persons
LEFT JOIN places;

Will result in an error:

ERROR:  syntax error at or near ";"

Indicating that the statement is not complete at the line ending marker “;”.

PostgreSQL will create the cross join product using the ANSI syntax.

SELECT *
FROM persons, places;
id last_name first_name id location person_id
1 Dunstan Andrew 1 Dallas (null)
1 Dunstan Andrew 2 London 2
1 Dunstan Andrew 3 Paris 3
1 Dunstan Andrew 4 Madrid (null)
2 Roybal Kirk 1 Dallas (null)
2 Roybal Kirk 2 London 2
2 Roybal Kirk 3 Paris 3
2 Roybal Kirk 4 Madrid (null)
3 Riggs Simon 1 Dallas (null)
3 Riggs Simon 2 London 2
3 Riggs Simon 3 Paris 3
3 Riggs Simon 4 Madrid (null)
6 Wong Mark 1 Dallas (null)
6 Wong Mark 2 London 2
6 Wong Mark 3 Paris 3
6 Wong Mark 4 Madrid (null)

Which is more likely a coding error than the intentional result.

To get this functionality intentionally, it is recommended to use the CROSS JOIN statement.

SELECT *
FROM persons
CROSS JOIN places;

Thus making it unambiguous what was meant in the statement.

NATURAL JOIN

PostgreSQL supports the NATURAL JOIN syntax, but a bit under protest.

SELECT *
FROM persons
NATURAL JOIN places;

This produces the following result.

id last_name first_name parent_id location person_id
1 Dunstan Andrew (null) Dallas (null)
2 Roybal Kirk 1 London 2
3 Riggs Simon 1 Paris 3

However, this syntax is a problem. For our example, the “id” column in both tables has nothing to do with each other. This join has produced a result, but one with completely irrelevant content.

Additionally, you may have a query that initially presents the correct result, but subsequent DDL statements silently affect.

Consider:

ALTER TABLE person ADD COLUMN places_id bigint;
ALTER TABLE places ADD COLUMN places_id bigint;
ALTER TABLE person ADD COLUMN person_id bigint;

Now what column is the NATURAL JOIN using? The choices are id, places_id, person_id, and all of the above. I’ll leave the answer as an exercise to the reader.

This syntax is a time bomb for your code. Just don’t use it.

Ok, so you’re not convinced. Well, then at least have some sane coding conventions. For the parent table, name the identity column “myparenttable_id”. When referencing it from child relations, use the same name, “myparenttable_id”.  Never name anything “id”, and never make a reference to a column with a different name. Ah, forget it. Just don’t do this.

You may be tempted to disambiguate the previous puzzle by using the USING keyword. That would look like this:

SELECT *
FROM persons
JOIN places
USING (id);

But the USING keyword can only take advantage of exact name matches across tables. Which again, in our example is just dead wrong.

The best practice choice for PostgreSQL is to simply avoid designing tables by coding convention standards.

Summary

These keyword techniques (vs. operators) are also available on Oracle. They are more cross-platform, and less ambiguous. That alone would make them best practices.

Added to that, they expose logical errors when improperly used. For any development in PostgreSQL, we unilaterally recommend using explicit keywords.

Tags: migration
Share this entry
  • Share on Facebook
  • Share on Twitter
  • Share on WhatsApp
  • Share on LinkedIn
4 replies
  1. Marti R.
    Marti R. says:
    April 29, 2020 at 7:52 am

    > Oracle supports, and many developers use, ANSI outer JOIN syntax using operators to the qualifications clause.

    Did you miss the deadline to post on April 1st? This seems to suggest that Oracle’s (+) join syntax is “ANSI outer JOIN syntax”, but the opposite is true: this is Oracle’s proprietary syntax.

    ANSI standard JOIN syntax is LEFT/RIGHT/OUTER JOIN.

    Reply
  2. nortonex
    nortonex says:
    April 30, 2020 at 9:47 pm

    Since when Oracle supports non-ansi full join? As far as I know in 11g it’s not possible to put (+) on both sides of the condition. Is it possible on newer releases?

    Reply
  3. Chris Antognini
    Chris Antognini says:
    May 14, 2020 at 5:52 am

    There is no Oracle Database syntax for a full join that uses the “(+)”. Hence, the following query is not supported by Oracle Database.

    SELECT * FROM persons, places WHERE persons.id(+) = places(+)

    Using FULL (OUTER)

    Reply
    • traveler
      traveler says:
      October 15, 2020 at 2:53 am

      yes,I tried it by PL/SQL,It threw out an error : ORA-01468.

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Get in touch with us!

Recent Posts

  • Random Data
  • Webinar: COMMIT Without Fear – The Beauty of CAMO [Follow Up]
  • Full-text search since PostgreSQL 8.3
  • Random numbers
  • Webinar: Best Practices for Bulk Data Loading in PostgreSQL [Follow Up]

Featured External Blogs

Tomas Vondra's Blog

Our Bloggers

  • Simon Riggs
  • Alvaro Herrera
  • Andrew Dunstan
  • Craig Ringer
  • Francesco Canovai
  • Gabriele Bartolini
  • Giulio Calacoci
  • Ian Barwick
  • Marco Nenciarini
  • Mark Wong
  • Pavan Deolasee
  • Petr Jelinek
  • Shaun Thomas
  • Tomas Vondra
  • Umair Shahid

PostgreSQL Cloud

2QLovesPG 2UDA 9.6 backup Barman BDR Business Continuity community conference database DBA development devops disaster recovery greenplum Hot Standby JSON JSONB logical replication monitoring OmniDB open source Orange performance PG12 pgbarman pglogical PG Phriday postgres Postgres-BDR postgres-xl PostgreSQL PostgreSQL 9.6 PostgreSQL10 PostgreSQL11 PostgreSQL 11 PostgreSQL 11 New Features postgresql repmgr Recovery replication security sql wal webinar webinars

Support & Services

24/7 Production Support

Developer Support

Remote DBA for PostgreSQL

PostgreSQL Database Monitoring

PostgreSQL Health Check

PostgreSQL Performance Tuning

Database Security Audit

Upgrade PostgreSQL

PostgreSQL Migration Assessment

Migrate from Oracle to PostgreSQL

Products

HA Postgres Clusters

Postgres-BDR®

2ndQPostgres

pglogical

repmgr

Barman

Postgres Cloud Manager

SQL Firewall

Postgres-XL

OmniDB

Postgres Installer

2UDA

Postgres Learning Center

Introducing Postgres

Blog

Webinars

Books

Videos

Training

Case Studies

Events

About Us

About 2ndQuadrant

What does 2ndQuadrant Mean?

News

Careers 

Team Profile

© 2ndQuadrant Ltd. All rights reserved. | Privacy Policy
  • Twitter
  • LinkedIn
  • Facebook
  • Youtube
  • Mail
Webinar: Date/Timestamp types in PostgreSQL [Follow Up] Date/Timestamp types in PostgreSQL [Follow Up] How to use Tree Machine Learning model with 2UDA – PostgreSQL and Orange (Part...
Scroll to top
×