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

CNG351 Lecture 10 DML Part 2

Uploaded by

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

CNG351 Lecture 10 DML Part 2

Uploaded by

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

SQL: Data Manipulation

“SELECT - JOIN”

CNG351 - Data Management and File Structures


Lecture - 10
Instructor: Dr. Yeliz Yesilada
Data Manipulation
• INSERT: to insert data into a table;
• UPDATE: to update data into a table;
• DELETE: to delete data from a table;
• SELECT: to query data in the database;
• JOIN: to join relations.
Multi-Table Queries
• Can use subqueries provided result columns come from same table.
• If result columns come from more than one table must use a join. The row
pairs that make up the joined table are those where the matching columns
in each of the two tables have the same value.
• To perform join, include more than one table in FROM clause.
• Use comma as separator and typically include WHERE clause to specify
join column(s).
• Also possible to use an alias for a table named in FROM clause.
• Alias is separated from table name with a space.
• Alias can be used to qualify column names when there is
ambiguity.
Simple Join
List names of all clients who have viewed a property along with
any comment supplied.
Only those rows from both tables that have identical values in the
clientNo columns (c.clientNo = v.clientNo) are included in result.
Equivalent to equi-join in relational algebra.

SELECT c.clientNo, fName, lName, SELECT c.clientNo, fName, lName,


propertyNo, comment propertyNo, comment
FROM Client c, Viewing v FROM Client c
WHERE c.clientNo = v.clientNo; JOIN Viewing v ON c.clientNo = v.clientNo;
Sorting a join
For each branch, list numbers and names of staff who manage
properties, and properties they manage.
SELECT s.branchNo, s.staffNo, fName, lName,
propertyNo
FROM Staff s, PropertyForRent p
WHERE s.staffNo = p.staffNo
ORDER BY s.branchNo, s.staffNo, propertyNo;
Three Table Join
For each branch, list staff who manage properties, including city
in which branch is located and properties they manage.

SELECT b.branchNo, b.city, s.staffNo, fName, lName,


propertyNo
FROM Branch b, Staff s, PropertyForRent p
WHERE b.branchNo = s.branchNo AND
s.staffNo = p.staffNo
ORDER BY b.branchNo, s.staffNo, propertyNo;

Alternative formulation for FROM and WHERE:


FROM (Branch b JOIN Staff s USING branchNo)
AS
bs JOIN PropertyForRent p USING staffNo
Multiple Grouping Columns
Find number of properties handled by each staff member.

SELECT s.branchNo, s.staffNo, COUNT(*) AS myCount


FROM Staff s, PropertyForRent p
WHERE s.staffNo = p.staffNo
GROUP BY s.branchNo, s.staffNo
ORDER BY s.branchNo, s.staffNo;
Computing a Join
• Procedure for generating results of a join are:
1. Form Cartesian product of the tables named in FROM clause.
2. If there is a WHERE clause, apply the search condition to each
row of the product table, retaining those rows that satisfy the
condition.
3. For each remaining row, determine value of each item in
SELECT list to produce a single row in result table.
4. If DISTINCT has been specified, eliminate any duplicate rows
from the result table.
5. If there is an ORDER BY clause, sort result table as required.
NOTE:
• SQL provides special format of SELECT for Cartesian product:
SELECT [DISTINCT | ALL] {* | columnList}
FROM Table1 CROSS JOIN Table2
Outer Joins
• If one row of a joined table is unmatched, row is omitted from result
table.
• Outer join operations retain rows that do not satisfy the join
condition.
• Consider following tables:
Outer Joins
• The (inner) join of these two tables:

SELECT b.*, p.*


FROM Branch1 b, PropertyForRent1 p
WHERE b.bCity = p.pCity;
Outer Joins
• Result table has two rows where cities are same.
• There are no rows corresponding to branches in Bristol
and Aberdeen.
• To include unmatched rows in result table, use an
Outer join.
Left Outer Join
List branches and properties that are in same city along with any
unmatched branches.
SELECT b.*, p.*
FROM Branch1 b LEFT JOIN
PropertyForRent1 p ON b.bCity = p.pCity;
• Includes those rows of first (left) table unmatched with rows from
second (right) table.
• Columns from second table are filled with NULLs.
Right Outer Join
List branches and properties in same city and any unmatched
properties.
SELECT b.*, p.*
FROM Branch1 b RIGHT JOIN
PropertyForRent1 p ON b.bCity = p.pCity;
• Right Outer join includes those rows of second (right) table that are
unmatched with rows from first (left) table.
• Columns from first table are filled with NULLs.
Full Outer Join
List branches and properties in same city and any unmatched
branches or properties.
SELECT b.*, p.*
FROM Branch1 b FULL JOIN
PropertyForRent1 p ON b.bCity = p.pCity;
• Includes rows that are unmatched in both tables.
• Unmatched columns are filled with NULLs.
EXISTS and NOT EXISTS
• EXISTS and NOT EXISTS are for use only with subqueries.
• Produce a simple true/false result.
• True if and only if there exists at least one row in result table
returned by subquery.
• False if subquery returns an empty result table.
• NOT EXISTS is the opposite of EXISTS.
• As (NOT) EXISTS check only for existence or non-existence of
rows in subquery result table, subquery can contain any number
of columns.
• Common for subqueries following (NOT) EXISTS to be of form:

(SELECT * ...)
Query using EXISTS
Find all staff who work in a London branch.
SELECT staffNo, fName, lName, position
FROM Staff s
WHERE EXISTS
(SELECT *
FROM Branch b
WHERE s.branchNo = b.branchNo AND
city = ‘London’);
Query using EXISTS
• Note, search condition s.branchNo = b.branchNo is necessary to
consider correct branch record for each member of staff.
• If omitted, would get all staff records listed out because subquery:
SELECT * FROM Branch WHERE city=‘London’
• would always be true and query would be:
SELECT staffNo, fName, lName, position FROM Staff
WHERE true;
• Could also write this query using join construct:
SELECT staffNo, fName, lName, position
FROM Staff s, Branch b
WHERE s.branchNo = b.branchNo AND
city = ‘London’;
Union, Intersect, and Difference (Except)
• Can use normal set operations of Union, Intersection, and
Difference to combine results of two or more queries into a single
result table.
– Union of two tables, A and B, is table containing all rows in
either A or B or both.
– Intersection is table containing all rows common to both A and
B.
– Difference is table containing all rows in A but not in B.
• Two tables must be union compatible.
Union, Intersect, and Difference (Except)
• Format of set operator clause in each case is:
operator [ALL] [CORRESPONDING [BY {column1 [, ...]}]]
• If CORRESPONDING BY specified, set operation performed on the
named column(s).
• If CORRESPONDING specified but not BY clause, operation
performed on common columns.
• If ALL specified, result can include duplicate rows.
Union, Intersect, and Difference (Except)
Use of UNION
List all cities where there is either a branch office or a property.

(SELECT city
FROM Branch
WHERE city IS NOT NULL) UNION
(SELECT city
FROM PropertyForRent
WHERE city IS NOT NULL);
Use of INTERSECT
List all cities where there is both a branch office and a property.

(SELECT city FROM Branch)


INTERSECT
(SELECT city FROM PropertyForRent);
• Or
(SELECT * FROM Branch)
INTERSECT CORRESPONDING BY city
(SELECT * FROM PropertyForRent);
Use of INTERSECT
• Could rewrite this query without INTERSECT operator:

SELECT b.city
FROM Branch b PropertyForRent p
WHERE b.city = p.city;
• Or:
SELECT DISTINCT city FROM Branch b
WHERE EXISTS
(SELECT * FROM PropertyForRent p
WHERE p.city = b.city);
Use of EXCEPT
List of all cities where there is a branch office but no properties.

(SELECT city FROM Branch)


EXCEPT
(SELECT city FROM PropertyForRent);
• Or

(SELECT * FROM Branch)


EXCEPT CORRESPONDING BY city
(SELECT * FROM PropertyForRent);
Use of EXCEPT
• Could rewrite this query without EXCEPT:

SELECT DISTINCT city FROM Branch


WHERE city NOT IN
(SELECT city FROM PropertyForRent);
• Or

SELECT DISTINCT city FROM Branch b


WHERE NOT EXISTS
(SELECT * FROM PropertyForRent p
WHERE p.city = b.city);
Summary
• If the columns of a result table come from more than one table, a
join must be used.;
• SQL allows the set operations of Union, Intersection, and
difference to be used with UNION, INTERSECT, and EXCEPT
commands.

You might also like