Hello Friends, welcome again, in this post I will discuss about the SQL INNER JOIN, from the last post we already know the use of JOIN, basically to join two tables in a Database based on Primary Key.
So, lets look at our table and then query for INNER JOIN
Table1: Customer
CID Name Designation
222 Whiskey Moderator
444 Onty Member
111 Gaurav Admin
333 Kumaar Admin
555 Rishabh Admin
666 Rose Admin
888 White Member
999 Mikey Member
Table2: Orders
OID Product CID
1 Perfume 666
2 Laptop 444
3 Perfume 999
4 Cell Phone 666
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name
ORDER BY column_name
ORDER BY is optional.
Now For our Query:
SELECT Customer.Name, Orders.Product
FROM Customer
INNER JOIN Orders
ON Customer.CID = Orders.CIDORDER BY Customer.Name
Note: Customer.Name and Customer.Designation means, select the Name and Designation Columns from Customer Table, same for the Orders Table.
Our Result set will have:
Name Products
Mikey Perfume
Onty Laptop
Rose Perfume
Rose Cell Phone
Explanation: In simple words just think that SQL will start checking the Customer.CID with each records of Orders.CID, in case it finds a match, it would extract it to the result-set, and look for next until no records found. Example: SQL will start with Customer.CID(222) and check each records on the Orders Table in the Orders.CID column, if there are any records that matches the values, it would show in the result set, since we have added an ORDER BY on Customer.Name, it would sort the display on the Customer.Name Column. Once its done with 222, it would move to next, that is 444, and perform the same function.
Its little confusing I know, but hang around here try with own examples, and I am sure you can get around the logic of this.
Hope this was informative, and Don't Forget to check the next Post.
Thank You!
So, lets look at our table and then query for INNER JOIN
Table1: Customer
CID Name Designation
222 Whiskey Moderator
444 Onty Member
111 Gaurav Admin
333 Kumaar Admin
555 Rishabh Admin
666 Rose Admin
888 White Member
999 Mikey Member
Table2: Orders
OID Product CID
1 Perfume 666
2 Laptop 444
3 Perfume 999
4 Cell Phone 666
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name
ORDER BY column_name
ORDER BY is optional.
Now For our Query:
SELECT Customer.Name, Orders.Product
FROM Customer
INNER JOIN Orders
ON Customer.CID = Orders.CIDORDER BY Customer.Name
Note: Customer.Name and Customer.Designation means, select the Name and Designation Columns from Customer Table, same for the Orders Table.
Our Result set will have:
Name Products
Mikey Perfume
Onty Laptop
Rose Perfume
Rose Cell Phone
Explanation: In simple words just think that SQL will start checking the Customer.CID with each records of Orders.CID, in case it finds a match, it would extract it to the result-set, and look for next until no records found. Example: SQL will start with Customer.CID(222) and check each records on the Orders Table in the Orders.CID column, if there are any records that matches the values, it would show in the result set, since we have added an ORDER BY on Customer.Name, it would sort the display on the Customer.Name Column. Once its done with 222, it would move to next, that is 444, and perform the same function.
Its little confusing I know, but hang around here try with own examples, and I am sure you can get around the logic of this.
Hope this was informative, and Don't Forget to check the next Post.
Thank You!
0 comments:
Post a Comment