0% found this document useful (0 votes)
3 views9 pages

Join

Uploaded by

adhamalaa172
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Join

Uploaded by

adhamalaa172
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Student table =A

sid sname Age adress dcode C1


1 Ali 18 Cairo 11
2 Mona 18 Cairo 11
3 Ahmed 20 Cairo 14
4 Omr 20 Assuit 14
5 marwa 18 cairo null
6 Adel 18 Alex 12
7 Hend 18 alex 12

Department table =B

Did dname C1 Gpa_lowest Gpa_highest


11 Is 2.5 2.7
12 It 2.6 2.8
13 Cs 2.5 3
14 Mi 3 3.3
15 mm 2 3

Nonequijoins
A nonequijoin is a join condition containing something other
than an equality operator.

Select s. sname , d.dname , d.Gpa_lowest

, d.Gpa_highest From student s join department d

On (between d.Gpa_lowest

And d. Gpa_highest)
INNER Versus OUTER Joins
• the join of two tables returning only matched rows
is called an inner join.
• A join between two tables that returns the results of
the inner join as well as the unmatched rows from
the left (or right) table is called a left (or right) outer
join.
• A join between two tables that returns the results of
an inner join as well as the results of a left and right
join is a full outer join.
1- Right outer join
2- Left outer join
3- Full outer join

Q1: query retrieves all rows in the student table, which is the
left table, even if there is no match in the DEPARTMENTS
table.
Select s.sname , s.age , d.dname
From student s left outer join departments d
On (d.did = s.dcode )

sname age dname


Ali 18 Is
Mona 18 Is
Ahmed 20 Mi
Omr 20 Mi
marwa 18 null
Adel 18 It
Hend 18 It

Select s.sname , s.age , d.dname


From student s right outer join departments d
On (d.did = s.dcode )

sname age dname


Ali 18 Is
Mona 18 Is
Adel 18 It
Hend 18 It
Ahmed 20 Mi
Omr 20 Mi
null null cs
null null mm
Select s.sname , s.age , d.dname
From student s full outer join departments d
On (d.did = s.dcode )

Generating a Cartesian Product


A Cartesian product is generated if a join condition is omitted.
The example in the slide displays the employee last name and
the department name from the EMPLOYEES and
DEPARTMENTS tables. Because no join condition was
specified, all rows (20 rows) from the student table are joined
with all rows (8 rows) in the DEPARTMENTS table, thereby
generating 160 rows in the output.
SELECT sname, dname
FROM student
CROSS JOIN departments ;

Section 1
• Natural joins:
– NATURAL JOIN clause
– USING clause
– ON clause
• 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


SELECT sname , dname
FROM student
NATURAL JOIN department ;

Creating Joins with the USING Clause


• If several columns have the same names but the
data types do not match, natural join can be applied
using 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.
• The NATURAL JOIN and USING clauses are mutually
exclusive.

SELECT sname , dname


FROM student
JOIN department
Using (Did ) ;

Creating Joins with the ON Clause


• The join condition for the natural join is basically
an equijoin of all columns with the same name.
• 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.

Select d.dname , s. sname


From students s
Join dept d
On (d.did = s.dcode )

Select d.dname , s. sname


From students s
Join dept d
On (d.did = s.did )

You might also like