SQL JOIN : Retrieving Data from many Tables

Monday, June 9, 2008

Joins are created when we display Data in queries that using more than one tables. we can connect a row in one table to another row in other table using the same value of both rows. Usualy this relation represented as Primary Key and Foreign Key relationship.


Joins allow an association between a field in one table or view with a field of the same data type in another table or view. Joins show how the data in one table relates to the data in another table. By using joins, data from two or more tables or queries based on logical relationships among the tables can be retrieved. Joins can be added, edited, or deleted to retrieve the data from the database.


For Example you have two tables called Products and Vendors that contain the following Data:



Data in table Vendors:


VendorID VendorName URL
--------- --------------------------- --------------------------
V0001 Microsoft Corporation http://www.microsoft.com
V0002 Oracle Corporation http://www.oracle.com
V0003 MySQL AB http://www.mysql.com
V0004 PremiumSoft CyberTech Ltd. http://www.navicat.com
V0005 Mentat Technologies http://www.mentattech.com



Data in table Products:


ProductID VendorID Description
---------- --------- -----------------------------------------
SDB0001 V0001 MS SQL Server Database 2005
SDB0002 V0002 Oracle Database 10g Enterprise
SDB0003 V0003 MySQL 5.0 Enterprise Edition
SDBA0001 V0004 Navicat For MySQL Administration
SDBA0002 V0005 Dreamcoder 4.1 For MySQL Enterprise
SDBA0003 V0005 Dreamcoder 4.1 For MySQL Profesional
SDBA0004 V0005 Dreamcoder 4.1 For MS SQL Server Database



Bellow is SQL JOIN Statement example to display ProductID, VendorName, and Description by Joining two tables above




select products.ProductID,vendors.VendorName,products.Description

from vendors, products

where vendors.vendorid=products.vendorid



Query Result:


ProductID VendorName Description
---------- -------------------------- -----------------------------------------
SDB0001 Microsoft Corporation MS SQL Server Database 2005
SDB0002 Oracle Corporation Oracle Database 10g Enterprise
SDB0003 MySQL AB MySQL 5.0 Enterprise Edition
SDBA0001 PremiumSoft CyberTech Ltd. Navicat For MySQL Administration
SDBA0002 Mentat Technologies Dreamcoder 4.1 For MySQL Enterprise
SDBA0003 Mentat Technologies Dreamcoder 4.1 For MySQL Profesional
SDBA0004 Mentat Technologies Dreamcoder 4.1 For MS SQL Server Database





Or you can use this form of SQL JOIN, will produce the same result



select products.ProductID,vendors.VendorName,products.Description

from vendors inner join products

on vendors.vendorid=products.vendorid



Query Result:


ProductID VendorName Description
---------- --------------------------- -----------------------------------------
SDB0001 Microsoft Corporation MS SQL Server Database 2005
SDB0002 Oracle Corporation Oracle Database 10g Enterprise
SDB0003 MySQL AB MySQL 5.0 Enterprise Edition
SDBA0001 PremiumSoft CyberTech Ltd. Navicat For MySQL Administration
SDBA0002 Mentat Technologies Dreamcoder 4.1 For MySQL Enterprise
SDBA0003 Mentat Technologies Dreamcoder 4.1 For MySQL Profesional
SDBA0004 Mentat Technologies Dreamcoder 4.1 For MS SQL Server Database

1 comments:

Myfsy said...

thanks untuk tutorial2 nya, tpi skrg ini saya lagi obok2 cari data tentang gimana menampilkan data tabel dari posgre sql ke blog(blogger.com).
ceritanya saya mau buat pos yg isinya tabel realtime dari database posgre sql
mohon bantuannya, makasih

Recent Comments

Tags Cloud

Blogumulus by Roy Tanck and Amanda Fazani