Natural Joins and the USING Keyword : Using « Join « SQL / MySQL






Natural Joins and the USING Keyword

     
mysql>
mysql> CREATE TABLE customer (
    ->   id int(11) default NULL,
    ->   first_name varchar(30) default NULL,
    ->   surname varchar(40) default NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO customer VALUES (1, 'Yvonne', 'Clegg');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO customer VALUES (2, 'Johnny', 'Chaka-Chaka');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO customer VALUES (3, 'Winston', 'Powers');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO customer VALUES (4, 'Patricia', 'Mankunku');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> CREATE TABLE sales (
    ->   code int(11) default NULL,
    ->   sales_rep int(11) default NULL,
    ->   customer int(11) default NULL,
    ->   value int(11) default NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO sales VALUES (1, 1, 1, 2000);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO sales VALUES (2, 4, 3, 250);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO sales VALUES (3, 2, 3, 500);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO sales VALUES (4, 1, 4, 450);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO sales VALUES (5, 3, 1, 3800);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO sales VALUES (6, 1, 2, 500);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> CREATE TABLE sales_rep (
    ->   employee_number int(11) default NULL,
    ->   surname varchar(40) default NULL,
    ->   first_name varchar(30) default NULL,
    ->   commission tinyint(4) default NULL,
    ->   date_joined date default NULL,
    ->   birthday date default NULL
    -> );
mysql>
mysql> INSERT INTO sales_rep VALUES (1, 'Rive', 'Sol', 10,  '2000-02-15', '1976-03-18');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO sales_rep VALUES (2, 'Gordimer', 'Charlene', 15, '1998-07-09', '1958-11-30');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO sales_rep VALUES (3, 'Serote', 'Mike', 10, '2001-05-14', '1971-06-18');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO sales_rep VALUES (4, 'Rive', 'Mongane', 10, '2002-11-23', '1982-01-04');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> ALTER TABLE sales CHANGE customer id INT;
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> SELECT first_name,surname,value FROM customer NATURAL JOIN sales;
+------------+-------------+-------+
| first_name | surname     | value |
+------------+-------------+-------+
| Yvonne     | Clegg       |  2000 |
| Winston    | Powers      |   250 |
| Winston    | Powers      |   500 |
| Patricia   | Mankunku    |   450 |
| Yvonne     | Clegg       |  3800 |
| Johnny     | Chaka-Chaka |   500 |
+------------+-------------+-------+
6 rows in set (0.00 sec)

mysql>
mysql> SELECT first_name,surname,value FROM customer INNER JOIN sales ON customer.id=sales.id;
+------------+-------------+-------+
| first_name | surname     | value |
+------------+-------------+-------+
| Yvonne     | Clegg       |  2000 |
| Winston    | Powers      |   250 |
| Winston    | Powers      |   500 |
| Patricia   | Mankunku    |   450 |
| Yvonne     | Clegg       |  3800 |
| Johnny     | Chaka-Chaka |   500 |
+------------+-------------+-------+
6 rows in set (0.00 sec)

mysql>
mysql> drop table customer;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table sales;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table sales_rep;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
  








Related examples in the same category

1.Using temp table during table join
2.Using a Join to Fill in Holes in a List
3.Produce the same results by using a USING clause to qualify the join
4.Specify the necessary join conditions in an ON or USING clause.
5.Create the same report using the JOIN function in the following command.
6.Using function in where clause during table joining
7.Using table alias in table join
8.Using key word USING, in which the common linking field is specified.