In the previous article you learnt how to use equi inner join in sql queries to join two tables and retrieve the combined result of both sql database tables. See the example of equi join here… SQL Inner Equi Join Examples. Notice that equi join sql query returned the categoryId column twice because of relation between two tables. Products table also has categoryId column that shows the product belongs to a particular category in categories whose categoryId is also saved in products table as a relational key between both the tables.
You can specify the required column names using sql natural join query.
Natural join query example:
SELECT C.*, P.PRODUCTID, P.PRODUCTNAME FROM CATEGORIES C INNER JOIN PRODUCTS P ON P.CATEGORYID = C.CATEGORYID
This natural join query will return all the columns of categories table and prodcutId and productName from products table.
You can further modify this natural inner join query as per your requirements to visualize the data by specifying the column names of categories table also.
Inner Join Query Example by specifying column names:
SELECT C.CATEGORYID, C.CATEGORYNAME, P.PRODUCTID, P.PRODUCTNAME, P.UNITPRICE FROM CATEGORIES C INNER JOIN PRODUCTS P ON P.CATEGORYID = C.CATEGORYID
This inner join query will display only the specified column names of both the tables.
Also see the examples of Joins in SQL:
Self Join in SQL
Joins using table aliases in SQL Server 2005
Joins In Sql Server 2005
Be the first to rate this post
Tags: sql, sql joins, sql inner join, sql natural join, sql equi join, sql join example
8/23/2008 8:46:52 PM