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

Houssein Alaeddine DataBase Lecture 2

test

Uploaded by

Ice Tech
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)
19 views9 pages

Houssein Alaeddine DataBase Lecture 2

test

Uploaded by

Ice Tech
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

Cartesian product/Cross Join

The Cartesian product of 2 tables consists in combining all


the possibilities of the 2 tables.

Each line of T1 will be concatenated with each line of T2.


The schema of the resulting table (A, B, X, Y) is the union
of schemas of both tables.
A B X Y
a1 b1 x1 y1
X Y
1 A B a1 b1 x2 y1
x1 y1
a1 b1 a1 b1 x2 y2
x2 y1
a2 b1 a2 b1 x1 y1
x2 y2
T1 a2 b1 x2 y1
T2
a2 b1 x2 y2

T1×T2 Dr. Houssein Alaeddine


Cartesian product/Cross Join
Author
Article
Syntax ID Name
NUM Title A_ID
1 Alaeddine
100 Hello 3
Method1: 2 Beydoun
SELECT * 101 Bye 1
3 Darwich
FROM Author,Article
ID Name Num Title A_ID
Method2:
SELECT * 1 Alaeddine 100 Hello 3
2 FROM Author 1 Alaeddine 101 Bye 1
CROSS JOIN Article 2 Beydoun 100 Hello 3
2 Beydoun 101 Bye 1
3 Darwich 100 Hello 3
Query: Show names of authors
3 Darwich 101 Bye 1
and titles of their articles?
Author × Article Dr. Houssein Alaeddine
Cross Join, Where
Query: Show names of authors and titles of their articles?
ID Name Num Title A_ID

1 Alaeddine 100 Hello 3


1 Alaeddine 101 Bye 1
2 Beydoun 100 Hello 3
2 Beydoun 101 Bye 1
3 Darwich 100 Hello 3
3 Darwich 101 Bye 1
3
Author × Article

SELECT Name, Title


FROM Author,Article
WHERE ID=A_ID Dr. Houssein Alaeddine
Inner Join T1 T2

Returns records that have matching values in T1 and T2.

SELECT Name, Title


FROM Author INNER JOIN Article
ON Author.ID = Article.A_ID

Name Title
4
Alaeddine Bye
Darwich Hello

Author. and Article. are optional here because ID is in Author only


and A_ID is in Article only i.e. no confusion will be encountered.
Dr. Houssein Alaeddine
Left Join T1 T2

Returns all records from T1, and the matching records from T2.

Query: Show names of all authors and titles of their articles.


A NULL value is to be associated with authors who haven’t
written any article.

5 Name Title
SELECT Name, Title Alaeddine Bye
FROM Author LEFT JOIN Article Beydoun NULL
ON Author.ID = Article.A_ID Darwich Hello

Dr. Houssein Alaeddine


Natural Join
Similar to Inner Join, Natural Join is based on common columns.
Hence the keyword ON is not used.

Author
Article
ID Name
NUM Title ID
1 Alaeddine
100 Hello 3
2 Beydoun
101 Bye 1
6 3 Darwich

SELECT Name, Title


FROM Author NATURAL JOIN Article
Dr. Houssein Alaeddine
Alias
Give a table or a column a temporary name.
The Keyword AS can be used but it is not mandatory.

Syntax

SELECT Name Author_Name, Title Article_Title


FROM Author R INNER JOIN Article E
ON R.ID = E.A_ID
7
Author_Name Article_Title

Alaeddine Bye
Darwich Hello
Dr. Houssein Alaeddine
Union
Merge two tables with same schema together.
Redundant records are not included.
Syntax
Author Old_Author
ID Name ID Name SELECT * FROM Author
1 Alaeddine 10 Fares UNION
2 Beydoun
SELECT * FROM Old_Author
3 Darwich ID Name
8 1 Alaeddine

2 Beydoun

3 Darwich

10 Fares
Dr. Houssein Alaeddine
Client

ANY ID
1
Name
Alaeddine
Salary
1500
2 Darwich 2000
Query: Find the client having
3 Beydoun 1800
the maximum salary?
10 Fares 1200

SELECT * FROM client


WHERE salary NOT IN
( Salary
SELECT salary FROM client 1500
WHERE salary < ANY 1800
9
(SELECT salary FROM client) 1200

)
What about the minimum
ID Name Salary salary ?
2 Darwich 2000 Dr. Houssein Alaeddine

You might also like