Wednesday, January 20, 2016

SQL NATURAL JOIN: Display Records That Have Match on the Other Table

Good day! I have new article for you related to joining tables using SQL query. There are several types of joins that combines two or more tables to retrieve data from multiple tables. You can use different types of joins but I prefer using the most efficient which is the NATURAL JOIN. You might need different joins base on your needs.

Equijoin or inner join, Natural join, Non-equijoin, Outer join, Self join are the different types of joins used in SQL query.

The NATURAL JOIN is used to join two or more tables and return records that has match on the other table. You can also achieve the same using an equijoin but the query is quite long, unlike using natural join, eliminates the conditions.

Let's try to compare the natural join and the equijoin. Below are the syntax.


select * from table1, table2 where table1.column_name=table2.column_name;


select * from table1 natural join table2;

I'll provide examples for you to easily understand. Let's say we have two (2) tables, the STUDENT and GRADE table and we are going to display all students with grades only. We'll exclude those students who were not given grades yet.


Using the natural join, the query will automatically look for the common column on both tables, so make sure that there is only one (1) common attribute to avoid wrong results.

Query Natural Join

select * from student natural join grade;

Query Equijoin

select * from student,grade where student.stud_id=grade.stud_id;

Both queries will have the same result like the one displayed below.

Output for Joins

Hope this helps! Good day! ^_^

SQL NATURAL JOIN: Display Records That Have Match on the Other Table

No comments:

Post a Comment