Equijoin or inner join, Natural join, Non-equijoin, Outer join, Self join are the different types of joins used in SQL query.
But the more we normalize our tables, the more complicated our queries would be. So I have here an example wherein you need to join three (3) tables to display needed data.
We have here the STUDENT table, GRADE table and the SUBJECT table. See illustration below:
STUDENT |
SUBJECT |
GRADE |
Based on the tables with data above, we need to display the student's name, their subjects with their corresponding grades. So to achieved this, we need to used the natural join. NATURAL JOIN is used to combine 2 or more tables based on a common column. It will display records with match on the other table only. If you want to display records with or without match on the other table, you need to use a different join which is the LEFT OR RIGHT JOIN.
SQL Query Natural Join
SELECT stud_name,subject_name,grade FROM student natural join grade natural join subject
Output Natural Join
If you want to display the grades of students on English subject only, then you need to use a WHERE clause to achieve it. The WHERE clause is used to limit the rows affected by a certain query.
SQL Query Natural join
SELECT stud_name,subject_name,grade FROM student natural join grade natural join subject where subject_name='English'
I'll give you another scenario using a WHERE clause and comparison operator. Let's say you want to display a listing whose grade is greater 89. That is 90 and above grades. So we will be using the comparison operator which is the greater than (>).
SELECT stud_name,subject_name,grade FROM student natural join grade natural join subject where grade>'89'
No comments:
Post a Comment