SQL Joins Introduction
The "Customers" table:
Note that the "Order_Id" column is the Primary Key in the "Orders" table and that the "Cust_Id " column refers to the Customers in the "Customers" table without using their names.
Notice that the relationship between the two tables above is the "Cust_Id" column.
Different SQL JOINs:
Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.
The JOIN Clause is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. Tables in a database are often related to each other with keys.
A Primary Key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
A Primary Key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
The "Customers" table:
Cust_Id
|
Cust_Name
|
Cust_Location
|
Cust_1234
|
ABC Soft Pvt Ltd.
|
East
|
Cust_3456
|
CDE Info Pvt Ltd
|
West
|
Cust_4466
|
FFDD Infor Pvt Ltd.
|
Mid-West
|
Cust_1122
|
EFC Tech Pvt Ltd.
|
South
|
Cust_6688
|
HHFF Tech Pvt Ltd.
|
Mid-South
|
Cust_5566
|
GHI Services Pvt Ltd.
|
North
|
Note that the "Cust_Id" column is the Primary Key in the "Customers" table. This means that two rows cannot have the same Cust_Id. The Cust_Id distinguishes two Customers even if they have the same name.
The "Orders" table:
Order_Id
|
Sales_Period
|
Cust_Id
|
Sales
|
ABC_1234
|
Q1-2014
|
Cust_1234
|
40474
|
BCD_2345
|
Q2-2014
|
Cust_2345
|
44321
|
CDE_3456
|
Q3-2014
|
Cust_3456
|
19906
|
DEF_4567
|
Q4-2014
|
Cust_4567
|
34943
|
EFG_1122
|
Q1-2015
|
Cust_1122
|
22114
|
FGH_3344
|
Q2-2015
|
Cust_3344
|
41364
|
GHI_5566
|
Q3-2015
|
Cust_5566
|
16157
|
IJK_7788
|
Q4-2015
|
Cust_7788
|
36054
|
Notice that the relationship between the two tables above is the "Cust_Id" column.
Different SQL JOINs:
Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.
JOIN or INNER JOIN:
Return rows when there is at least one match in both tables.
Return rows when there is at least one match in both tables.
LEFT OUTER JOIN:
Return all rows from the left table, even if there are no matches in the right table.
Return all rows from the left table, even if there are no matches in the right table.
RIGHT OUTER JOIN:
Return all rows from the right table, even if there are no matches in the left table.
Return all rows from the right table, even if there are no matches in the left table.
FULL OUTER JOIN:
Return rows when there is a match in one of the tables.
Thanks, Tamatam
Return rows when there is a match in one of the tables.
Thanks, Tamatam
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.