INNER JOIN in SQL Server

JOIN is the keyword or Command which is used to link 2 or more tables to retrieve data.


INNER JOIN is the the most commonly used JOIN in SQL SERVER. INNER JOIN will retrieve (fetch) the data only if the criteria is satisfied in both the tables.

Syntax for INNER JOIN is:

SELECT <A.Column Names >, <B.Columm Names>
FROM <Left side Table Name> <A>
INNER JOIN  <Right side Table Name > <B>
ON <A.matching column Name> = <B.Matching Column Name>

In the above syntax A indicates the alias name for left side table Name
B indicates the alias name for right side table Name.

Example:

Consider 2 tables STUDENT and LANGUAGES .

STUDENT table holds STUDENT details like student id,name, class and student second language. LANGUAGES  table holds all the languages that are offered in that school.

SECOND_LANG_ID in STUDENT will hold the LANG_ID value of LANGUAGES table.

Below screen shot represents the data in STUDENT and LANGUAGES table.


















Second language information of a student can be fetched by joining STUDENT table with LANGUAGES table.

To get the student details along with their language information following query can be used:


Query:

SELECT S.STUDENT_ID,S.NAME,S.CLASS,L.NAME
FROM STUDENT S
INNER JOIN LANGUAGES L
ON S.SECOND_LANG_ID =L.LANG_ID

Result Set:



























Result Set contains the Student information and their second language also.

In the above result set STUDENT_ID=8 information is not fetched since this student Language Id is 20. Language ID =20 is not found in LANGUAGES table.
Since NO matching record found for Student_id=8 in both the tables. , this record is not retrieved.

To know more about other types of JOINs in SQL SERVER refer the below link:

This entry was posted in , . Bookmark the permalink.

Leave a reply