Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly.
Hypersistence Optimizer is that tool!
In this article, we are going to see how the JOIN USING clause works in SQL, and how we can use it to replace the ON condition clause when the columns used to join the two tables have the same name in both the parent and the child tables.
To demonstrate how the JOIN USING clause works, we are going to use the following post and post_comment database tables, which form a one-to-many table relationship via the post_id Foreign Key column in the post_comment table that references the post_id Primary Key column in the post table:
There are the following 3 rows in the parent post table:
If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.
If you design your database schema so that Foreign Key column names match the columns they reference, and the JOIN conditions only check if the Foreign Key column value is equal to the value of its mirroring column in the other table, then you can employ the USING clause.
Otherwise, if the Foreign Key column name differs from the column it references or you want to include a more complex joining condition, then you should use the ON clause instead.