Data Manipulation (Part II)
Data Manipulation (Part II)
Week 5, 6
National College of Ireland
Dublin, Ireland.
(SELECT Sno
FROM Staff
WHERE Bno = Property_for_Rent
(SELECT Bno Staff
FROM Branch Branch
• Inner query produces set {12000, 18000, 24000} and outer query selects
those staff whose salaries are greater than any of the values in this set.
Sno
6
Use of ALL
Sno
7
Use of Alias
• Can use subqueries provided result columns come from same table.
• If the result of columns come from more than one table must use a join.
• 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.
SELECT Pno, Street, City, Rooms Without use of Alias
FROM property_for_rent;
SELECT P.Pno, P.Street, P.City, P.Rooms
With use of Alias 8
FROM property_for_rent P;
Simple Join
List names of all clients (renters) who have viewed a property
along with any comment supplied.
SELECT r.Rno, r.fName, r.lName, v.Pno, v.comment
FROM Renter r, Viewing v Alias
WHERE r.Rno = v.Rno;
• Only those rows from both tables that have identical values in the Rno
columns (r.Rno = v.Rno) are included in result.
• Equivalent to equi-join in relational algebra.
Rno Pno
9
Alternative JOIN Constructs
11
Three Table Join
For each branch, list staff who manage properties,
including city in which branch is located and
properties they manage.
SELECT b.Bno, b.city, s.Sno, s.fName, s.lName, p.Pno
FROM Branch b, Staff s, Property_For_Rent p
WHERE b.Bno = s.Bno AND s.Sno = p.Sno
ORDER BY b.Bno, s.Sno, p.Pno;
12
Multiple Grouping Columns
Bno Sno
13
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.
• SQL provides a special format of SELECT for Cartesian product:
SELECT [DISTINCT | ALL] {* | columnList}
14
FROM Table1 CROSS JOIN Table2
Outer Join
• The FULL OUTER JOIN keyword returns all rows from the left
table (table1) and from the right table (table2).
• SELECT column_name(s)
FROM table1
Outer join operations retain rows that
FULL OUTER JOIN table2 do not satisfy the join condition.
ON table1.column_name=table2.column_name;
15
Inner Joins
• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
16
Inner Join
17
Left Outer Join
List branches and properties that are in same city along
with any unmatched branches.
SELECT b.Bno AS Bno,
The LEFT JOIN keyword returns all rows from the
b.city AS bcity, left table (table1), with the matching rows in the
right table (table2). The result is NULL in the right
p.Pno AS Pno, side when there is no match.
p.city AS pcity Columns from second table are filled with NULLs.
FROM branch b
LEFT JOIN property_for_rent p ON b.city = p.city;
Bno Pno
18
Right Outer Join
19
Full Outer Join
List branches and properties in same city and any unmatched
branches or properties.
FULL OUTER JOINS are
SELECT b.Bno AS Bno, b.city AS bcity,
not supported in MySql
p.Pno AS Pno, p.city AS pcity
FROM branch b
Union All
LEFT JOIN property_for_rent p
ON b.city = p.city The FULL OUTER JOIN keyword returns
UNION ALL all rows from the left table (table1) and
SELECT b.Bno AS Bno, from the right table (table2).
b.city AS bcity,
Bno Pno
p.Pno AS Pno, p.city AS pcity
FROM branch b
RIGHT JOIN property_for_rent p
20
ON b.city = p.city;
EXISTS and NOT EXISTS
• EXISTS and NOT EXISTS are for use only with subqueries.
• True if and only if there exists at least one row in the result table returned by
subquery.
Sno
22
Query using EXISTS
• Note, search condition s.Bno = b.Bno is necessary to consider
correct branch record for each member of staff.
(SELECT city
FROM Branch) INTERSECT is not
INTERSECT available in MySQL
(SELECT city
FROM Property_For_Rent);
OR
SELECT city FROM branch WHERE city IN (SELECT
city FROM property_for_rent); 27
Use of INTERSECT
Or
INSERT INTO Staff VALUES ('SG44', 'Anne',
'Jones','Bradford', '44-435-567', 'Assistant', 'M', '1975-05-
23', 8100, 'PD2324032', 'B8'); 34
INSERT … SELECT
35
INSERT … SELECT
[, columnName2 = dataValue2...]
[WHERE searchCondition]
• SET clause specifies names of one or more columns that are to be updated.
[WHERE searchCondition]
41
Module Resources
Recommended Book Resources Dr. Muhammad M Iqbal*
• Thomas Connolly, Carolyn Begg 2014, Database Systems: A Practical Approach
to Design, Implementation, and Management, 6th Edition Ed., Pearson
Education [ISBN: 1292061189] [Present in our Library]
Supplementary Book Resources
• Gordon S. Linoff, Data Analysis Using SQL and Excel, Wiley [ISBN:
0470099518]
• Eric Redmond, Jim Wilson, Seven Databases in Seven Weeks, Pragmatic
Bookshelf [ISBN: 1934356921]
• Baron Schwartz, Peter Zaitsev, Vadim Tkachenko, High Performance MySQL,
O'Reilly Media [ISBN: 1449314287]
Other Resources
• Website: http://www.thearling.com
• Website: http://www.mongodb.org
42
• Website: http://www.mysql.com