Lab - 5 - Retrieving Data From Multiple Tables
Lab - 5 - Retrieving Data From Multiple Tables
Faculty of Engineering
Dept. of Computer Engineering
Database Lab (ECOM 4113)
Lab 5
SQL
Retrieving Data from
Multiple Tables
3) Natural join
It is based on the two conditions:
The JOIN is made on all the columns with the same name for quality
Removes duplicate columns from the result.
4) Cross join
It is the Cartesian product of the two tables involved.
5) Self
It is a JOIN (INNER, OUTER, etc) of a table to itself.
Join Conditions:
Natural:
Creates an implicit join base on columns that have the same name in both
tables, also common column must have the same datatype, otherwise
return error.
Using ( column_name) :
Creates an explicit join base on columns that have the same name in both
tables, usually use key attribute to join two table.
On:
creates an explicit join using join condition to bind to two table, use it when
key attribute in each column has different name, also we can add
conditional expression as where.
Note:
All INNER and OUTER keywords are optional
Join Conditions (natural, using, on) can be combined with any join type
Example: Find a list of all students, displaying their ID, and name, dept_name, and
tot_cred, along with the courses that they have taken
Use on clause
SELECT STUDENT.ID ,NAME,DEPT_NAME,TOT_CRED,COURSE_ID
FROM STUDENT JOIN TAKES
ON STUDENT.ID=TAKES.ID ;
OUTER JOIN
By default, joining tables with the NATURAL JOIN, USING, or ON clauses results in
an inner join. Any unmatched rows are not displayed in the output. To return the
unmatched rows, you can use an outer join. An outer join returns all rows that
satisfy the join condition and also returns some or all of those rows from one
table for which no rows from the other table satisfy the join condition.
If the SELECT statement in which the JOIN operations appears has an asterisk (*)
in the select list, the asterisk will be expanded to the following list of columns (in
this order):
All the common columns.
Every column in the first (left) table that is not a common column.
Every column in the second (right) table that is not a common column
SELECT *
FROM INSTRUCTOR
LEFT JOIN DEPARTMENT
USING(DEPT_NAME);
Instructor “ibraheem “do not work in any department so all department attribute
returned as null value.
Also we can use natural condition to join between two table base common
column tables.
SELECT *
FROM INSTRUCTOR
NATURAL LEFT JOIN DEPARTMENT
2) Right OUTER JOIN:
SELECT *
FROM INSTRUCTOR
RIGHT JOIN DEPARTMENT
USING(DEPT_NAME);
SELECT *
FROM INSTRUCTOR
FULL JOIN DEPARTMENT
USING(DEPT_NAME);
Left table
Right table
Self-join: Joining a Table to Itself
Example: Find the names of all instructors whose salary is greater than at least
one instructor in the Biology department.
Example: For each course section offered in 2009, find the average total credits
( tot cred) of all students enrolled in the section, which had at least 2 students
Example: Find the number of instructors in each department who teach a course
in the Spring 2010 semester
END