0% found this document useful (0 votes)
21 views

SQL Joins

The document discusses different types of SQL joins including inner joins, outer joins, self joins and more. It provides examples and explanations of each join type and how they are used to combine data from multiple tables.

Uploaded by

Boby Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

SQL Joins

The document discusses different types of SQL joins including inner joins, outer joins, self joins and more. It provides examples and explanations of each join type and how they are used to combine data from multiple tables.

Uploaded by

Boby Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

DATABASE MANAGEMENT SYSTEMS

SQL Joins
SQL Joins
SQL Joins
SQL Joins

SQL Joins
➢ SQL is a relational database query language
➢ Its most important features is its ability to retrieve information from several different
related tables
➢ In relational database term, this process is called a Join
➢ A join is a query that combines rows from two or more tables or views
➢ When the required data are in more than one table, related tables are joined using a join
condition
➢ The tables to be joined are named in the FROM clause of the SELECT statement with each
table name separated by a comma (,)
➢ The relationship(s) between the tables in a join are defined in the WHERE clause
➢ The query’s SELECT clause can have the columns or expressions from any or all of these
tables
SQL Joins

➢ Whenever multiple tables appear in the FROM clause of the SELECT statement, a
join operation is performed
SQL Joins

CARTESIAN PRODUCT OR CROSS PRODUCT


➢ A cross product specifies the cross-product of two tables
➢ A cartesian join occurs when data is selected from two or more tables and there is no
common relation specified in the WHERE clause
➢ If a join condition is not specified for tables listed in FROM clause, a cross join is performed
➢ A cross join returns all rows from first table to every row in the second table
SQL Joins

CARTESIAN PRODUCT OR CROSS PRODUCT


Example

Cartesian product (m*n)rows of both tables -


Where m = number of rows in table T1
n= number of rows in table T2
SQL Joins

CARTESIAN PRODUCT OR CROSS PRODUCT


Example
T1
X Y Z
X1 Y1 Z1 X Y Z P Q
X2 Y2 Z2
SELECT *
T2 FROM T1, T2;

P Q
P1 Q1
P2 Q2
P3 Q3
SQL Joins

CARTESIAN PRODUCT OR CROSS PRODUCT - Important Facts

➢ There is no join without a join condition; so cartesian product is not a join


➢ Cartesian product is only performed when you want to retrieve all possible combinations of
rows from two tables
➢ To avoid cartesian product, there should be at least n-1 join conditions when joining n
number of tables
SQL Joins
SQL Joins

Inner Join
SQL Joins

Inner Join
SQL Joins

Inner Join
➢ Returns all rows from multiple tables where the join condition is met
➢ Inner join specifies that all matching pair of rows are returned
➢ Inner join is default if no join type is specified
➢ With the inner join operator, any relational comparison operator (=, >, >=, <, <=, !=)or other
operators (BETWEEN….AND, IN etc.) can be used
SQL Joins
Inner Join
➢ Tablename.columnname – when columns are retrieved from more than one table; the use of a
table name qualifier in front of the column name specifies to retrieve that column from the
specified table
➢ If only one column name exists in only one of the two tables involved in the query; it is not
necessary to use a table name as a qualifier
➢ If a column name exists in both tables, you must use the table name qualifier to avoid
ambiguity
➢ The use of qualifier improves the performance because it tells the db server where to go to find
that column
➢ To join, columns need not to have the same name
➢ Once table alias names are defined, you can not use the table name to qualify a column; you
should use the alias name to qualify the column
SQL Joins
Inner Join

➢ If the query is relating two tables using an equality operator (=), it is an equijoin
➢ A natural join is a join which joins the tables on the condition of equality between any columns
with the same name in both tables
➢ If column names are not same but equality operator is used, it is known as equijoin
➢ If any other operator is used to join the tables in the query, it is a non equijoin
➢ An inner join can not be nested inside a left join or right join
➢ In an inner join, only rows that satisfy the join condition are present in the result table
SQL Joins
Q1. Retrieve all employee names along with their department names.
SQL Joins
Q1. Retrieve all employee names along with their department names.
SQL Joins
Q2. Retrieve all the employee numbers and their names who work in RESEARCH department.
SQL Joins
Q3. Retrieve all the employee names and their job locations who are working as a ‘PRESIDENT’ and
their salary is greater than 4000.
SQL Joins

Outer Join

➢ The table that does not contain the matching value is known as the deficient table
➢ An outer join uses the (+) operator in the join condition on the deficient side
SQL Joins

Outer Join

The general syntax is The general syntax is The general syntax is

SELECT columns SELECT columns SELECT columns


FROM table1 FROM table1 FROM table1
LEFT [OUTER] JOIN table2 RIGHT [OUTER] JOIN table2 FULL [OUTER] JOIN table2
ON table1.column = table2.column; ON table1.column = table2.column; ON table1.column = table2.column;
SQL Joins
Left outer Join
➢ Specifies all the records from the first (left) of the two tables, even if there are no matching
values for rows in the second (right) table
➢ If there is no matching row found from the right table, the left join will have null values for the
columns of the right table
➢ This type of join returns all rows from the Left-hand table specified in the ON condition and
only those rows from the other table where the joined fields are equal (join condition is met)
SQL Joins
Q4.

SELECT D.DEPTNO, E.EMPNO, E.ENAME


FROM DEPARTMENT D LEFT OUTER JOIN
EMPLOYEE E ON
D.DEPTNO=E.DEPTNO;

SELECT D.DEPTNO, E.EMPNO, E.ENAME


FROM DEPARTMENT D, EMPLOYEE E
WHERE D.DEPTNO = E.DEPTNO(+);
SQL Joins

➢ The (+) operator can be used on any side of the join conditions, but it can
not be used on both sides in one condition
SQL Joins
Right outer Join

➢ Specifies all the records from the second (right) of the two tables, even if there are no matching
values for rows in the first table
➢ If there is no matching row found from the left table, the right join will have null values for the
columns of the left table
➢ This type of join returns all rows from the Right-hand table specified in the ON condition and
only those rows from the other table where the joined fields are equal (join condition is met)
SQL Joins
Q5.

SELECT D.DNAME, E.ENAME, D.DEPTNO


FROM EMPLOYEE E RIGHT OUTER JOIN
DEPARTMENT D ON
E.DEPTNO = D.DEPTNO;

SELECT D.DNAME, E.ENAME, D.DEPTNO


FROM EMPLOYEE E, DEPARTMENT D
WHERE E.DEPTNO (+) = D.DEPTNO;
SQL Joins
Full outer Join

➢ If a row, from either the left or right table does not match the selection criteria, full outer join
specifies the row be included in the result set and output columns that correspond to the other
table be set to NULL
➢ This is in addition to all rows usually returned by the inner join
➢ This type of join returns all rows from the Left-hand table and Right-hand table with nulls in
place where the join condition is not met
SQL Joins
Q6.

SELECT E.EMPNO, E.DEPTNO, E.ENAME, D.DEPTNO, D.DNAME


FROM EMPLOYEE E FULL OUTER JOIN DEPARTMENT D ON
E.DEPTNO=D.DEPTNO;
SQL Joins
Differences between Left Outer Join, Right Outer Join, Full Outer Join
LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
Fetches all the rows Fetches all the rows Fetches all the rows
from the table on the from the table on the from both the tables
left right
Inner Join + Inner Join + Inner Join +
all the unmatched rows all the unmatched rows all the unmatched rows
from the left table from the right table from the left table +
all the unmatched rows
from the right table
Unmatched data of the Unmatched data of the No data is lost
right table is lost left table is lost
SQL Joins
Self Join

➢ A self join joins a table to itself


➢ The table name appears in the FROM clause twice, with different alias names
➢ When a table is joined to itself, the two table aliases are treated as two different tables and a
join is produced from those two different tables
➢ You look the table twice in self join
➢ A self join allows you to compare information in a table with other information in the same
table
SQL Joins

Self Join

The general syntax is

SELECT columns
FROM table1 t1 JOIN table2 t2
ON t1.column > t2.column;
SQL Joins
Q7.

SELECT D1.DEPTNO, D2.DEPTNO


FROM DEPARTMENT D1 JOIN DEPARTMENT D2 ON
D1.DEPTNO > D2.DEPTNO;
SQL Joins
Semi Join & Anti join

➢ A semi-join returns one copy of each row in first table for which at least one match is found.
➢ Semi-joins are written using the EXISTS construct.
➢ An anti-join returns one copy of each row in the first table for which no match is found.
➢ Anti-joins are written using the NOT EXISTS or NOT IN constructs.
SQL Joins

Q1. The SQL statement select * from R, Q is equivalent to _____? [ACP]

A. Select * from R natural join Q

B. Select * from R cross join Q

C. Select * from R union join Q

D. Select * from R inner join Q


SQL Joins

Q2. Which one of the join operation does not preserve non matched tuples? [ACP]

A. Left Outer Join

B. Right Outer Join

C. Inner Join

D. Full Outer Join


SQL Joins

Q3. Which clause represents a Cartesian-product operation in basic structure of an SQL expression?
[ACF & FRO]

A. Select

B. From

C. Where

D. as
SQL Joins

Q4. The Cartesian product of A = (1, 2) and B = {a, b, c} is ? [ITI]

A. A x B = {(a, 1), (a, 2), (b, 1), (b, 2), (c, 1), (c, 2)}

B. A x B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)}

C. A x B = {(1, a, b, c ), (2, a, b, c)}

D. A x B = {(a, b, c, 1), (a, b, c, 2)}


SQL Joins

Q5. The SQL expression


Select distinct T. branchname from branch T, branch S where T.
Assets > S. assets and S branchcity = “XYZ”
Find the names of [Programmer]

A. All branches that have greater assets than some branch located in XYZ

B. All branches that have greater assets than some branch located in XYZ

C. The branch that has the greatest asset in XYZ

D. Any branch that has greater asset than any branch located in XYZ
SQL Joins
Q6. Database table by name Loan_Records is given below. [Lecturer]

Borrower Bank Manager Loan_Amount


VASU Sunderajan 10000.00
ABHISHEK Ramgopal 5000.00

PIYUS Sunderajan 7000.00

What is the output of the following SQL query?


SELECT count(*)
FROM
(SELECT Borrower, Bank_Manager FORM Loan_Records) AS S NATURAL JOIN
(SELECT Bank_Manager, Loan_Amount FORM Loan_Records) AS T );

(a) 3 (b) 9 (c) 5 (d) 6


SQL Joins

Q7. An athletics meeting involves several competitors who participate in a number of events. The database is
intended to record who is to take part in which event and to record the outcome of each event. As results become
available the winner attribute will be updated with the cid of the appropriate competitor. [Programmer]
Competitor (cid, name, nationality) Competitor
cid Name nationality
Event (cid, description, winner)
01 Pat British
Competes (cid, eid) 02 Hilary British
03 Sven Swedish
Identify the result of the following SQL statement
01 Pierre French
SELECT eid FROM Competes, Competitor Competes Event
WHERE Competes.cid = Competitor, cid cid eid eid Description Winner
01 01
AND nationality = ‘Swedish’ 02 01 01 running
03 02 02 jumping
04 02
03 throwing
04 03

(a) 01 (b) 02 (c) 03 (d) 04

You might also like