SQL Join is the most important thing to learn if you are going to use SQL is Database in your project. Joins are the most used clause in almost every query or procedure. SO, lets check out what is SQL Join and how many types are their and how to use it.
First we will talk about what is an SQL Join.
First we will talk about what is an SQL Join.
SQL Join
A join is a clause uses in a query or procedure to fetch data from multiple tables within a single query. The join will apply on the same column type in multiple tables to fetch relevant data based on the query.
Similarly you can use join to fetch unrelated data with the help of not equals to(!=) operator. That depends on you what you want to fetch and what not.
For example:-
Lets we have 2 table
Table 1: A student table including all basic information about the student.
Table 2: A marks table indicating the overall marks of the students in all subjects.
Lets we assume that a column StudentRollNo is common in bot the tables.
Table 1 Table 2
| StudentRollNo | StudentName |
| 1 | Methu |
| 2 | Ali |
| 3 | Andru |
| 4 | Saif |
| 5 | Pollard |
| 6 | Sam |
| StudentRollNo | Marks |
| 1 | 100 |
| 2 | 99 |
| 3 | 98 |
| 4 | 89 |
| 5 | 88 |
| 6 | 99 |
Now lets apply SQL Join to get data from both tables using StudentRollNo column.
"select
T1.StudentRollNo,
T1.StudentName,
T2.Marks
from
Table1 T1
inner join Table2 T2 on T2.StudentRollNo = T1.StudentRollNo"
Here we applied an inner join(we will talk about inner join later) to get data from both tables. We used an object T1 for Table1 and T2 for Table2 in the query(we will discuss about this later).
Result:-
| StudentRollNo | StudentName | Marks |
| 1 | Methu | 100 |
| 2 | Ali | 99 |
| 3 | Andru | 98 |
| 4 | Saif | 89 |
| 5 | Pollard | 88 |
| 6 | Sam | 99 |
No comments:
Post a Comment