SQL Joins
SQL Joins
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
P Q
P1 Q1
P2 Q2
P3 Q3
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 (+) 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.
➢ 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.
Self Join
SELECT columns
FROM table1 t1 JOIN table2 t2
ON t1.column > t2.column;
SQL Joins
Q7.
➢ 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
Q2. Which one of the join operation does not preserve non matched tuples? [ACP]
C. Inner Join
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
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)}
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
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]
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