Close Menu
Java HelpsJava Helps
    What's Hot

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022
    Facebook X (Twitter) Instagram
    Java HelpsJava Helps
    • Home
    • How To
      1. Install
      2. Run
      3. Configure
      4. View All

      How to Install MySQL with phpMyAdmin on Ubuntu?

      December 4, 2022

      Install The Latest IntelliJ IDEA on Linux

      November 28, 2022

      Install The Latest Apache Maven on Linux

      November 27, 2022

      Install The Latest Oracle JDK on Linux

      November 27, 2022

      Run NiFi Cluster in Docker with SSL Enabled

      December 6, 2022

      How to Run Apache NiFi Docker on Mac M1?

      December 1, 2022

      How to Run Apache NiFi on Docker?

      December 1, 2022

      Create A New Maven Project In IntelliJ IDEA

      November 29, 2022

      Things To Do After Installing IntelliJ IDEA

      December 20, 2022

      How to Set Proxy for IntelliJ IDEA?

      December 20, 2022

      How to Set Proxy for Maven?

      December 6, 2022

      How to Create a Fat JAR Using Maven?

      December 5, 2022

      Things To Do After Installing IntelliJ IDEA

      December 20, 2022

      How to Set Proxy for IntelliJ IDEA?

      December 20, 2022

      Manage GitHub Artifact Storage Quota

      December 6, 2022

      Run NiFi Cluster in Docker with SSL Enabled

      December 6, 2022
    • Hello World
      1. Framework
      2. Library
      3. View All

      JPA Hello World! using Hibernate and MySQL

      December 3, 2022

      Jersey 3.x – Hello World!

      December 2, 2022

      Microservices Framework for Java (MSF4J) – Hello World!

      December 1, 2022

      How to Parse PCAP files in Java?

      November 30, 2022

      JPA Hello World! using Hibernate and MySQL

      December 3, 2022

      Jersey 3.x – Hello World!

      December 2, 2022

      Microservices Framework for Java (MSF4J) – Hello World!

      December 1, 2022

      How to Parse PCAP files in Java?

      November 30, 2022
    • More
      • Privacy Policy
        • Java Helps
        • Android Apps
      • Contact US
      • About
    Facebook X (Twitter) Instagram
    Java HelpsJava Helps
    Home » Types of Joins in Presto Explained
    Trino DB
    Trino DB
    TrinoDB

    Types of Joins in Presto Explained

    Gobinath LoganathanBy Gobinath LoganathanDecember 4, 2022Updated:December 4, 2022No Comments5 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    SQL Join is one of the most important and expensive SQL operations and requires deep understanding from database engineers to write efficient SQL queries. From database engineers’ perspective, understanding how the JOIN operation works helps them to optimize them for efficient execution. This article, explains the JOIN operations supported in the open-source distributed computing engine: Presto SQL.

    This article was written when TrinoDB was called Presto SQL. It’s been a while and the supported JOINs might have changed.

    Almost all well-known databases support the following five types of joins: Cross Join, Inner Join, Left Join, Right Join, and Full Join. This article walks through these five types of joins from the end user’s perspective and finally explains two more internal joins which are not obvious to the end user.

    Types of Joins in Presto

    Cross Join

    Cross Join returns the Cartesian product of both tables without any conditions. In other words, every single row from the left table will be joined with every single row from the right table. Cross join is more expensive than other joins as it does Cartesian product between two tables.

    presto:tiny> SELECT * FROM (VALUES 1, 2) t("left"), (VALUES 3, 4) u("right");
    left | right
    ------ -------
    1 | 3
    2 | 3
    1 | 4
    2 | 4
    (4 rows)

    Inner Join

    Inner join joins two tables under a condition. Every row from a table will be joined with every row from the other table only when the given condition is satisfied. In Presto SQL, INNER JOIN, JOIN and , separated tables with a WHERE clause all treated as an inner join. If the query contains comma-separated tables with where clause as given below, it will be read as a Cross Join with a filter applied after the join operation and internally rewritten by a Presto optimizer to the inner join query.

    presto:tiny> SELECT * FROM (VALUES 1, 2) t("left"), (VALUES 1, 1, 2) u("right") 
    WHERE t."left" = u."right";
    left | right
    ------ -------
    1 | 1
    1 | 1
    2 | 2
    (3 rows)

    Left Outer Join

    Like the inner join, the Left Outer join joins every row from the left table with matching rows from the right table. If no match is found for a row from the left table, it will be completed with null values filled in columns from the right table. In Presto SQL the keyword OUTER is optional in the LEFT OUTER JOIN operation. In other words LEFT JOIN and LEFT OUTER JOIN mean the same.

    presto:tiny> SELECT * FROM (VALUES 1, 2) t("left") 
    LEFT OUTER JOIN (VALUES 1, 1) u("right")
    ON t."left" = u."right";
    left | right
    ------ -------
    1 | 1
    1 | 1
    2 | NULL
    (3 rows)

    Right Outer Join

    The right outer join joins every row from the right table with the left table similar to the Left Outer Join. If no match is found for a row from the right table, it will be completed with null values for columns from the left table. In Presto SQL the keyword OUTER is optional in the RIGHT OUTER JOIN operation. In other words RIGHT JOIN and RIGHT OUTER JOIN mean the same.

    presto:tiny> SELECT * FROM (VALUES 1, 2) t("left") 
    RIGHT OUTER JOIN (VALUES 1, 2, 3) u("right")
    ON t."left" = u."right";
    left | right
    ------ -------
    1 | 1
    2 | 2
    NULL | 3
    (3 rows)

    Full Outer Join

    Full Outer Join can be seen as the combination of Left Outer Join and Right Outer Join. It joins every row from each side with matching rows from the other side. If no match is found for a row from the left table, it will be completed with null values for columns from the right table. Similarly, if there is no match for a row from the right side, it will be completed with null values for the columns from the left table. In Presto SQL the keyword OUTER is optional in the FULL OUTER JOIN operation. In other words FULL JOIN and FULL OUTER JOIN mean the same.

    presto:tiny> SELECT * FROM (VALUES 1, 2) t("left") 
    FULL OUTER JOIN (VALUES 1, 3) u("right")
    ON t."left" = u."right";
    left | right
    ------ -------
    1 | 1
    2 | NULL
    NULL | 3
    (3 rows)

    In addition to the above-mentioned join types, there are two join types named: Semi Join and Anti Join. These two joins are used to support some other SQL operations which an end user might not consider as a join.


    Semi Join

    Semi Join evaluates the join criteria as any other join criteria but only returns the records from one table if there is a match. For example, the IN/EXISTS operations are performed using a semi-join internally. There are two variants of semi-join: Left Semi Join and Right Semi Join. As the name suggests, Left Semi Join evaluates the condition and returns rows from the left table if there is a match for those rows. Similarly, the Right Semi Join returns only the matching rows from the right table. The following query using the IN predicate is mapped to a Semi Join operation internally in the Presto database engine.

    presto:tiny> SELECT * FROM (VALUES 1, 2) t(one) WHERE t.one IN (VALUES 1, 1, 3);
    one
    -----
    1
    (1 row)

    As you can see, there are two tables involved in the SQL query but we want the rows only from the left table.


    Anti Join

    Anti Join is another implicit join to return rows from a table when the join criteria do not match. It is similar to Semi Join in returning only rows from one table but the Join criteria are opposite to the Semi Join. For example, the following NOT IN query can be evaluated using an Anti Join.

    presto:tiny> SELECT * FROM (VALUES 1, 2) t(one) WHERE t.one NOT IN (VALUES 1, 1, 3);
    one
    -----
    2
    (1 row)

    Though Anti Join can be seen as a different join type, internally Presto treats it as a Semi Join by negating the join criteria. Not only the Semi Join but any join operation can also be composed using other joins. For example, a left outer join can be rewritten as a right outer join by reordering the tables. The flexibility of rewriting a join into another join allows database engineers to do several cost-based optimizations on the logical plan built from the submitted SQL query. Even for end users, if the database cannot perform cost-based optimizations and join reordering automatically due to statistics limitations and configurations, the knowledge of how to rewrite a naive join operation into an optimal form can save a few minutes to hours of execution time.

    More technical details about how Presto executes these joins internally will be covered in another article. If you have any questions regarding join types or if you like to share your feedback, please comment below.

    big data
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    Run NiFi Cluster in Docker with SSL Enabled

    December 6, 2022

    Presto SQL for Newbies

    December 4, 2022

    Presto SQL: Join Algorithms Explained

    December 4, 2022

    How to Run Apache NiFi Docker on Mac M1?

    December 1, 2022

    How to Run Apache NiFi on Docker?

    December 1, 2022
    Don't Miss
    Configure

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    IntelliJ Idea: the famous IDE for JVM languages including Java, Scala, and Kotlin. If you…

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022

    Manage GitHub Artifact Storage Quota

    December 6, 2022
    Our Picks

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022

    Manage GitHub Artifact Storage Quota

    December 6, 2022
    About Us
    About Us

    Java Helps is the platform to share all about the Java ecosystem. All the sample code available on Java Helps articles are published under Apache 2.0 License. Readers are free to use them according to the Apache 2.0 License.

    "The world is my town; its people my kinsmen."
    -Kaṉiyan Pūngunṟanār

    Email Us: www.javahelps@gmail.com

    Our Picks

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022
    New Comments

      Type above and press Enter to search. Press Esc to cancel.

      Go to mobile version