Documents

SQL Reference

Description
it is book on sql
Categories
Published
of 30
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
  The SQL Joins  clause is used to combine records from two or more tables in a database. A JOIN is ameans for combining fields from two tables by using values common to each.Consider the following two tables, (a) CUSTOMERS table is as follows: | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 2 | Delhi | 100.00 | | 3 | !a shi! | 23 | K#$a | 2000.00 | | % | &hai$ali | 2 | M mbai | '00.00 | |  | (a)di! | 2* | h#,al | 00.00 | | ' | K#mal | 22 | M | %00.00 | | * | M // | 2% | Ind#)e | 10000.00 | +----+----------+-----+-----------+----------+ (b) Another table is ORDERS as follows: +-----+---------------------+-------------+--------+|ID | DAE | &SMER4ID | AMN | +-----+---------------------+-------------+--------+| 102 | 2005-10-0 00600600 | 3 | 3000 | | 100 | 2005-10-0 00600600 | 3 | 100 | | 101 | 2005-11-20 00600600 | 2 | 1'0 | | 103 | 200-0-20 00600600 | % | 20'0 | +-----+---------------------+-------------+--------+ Now, let us join these two tables in our SELECT statement as follows: S7L8 SELE& ID9 NAME9 AGE9 AMN  :RM &SMERS9 RDERS ;(ERE &SMERS.ID < RDERS.&SMER4ID= This would produce the following result: +----+----------+-----+--------+| ID | NAME | AGE | AMN | +----+----------+-----+--------+| 3 | !a shi! | 23 | 3000 | | 3 | !a shi! | 23 | 100 | | 2 | Khilan | 2 | 1'0 | | % | &hai$ali | 2 | 20'0 | +----+----------+-----+--------+ Here, it is noticeable that the join is performed in the WHERE clause. Several operators can be used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they can all be used to join tables.However, the most common operator is the equal symbol. SQL Join Types:  There are different types of joins available in SQL: ã INNER JOIN: returns rows when there is a match in both tables. ã LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table. ã RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table. ã FULL JOIN: returns rows when there is a match in one of the tables. ã SELF JOIN: is used to join a table to itself as if the table were two tables, temporarily renaming atleast one table in the SQL statement. ã CARTESIAN JOIN: returns the Cartesian product of the sets of records from the two or more joined tables.The most frequently used and important of the joins is the INNER JOIN . They are also referred to as anEQUIJOIN.The INNER JOIN creates a new result table by combining column values of two tables (table1 and table2)based upon the join-predicate. The query compares each row of table1 with each row of table2 to find allpairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for eachmatched pair of rows of A and B are combined into a result row. Syntax: The basic syntax of INNER JOIN  is as follows: SELE& $able1.>#l mn19 $able2.>#l mn2... :RM $able1INNER ?IN $able2N $able1.>#mm#n4/ield < $able2.>#mm#n4/ield= Example: Consider the following two tables, (a) CUSTOMERS table is as follows: +----+----------+-----+-----------+----------+| ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 2 | Delhi | 100.00 | | 3 | !a shi! | 23 | K#$a | 2000.00 | | % | &hai$ali | 2 | M mbai | '00.00 | |  | (a)di! | 2* | h#,al | 00.00 | | ' | K#mal | 22 | M | %00.00 | | * | M // | 2% | Ind#)e | 10000.00 | +----+----------+-----+-----------+----------+ (b) Another table is ORDERS as follows: +-----+---------------------+-------------+--------+| ID | DAE | ID | AMN |  +-----+---------------------+-------------+--------+| 102 | 2005-10-0 00600600 | 3 | 3000 | | 100 | 2005-10-0 00600600 | 3 | 100 | | 101 | 2005-11-20 00600600 | 2 | 1'0 | | 103 | 200-0-20 00600600 | % | 20'0 | +-----+---------------------+-------------+--------+ Now, let us join these two tables using INNER JOIN as follows: S7L8 SELE& ID9 NAME9 AMN9 DAE  :RM &SMERS INNER ?IN RDERS N &SMERS.ID < RDERS.&SMER4ID= This would produce the following result: +----+----------+--------+---------------------+| ID | NAME | AMN | DAE | +----+----------+--------+---------------------+| 3 | !a shi! | 3000 | 2005-10-0 00600600 | | 3 | !a shi! | 100 | 2005-10-0 00600600 | | 2 | Khilan | 1'0 | 2005-11-20 00600600 | | % | &hai$ali | 20'0 | 200-0-20 00600600 | +----+----------+--------+---------------------+ The SQL LEFT JOIN  returns all rows from the left table, even if there are no matches in the right table.This means that if the ON clause matches 0 (zero) records in right table, the join will still return a row inthe result, but with NULL in each column from right table.This means that a left join returns all the values from the left table, plus matched values from the righttable or NULL in case of no matching join predicate. Syntax: The basic syntax of LEFT JOIN  is as follows: SELE& $able1.>#l mn19 $able2.>#l mn2... :RM $able1LE: ?IN $able2N $able1.>#mm#n4/ield < $able2.>#mm#n4/ield= Here given condition could be any given expression based on your requirement. Example: Consider the following two tables, (a) CUSTOMERS table is as follows:  +----+----------+-----+-----------+----------+| ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+| 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 2 | Delhi | 100.00 | | 3 | !a shi! | 23 | K#$a | 2000.00 | | % | &hai$ali | 2 | M mbai | '00.00 | |  | (a)di! | 2* | h#,al | 00.00 | | ' | K#mal | 22 | M | %00.00 | | * | M // | 2% | Ind#)e | 10000.00 | +----+----------+-----+-----------+----------+ (b) Another table is ORDERS as follows: +-----+---------------------+-------------+--------+| ID | DAE | &SMER4ID | AMN | +-----+---------------------+-------------+--------+| 102 | 2005-10-0 00600600 | 3 | 3000 | | 100 | 2005-10-0 00600600 | 3 | 100 | | 101 | 2005-11-20 00600600 | 2 | 1'0 | | 103 | 200-0-20 00600600 | % | 20'0 | +-----+---------------------+-------------+--------+ Now, let us join these two tables using LEFT JOIN as follows: S7L8 SELE& ID9 NAME9 AMN9 DAE  :RM &SMERS LE: ?IN RDERS N &SMERS.ID < RDERS.&SMER4ID= This would produce the following result: +----+----------+--------+---------------------+| ID | NAME | AMN | DAE | +----+----------+--------+---------------------+| 1 | Ramesh | NLL | NLL | | 2 | Khilan | 1'0 | 2005-11-20 00600600 | | 3 | !a shi! | 3000 | 2005-10-0 00600600 | | 3 | !a shi! | 100 | 2005-10-0 00600600 | | % | &hai$ali | 20'0 | 200-0-20 00600600 | |  | (a)di! | NLL | NLL | | ' | K#mal | NLL | NLL | | * | M // | NLL | NLL | +----+----------+--------+---------------------+ The SQL RIGHT JOIN  returns all rows from the right table, even if there are no matches in the left table.This means that if the ON clause matches 0 (zero) records in left table, the join will still return a row in theresult, but with NULL in each column from left table.
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks