Skip to main content

SQL JOIN | SQL JOINS | SQL JOINS with Examples | SQL JOIN TABLES | SQL JOINS TUTORIAL

SQL JOINS

SQL Joins basically used for mapping the data from more than 1  tables.

For correct out output we need to map correct respective columns.



Following example will demonstrate how to use sql joins
SQL Joins


TYPES of SQL JOINS:

followings are joins type in sql

1.Inner Join
2.Left Join 
3.RIght Join
4.Full outer Join

Table setup for Join example.

Create Table CityMaster_k4Coding
(
id bigint identity(1,1),
CityName varchar(100)
)

Create Table StudentMaster_k4Coding
(
 STudentName Varchar(100),
 Standard varchar(100),
 Admissiondate datetime,
Cityid bigint
)

Insert Into cityMaster_k4coding values ('MUMBAI') -- auto id 1
Insert Into cityMaster_k4coding  values ('DELHI') -- auto id 2
Insert Into cityMaster_k4coding  values ('PUNE') -- auto id 3
Insert Into cityMaster_k4coding  values ('CHENNAI') -- auto id 4


Insert Into  StudentMaster_k4Coding  values ('ASHISH','X',getdate(),1)
Insert Into  StudentMaster_k4Coding  values ('Jay','X',getdate(),2)
Insert Into  StudentMaster_k4Coding  values ('VIJ','X',getdate(),3)
Insert Into  StudentMaster_k4Coding  values ('PAREKH','X',getdate(),4)
Insert Into  StudentMaster_k4Coding  values ('David','X',getdate(),1)
Insert Into  StudentMaster_k4Coding  values ('Rohan','X',getdate(),2)
Insert Into  StudentMaster_k4Coding  values ('Swap','X',getdate(),2)
Insert Into  StudentMaster_k4Coding  values ('Mutthu','X',getdate(),5)






City Master :Sample data

Query :Select * from cityMaster_k4coding

SQL JOINS



Student Master:  Sample data

Query : Select * from StudentMaster_k4Coding

SQL JOINS


Inner Join :

Inner Join is used when matched records need to be extracted.



As per sample data in City we have respective name and  ids as 1.2.3 & 4 where as in Student table we have address ids 1,2,3,4,5.

We we apply inner join query based on city id unique field in this case only matched records will display.

Query Syntax:
Select * from tablename1 S
Inner Join tablename1  C on
S.uniquefield=c.uniquefield 

Note: Here S and C are the table alias and uniquefield is common fields in both table for join.

Select * from StudentMaster_k4Coding S
Inner Join cityMaster_k4coding C on
S.Cityid=c.id


System showing the out put where matching records are available .

In City master table there is no ID=5 so query is not displaying that additional record.

SQL JOINS


Left Join :

Left Join is used when all matched records need to be extracted from left side of the table data.

Similar to Inner Join query 

Select * from StudentMaster_k4Coding S
Left Join cityMaster_k4coding C on
S.Cityid=c.id

here we have StudentMaster_k4Coding  S as left side table, so all the output will display from student table.




As per above screen no data present in City table and we have left join from student table because of that its showing cityid and name as NULL.

RIGHT Join :

Left Join is used when all matched records need to be extracted from left side of the table data.

Similar to LEFT Join query we are doing join with RIGHT keyword

Select * from cityMaster_k4coding C 
RIGHT Join StudentMaster_k4Coding S on
S.Cityid=c.id

here we have StudentMaster_k4Coding  S as left side table, so all the output will display from student table.

SQL JOINS



FULL Outer Join :

FULL outer Join is used when all matched records need to be extracted from both side of the table data.

Similar to RIGHT Join query we are doing join with FULL OUTER JOIN keyword

Select * from cityMaster_k4coding C 
FULL OUTER Join StudentMaster_k4Coding S on
S.Cityid=c.id

here we have StudentMaster_k4Coding  S as left side table, so all the output will display from student table and City table both.

SQL JOINS

Comments

Ads

loading...

Popular posts from this blog

India's second spaceport to be in TN's Thoothukdi: ISRO

India's second spaceport to be in TN's Thoothukdi: ISRO second spaceport for launching small satellites would be located in Tamil Nadu's Thoothukudi district, ISRO chief K. Sivan said on Wednesday. "The Tamil Nadu government has begun acquiring about 2,300 acres of land in Thoothukudi district for our second satellite launch port, ideally located for launching smaller satellites in the earth's lower orbit," the Indian Space Research Organisation Chairman told reporters here. Thoothukudi is about 600 km southeast of Chennai. The country's first spaceport is at Sriharikota in Andhra Pradesh, about 90 km northeast of Chennai. "The new location is ideal for launching smaller satellites of less than 500kg in the sun-synchronous orbit," said Sivan. The space agency will outsource making of the small satellites to the private industry. "The smaller satellites will be initially launched from Sriharikota and subsequently to the...

What Is a VPN?

What Is a VPN? एक वर्चुअल प्राइवेट नेटवर्क या वीपीएन, डिवाइस से नेटवर्क तक इंटरनेट पर एक एन्क्रिप्टेड कनेक्शन है।  एन्क्रिप्टेड कनेक्शन यह सुनिश्चित करने में मदद करता है कि संवेदनशील डेटा सुरक्षित रूप से प्रेषित हो।  यह अनधिकृत लोगों को ट्रैफ़िक पर ईगलडॉपिंग से रोकता है और उपयोगकर्ता को दूरस्थ रूप से कार्य करने की अनुमति देता है।  वीपीएन तकनीक का व्यापक रूप से कॉर्पोरेट वातावरण में उपयोग किया जाता है।  वर्चुअल प्राइवेट नेटवर्क (वीपीएन) कैसे काम करता है?  एक वीपीएन इंटरनेट पर बने एन्क्रिप्टेड कनेक्शन के माध्यम से एक कॉर्पोरेट नेटवर्क का विस्तार करता है।  क्योंकि डिवाइस और नेटवर्क के बीच ट्रैफ़िक एन्क्रिप्ट किया गया है, ट्रैफ़िक निजी रहता है क्योंकि यह यात्रा करता है।  एक कर्मचारी कार्यालय के बाहर काम कर सकता है और फिर भी कॉर्पोरेट नेटवर्क से सुरक्षित रूप से जुड़ सकता है।  यहां तक ​​कि स्मार्टफोन और टैबलेट एक वीपीएन के माध्यम से कनेक्ट हो सकते हैं।  सुरक्षित रिमोट एक्सेस क्या है?  सुरक्षित रिमोट एक्सेस उपयोगकर्ताओं और ...

what is Binary ? बाइनरी क्या है?

बाइनरी क्या है? कंप्यूटर उन शब्दों या संख्याओं को नहीं समझते हैं जो मनुष्य करते हैं।  आधुनिक सॉफ़्टवेयर अंतिम उपयोगकर्ता को इसे अनदेखा करने की अनुमति देता है, लेकिन आपके कंप्यूटर के निम्नतम स्तरों पर, सब कुछ एक द्विआधारी विद्युत संकेत द्वारा दर्शाया जाता है जो दो में से एक स्थिति में पंजीकृत होता है: चालू या बंद।  जटिल डेटा की समझ बनाने के लिए, आपके कंप्यूटर को बाइनरी में एनकोड करना होगा।  बाइनरी एक बेस 2 नंबर सिस्टम है।  बेस 2 का अर्थ है कि केवल दो अंक हैं - 1 और 0 - जो आपके कंप्यूटर को समझने और चालू करने के लिए अनुरूप हैं।  आप शायद बेस 10 - दशमलव प्रणाली से परिचित हैं।  दशांश दस अंकों का उपयोग करता है जो 0 से 9 तक होता है, और फिर दो अंकों की संख्या बनाने के लिए चारों ओर लपेटता है, प्रत्येक अंक पिछले (1, 10, 100, आदि) से दस गुना अधिक मूल्य का होता है।  बाइनरी समान है, जिसमें प्रत्येक अंक पिछले से दो गुना अधिक है। तो कंप्यूटर बाइनरी का उपयोग क्यों करते हैं?  संक्षिप्त उत्तर: हार्डवेयर और भौतिकी के नियम।  आपके कंप्यूटर का प...

Visits



blog counter