Databases| SQL| MySQL| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Joins 
 

Sometimes you required the data from more than one table. When you select the data from more than one table this is known as Joining. A join is a SQL query that is used to select the data from more than one table or views.

 

Joins

                         

Sometimes you required the data from more than one table. When you select the data from more than one table this is known as Joining. A join is a SQL query that is used to select the data from more than one table or views. When you define multiple tables or views in the FROM clause of a query the MySQL performs a join that linking the rows from multiple tables together.

Types of Joins :

  • INNER Joins
  • OUTER Joins
  • SELF Joins

We are going to describe you the Join with the help of following two tables :

mysql> SELECT * FROM Client;
+------+---------------+----------+
| C_ID | Name          | City     |
+------+---------------+----------+
| 1    | A K Ltd       | Delhi    |
| 2    | V K Associate | Mumbai   |
| 3    | R K India     | Banglore |
| 4    | R S P Ltd     | Kolkata  |
+------+---------------+----------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM Products;
+---------+-------------+------+
| Prod_ID | Prod_Detail | C_ID |
+---------+-------------+------+
| 111     | Monitor     | 1    |
| 112     | Processor   | 2    |
| 113     | Keyboard    | 2    |
| 114     | Mouse       | 3    |
| 115     | CPU         | 5    |
+---------+-------------+------+
5 rows in set (0.00 sec)

INNER Joins

The INNER join is considered as the default Join type. Inner join returns the column values from one row of a table combined with the column values from one row of another table that satisfy the search condition for the join. The general syntax of INNER Join is :
SELECT <column_name1>, <column_name2> FROM <tbl_name> INNER JOIN <tbl_name> ON <join_conditions>

The following example takes all the records from table Client and finds the matching records in table Product. But if no match is found then the record from table Client is not included in the results. But if multiple results are found in table Product with the given condition then one row will be return for each.
Example :

mysql> SELECT * FROM Client
    -> INNER JOIN Products
    -> ON Client.C_ID=Products.C_ID;
+------+---------------+----------+---------+-------------+------+
| C_ID | Name          | City     | Prod_ID | Prod_Detail | C_ID |
+------+---------------+----------+---------+-------------+------+
| 1    | A K Ltd       | Delhi    | 111     | Monitor     | 1    |
| 2    | V K Associate | Mumbai   | 112     | Processor   | 2    |
| 2    | V K Associate | Mumbai   | 113     | Keyboard    | 2    |
| 3    | R K India     | Banglore | 114     | Mouse       | 3    |
+------+---------------+----------+---------+-------------+------+
4 rows in set (0.04 sec)

OUTER Joins

Sometimes when we are performing a Join between the two tables, we need all the records from one table even there is no corresponding record in other table. We can do this with the help of OUTER Join. In other words an OUTER Join returns the all rows that returned by an INNER Join plus all the rows from one table that did not match any row from the other table. Outer Join are divided in two types : LEFT OUTER Join, RIGHT OUTER Join

LEFT OUTER Join

LEFT OUTER Join is used to return all the rows that returned by an INNER Join plus all the rows from first table that did not match with any row from the second table but with the NULL values for each column from second table. The general syntax of LEFT OUTER Join is :
SELECT <column_name1>, <column_name2> FROM <tbl_name> LEFT OUTER JOIN <tbl_name> ON <join_conditions>

In the following example we are selected every row from the Client table which don’t have a match in the Products Table. Example :

mysql> SELECT * FROM CLIENT
    -> LEFT OUTER JOIN Products
    -> ON Client.C_ID=Products.C_ID;
+------+---------------+----------+---------+-------------+------+
| C_ID | Name          | City     | Prod_ID | Prod_Detail | C_ID |
+------+---------------+----------+---------+-------------+------+
| 1    | A K Ltd       | Delhi    | 111     | Monitor     | 1    |
| 2    | V K Associate | Mumbai   | 112     | Processor   | 2    |
| 2    | V K Associate | Mumbai   | 113     | Keyboard    | 2    |
| 3    | R K India     | Banglore | 114     | Mouse       | 3    |
| 4    | R S P Ltd     | Kolkata  | NULL    |             | NULL |
+------+---------------+----------+---------+-------------+------+
5 rows in set (0.00 sec)

In the following example we are using the ORDER BY Clause with the LEFT OUTER Join.

mysql> SELECT * FROM Client
    -> LEFT OUTER JOIN Products
    -> ON Client.C_ID=Products.C_ID
    -> ORDER BY Client.City;
+------+---------------+----------+---------+-------------+------+
| C_ID | Name          | City     | Prod_ID | Prod_Detail | C_ID |
+------+---------------+----------+---------+-------------+------+
| 3    | R K India     | Banglore | 114     | Mouse       | 3    |
| 1    | A K Ltd       | Delhi    | 111     | Monitor     | 1    |
| 4    | R S P Ltd     | Kolkata  | NULL    |             | NULL |
| 2    | V K Associate | Mumbai   | 113     | Keyboard    | 2    |
| 2    | V K Associate | Mumbai   | 112     | Processor   | 2    |
+------+---------------+----------+---------+-------------+------+
5 rows in set (0.08 sec)

In the result of LEFT OUTER Join " R S P Ltd " is included even though it has no rows in the Products table.

RIGHT OUTER Join

RIGHT OUTER Join is much same as the LEFT OUTER JOIN. But RIGHT OUTER Join is used to return all the rows that returned by an INNER Join plus all the rows from second table that did not match with any row from the first table but with the NULL values for each column from first table. The general syntax of RIGHT OUTER Join is :
SELECT <column_name1>, <column_name2> FROM <tbl_name> RIGHT OUTER JOIN <tbl_name> ON <join_conditions>

In the following example we are selected every row from the Products table which don’t have a match in the Client Table. Example :

mysql> SELECT * FROM Client
    -> RIGHT OUTER JOIN Products
    -> ON Client.C_ID=Products.C_ID;
+------+---------------+----------+---------+-------------+------+
| C_ID | Name          | City     | Prod_ID | Prod_Detail | C_ID |
+------+---------------+----------+---------+-------------+------+
| 1    | A K Ltd       | Delhi    | 111     | Monitor     | 1    |
| 2    | V K Associate | Mumbai   | 112     | Processor   | 2    |
| 2    | V K Associate | Mumbai   | 113     | Keyboard    | 2    |
| 3    | R K India     | Banglore | 114     | Mouse       | 3    |
| NULL |               |          | 115     | CPU         | 5    |
+------+---------------+----------+---------+-------------+------+
5 rows in set (0.03 sec)

SELF Join

SELF Join means a table can be joined with itself. SELF Join is useful when we want to compare values in a column to other values in the same column. For creating a SELF Join we have to list a table twice in the FROM clause and assign it a different alias each time. For referring the table we have to use this aliases.

The following example provide you the list of those Clients that belongs to same city of C_ID=1.

mysql> SELECT b.C_ID,b.Name,b.City FROM Client a, Client b
    -> WHERE a.City=b.City AND a.C_ID=1;
+------+----------+-------+
| C_ID | Name     | City  |
+------+----------+-------+
| 1    | A K Ltd  | Delhi |
| 5    | A T Ltd  | Delhi |
| 6    | D T Info | Delhi |
+------+----------+-------+
3 rows in set (0.00 sec)

we can write this SELF JOIN Query in Subquery like this also :

mysql> SELECT * FROM Client
    -> WHERE City=(
    -> SELECT City FROM Client
    -> WHERE C_ID=1);
+------+----------+-------+
| C_ID | Name     | City  |
+------+----------+-------+
| 1    | A K Ltd  | Delhi |
| 5    | A T Ltd  | Delhi |
| 6    | D T Info | Delhi |
+------+----------+-------+
3 rows in set (0.03 sec)

                         

» View all related tutorials
Related Tags: sql mysql c com file files syntax server dynamic interface function object fun io help user remove new state read

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

This very much useful for beginners.

Posted by Godwin on Monday, 07.28.08 @ 18:41pm | #69564

This material is pretty good for the beginners, giving them a comprehensive knowledge about MySQL. I appreciate the work done behind.
Please accept my regards.

Thanks,
Deepak E.

Posted by deepak on Wednesday, 02.20.08 @ 17:08pm | #49212


its very gud site. I learn so much. Thanks guys who build this site

bye

Posted by jitendra on Wednesday, 08.29.07 @ 22:26pm | #24445

I have a query related to joins.

Problem is that i want to access data from two table. from first table all record i want to retrive but seconds table not have all records related to first table record. i used left outer join but it returns NULL value for second table that have no record. Please suggest me what should i do?
ranu

Posted by ranu on Thursday, 05.10.07 @ 17:40pm | #15578

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.