Database CIS 340 lab#6 I.Arwa Najdi a_najdi1988@yahoo.com
Outlines Obtaining Data from Multiple Tables (Join) Equijoins= inner join natural join Creating Joins with the USING Clause Creating Joins with the ON Clause Non-Equijoins Self-join
Join Joins allow you to link data from two or more tables together into a single query result-- from one single SELECT statement. A "Join" can be recognized in a SQL SELECT statement if it has more than one table after the FROM keyword.
Benefits of join in SQL what would happen if you worked with one table only, and didn't have the ability to use "joins?? Let's say you have a one-table database that is used to keep track of all of your customers and what they purchase from your store: Everytime a new row is inserted into the table, all columns will be be updated, thus resulting in unnecessary "redundant data".
For example, every time Wolfgang Schultz purchases something, the following rows will be inserted into the table:
An ideal database would have two tables: One for keeping track of your customers And the other to keep track of what they purchase Now, whenever a purchase is made from a repeating customer, the 2nd table, "Purchases" only needs to be updated! We've just eliminated useless redundant data
Notice how each of the tables have a common "cusomer_number" column. This column, which contains the unique customer number will be used to JOIN the two tables. Using the two new tables, let's say you would like to select the customer's name, and items they've purchased. Here is an example of a join statement to accomplish this data. SELECT customer_info.firstname, customer_info.lastname, purchases.item FROM customer_info, purchases WHERE customer_info.customer_number = purchases.customer_number; This is inner join or Equijoin, it is the most common type of "Join" that you will see or use.
. SQL Joins SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables. The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. Tables in a database are often related to each other with keys. A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
Equijoins=inner join An equijoin is a join with a join condition containing an equality operator ( = ). An equijoin combines rows that have equivalent values for the specified columns. For example: the following query returns empno,name,sal,deptno and department name and city from department table. Select emp.empno,emp.ename,emp.sal,emp.deptno,dept.dn ame,dept.city from emp,dept where emp.deptno=dept.deptno; The above query can also be written like, using aliases, given below. select e.empno, e.ename, e.sal, e.deptno, d.dname, d.city from emp e, dept d where emp.deptno=dept.deptno;
Equijoins=inner join SQL INNER JOIN returns records from both tables where a match is found based on join_condition. The syntax of SQL INNER JOIN is as follows: SELECT selection_list FROM table_a INNER JOIN table_b ON join_condition WHERE row_conditions. SQL INNER JOIN gets all the records from the table_a and finds the matching records in the table_b according to the join condition. The join condition determines whether both records are matched or not. If there is no match found, no records is returned.
another form of SQL INNER JOIN There is another form of SQL INNER JOIN which called implicit inner join. implicit SQL INNER JOIN syntax: SELECT selection_list FROM table_a, table_b WHERE join_condition.
Retrieving Records with Equijoins
Retrieving Records with Equijoins
Qualifying Ambiguous Column Names Use table prefixes to qualify column names that are in multiple tables. Use table prefixes to improve performance. Use column aliases to distinguish columns that have identical names but reside in different tables.
Joining More Than Two Tables
natural join The NATURAL JOIN clause is based on all columns in the two tables that have the same name. It selects rows from the two tables that have equal values in all matched columns. If the columns having the same names have different data types, an error is returned.
Retrieving Records with Natural Joins
Creating Joins with the USING Clause If several columns have the same names but the data types do not match, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an equijoin. Use the USING clause to match only one column when more than one column matches. Do not use a table name or alias in the referenced columns. The NATURAL JOIN and USING clauses are mutually exclusive.
Retrieving Records with the USING Clause Do not use aliases on columns that are identified in the USING clause and listed elsewhere in the SQL statement.
Creating Joins with the ON Clause Use the ON clause to specify arbitrary conditions or specify columns to join. The join condition is separated from other search conditions. The ON clause makes code easy to understand.
Retrieving Records with the ON Clause
Creating Three-Way Joins with the ON Clause
Non-Equijoins Non equi joins is used to return result from two or more tables where exact join is not possible. For example: we have emp table and job_grade table. The job_grade table contains grade and their low salary and high salary. Suppose you want to find the grade of employees based on their salaries then you can use NON EQUI join.
Non-Equijoins
Retrieving Records with Non-Equijoins
Self-Joins A self join is a join of a table to itself. This table appears twice in the FROM clause and is followed by table aliases that qualify column names in the join condition. To perform a self join, Oracle combines and returns rows of the table that satisfy the join condition.
Self-Joins
Joining a Table to Itself
Self-Joins Using the ON Clause
Applying Additional Conditions to a Join