Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Sqljoin Mysql Natural Join

Related Tutorials


 
 

Share on Google+Share on Google+

Mysql Natural Join

Advertisement
Mysql Natural Join is a specialization of equi-joins. The join compares all columns in both tables that have the same column-name in both tables that have column name in the joined table.

Mysql Natural Join

     

Mysql Natural Join is a specialization of equi-joins. The join compares all columns in both tables that have the same column-name in both tables that have column name in the joined table. The resulting set include only one column for each pair of the same named column.

Understand with Example

The Tutorial illustrate an example from 'Mysql Natural Join'. To grasp this example, we create a table 'roseindia'. The create table is used to construct a table 'roseindia' with field name and their respective data type.

 

 

Query to Create Table named roseindia:-

mysql> CREATE TABLE roseindia ( 
            -> Empid int(11),          
             ->firstname varchar(30),  
            -> city varchar(30)        
          -> );
Query OK, 0 rows affected (0.05 sec)

Query to insert data into Table named roseindia:-

The insert into add the records values into table 'roseindia'.

mysql>  insert into roseindia values(01,'Girish','Nainital');
Query OK, 1 row affected (0.02 sec)

mysql>  insert into roseindia values(02,'Komal','Merrut');
Query OK, 1 row affected (0.00 sec)

mysql>  insert into roseindia values(03,'Amit','Lucknow');
Query OK, 1 row affected (0.02 sec)

mysql>  insert into roseindia values(04,'Sandeep','Lucknow');
Query OK, 1 row affected (0.03 sec)
mysql>  insert into roseindia values(06,'AA','Delhi');
Query OK, 1 row affected (0.03 sec)
mysql>  insert into roseindia values(09,'BA','Jaipur');
Query OK, 1 row affected (0.03 sec)

Query to view data of  Table named roseindia:-

mysql> select * from roseindia;

Output:-

mysql> select * from roseindia;
+-------+-----------+----------+
| Empid | firstname | city     |
+-------+-----------+----------+
|     1 | Girish    | Nainital |
|     2 | Komal     | Merrut   |
|     3 | Amit      | Lucknow  |
|     4 | Sandeep   | Lucknow  |
|     6 | AA        | Delhi    |
|     9 | BA        | Jaipur   |
+-------+-----------+----------+
6 rows in set (0.00 sec)

Query to Create Table newstrack:-

Now again we create another table 'newstrack'. The createtable is used to construct a table 'newstrack'.

mysql> CREATE TABLE newstrack (
        ->     Empid int(11),          
         ->    firstname varchar(10),  
         ->    email varchar(30) 
          -> );
Query OK, 0 rows affected (0.03 sec)

Query to insert data into Table named newstrack:-

mysql>  insert into newstrack values(01,'Suman','girish@gmail.com');
Query OK, 1 row affected (0.02 sec)

mysql>  insert into newstrack values(02,'Ravi','komal@gmail.com');
Query OK, 1 row affected (0.01 sec)

mysql>  insert into newstrack values(03,'Santosh','Amit@gmail.com');
Query OK, 1 row affected (0.01 sec)

Query to view data of  Table named newstrack:-

mysql> select * from newstrack;

Output:-

+-------+-----------+------------------+
| Empid | firstname | email            |
+-------+-----------+------------------+
|     1 | Suman     | girish.gmail.com |
|     2 | Ravi      | komal.gmail.com  |
|     3 | Santosh   | Amit.gmail.com   |
+-------+-----------+------------------+
3 rows in set (0.00 sec)

Query to join data of  Table's created above using Natural join:-

Now we apply Natural Join to the table 'roseindia' and 'newstrack'. The natural left join in below query returns you the matching records from table 'newstrack' that matches with 'roseindia'.

mysql> SELECT R.FirstName, R.city,R.empid
    ->     FROM roseindia as R
    ->      NATURAL LEFT JOIN newstrack as N;

Output:-

+-----------+----------+-------+
| FirstName | city     | empid |
+-----------+----------+-------+
| Girish    | Nainital |     1 |
| Komal     | Merrut   |     2 |
| Amit      | Lucknow  |     3 |
| Sandeep   | Lucknow  |     4 |
| AA        | Delhi    |     6 |
| BA        | Jaipur   |     9 |
+-----------+----------+-------+
6 rows in set (0.00 sec)
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 16, 2009

Related Tutorials

Discuss: Mysql Natural Join  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
mukesh
May 22, 2012
Gud One

I like this one and appreciate this
DMCA.com