Infolinks

Breaking

Adsense

Friday, January 29, 2016

SQL JOIN TABLES: Join Multiple Tables in Database Using SQL

Good day! This new article for you is 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. I already created an article for the NATURAL JOIN similar with this article. You might encounter different joins but select what's suitable 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.

It is said that normalization helps to make our database more efficient in searching, storing and managing our data within our database because it controls data redundancy or duplication of data.

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'

Thank you for visiting and hope this helps you. Feel free to leave a comment below for any questions. I got some other articles related to php and mysql here in my blog. Watch out for more upcoming tutorials related to blogging and programming. Good day! ^_^

SQL JOIN TABLES: Join Multiple Tables in Database Using SQL

No comments:

Post a Comment

Adbox