You are on page 1of 42

Data Manipulation (Part II)

Week 5, 6
National College of Ireland
Dublin, Ireland.

Lecturer: Dr. Muhammad Iqbal


Email: Muhammad.Iqbal@ncirl.ie 1
Outline
• Purpose and importance of SQL.
• How to write an SQL command.
• How to retrieve data from database using SELECT and:
– Use simple and compound WHERE conditions.
– Sort query results using ORDER BY.
– Use AGGREGATE functions.
– Group data using GROUP BY and HAVING.
– Use subqueries.
– Join tables together.
– Perform set operations (UNION, INTERSECT, EXCEPT).
• How to update database using INSERT, UPDATE, and DELETE.
2
Subquery Rules
• ORDER BY clause may not be used in a subquery (although it
may be used in outermost SELECT).

• Subquery SELECT list must consist of a single column name or


expression, except for subqueries that use EXISTS.

• By default, the column names refer to table name in FROM clause


of subquery. Can refer to a table in FROM using an alias.

• When subquery is an operand in a comparison, subquery must


appear on right-hand side.

• A subquery may not be used as an operand in an expression. 3


Nested Subquery: use of IN

List properties handled by staff at '163 Main St'.


SELECT Pno, street, city, pcode, type, rooms, rent
Ladder Query
FROM Property_For_Rent
WHERE Sno IN Pno pcode

(SELECT Sno
FROM Staff
WHERE Bno = Property_for_Rent
(SELECT Bno Staff
FROM Branch Branch

WHERE street = '163 Main St')); 4


ANY and ALL

• ANY and ALL keywords may be used with subqueries that


produce a single column of numbers.

• With ALL, the condition will only be true if it is satisfied by


all values produced by subquery.

• With ANY, the condition will be true if it is satisfied by any


values produced by subquery.

• If subquery is empty, ALL returns true, ANY returns false.

• SOME may be used in place of ANY. 5


Use of ANY/SOME
Find staff whose salary is larger than salary of at least one member of
staff at branch 'B3'.
SELECT Sno, fName, lName, position, salary
FROM Staff
WHERE salary > SOME (SELECT salary
FROM Staff
WHERE Bno = 'B3');

• 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

Find staff whose salary is larger than salary of


every member of staff at branch B3.
SELECT Sno, fName, lName, position, salary
FROM Staff
WHERE salary > ALL
(SELECT salary
FROM Staff
WHERE Bno = 'B3');

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

• SQL provides alternative ways to specify joins:

• FROM Renter r JOIN Viewing v ON r.Rno = v.Rno

• FROM Renter JOIN Viewing USING Rno

• FROM Renter NATURAL JOIN Viewing

• In each case, FROM replaces original FROM and


WHERE. However, first produces table with two
identical Rno columns.
10
Sorting a join

For each branch, list numbers and names of staff who


manage properties, and properties they manage.

SELECT s.Bno, s.Sno, s.fName, s.lName, p.Pno


FROM Staff s, Property_For_Rent p
WHERE s.Sno = p.Sno
ORDER BY s.Bno, s.Sno, p.Pno;

Bno Sno Pno

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;

Bno Sno Pno

12
Multiple Grouping Columns

Find number of properties handled by each staff


member.
SELECT s.Bno, s.Sno, COUNT(*) AS myCount
FROM Staff s, Property_For_Rent p
WHERE s.Sno = p.Sno
GROUP BY s.Bno, s.Sno
ORDER BY s.Bno, s.Sno;

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).

• The FULL OUTER JOIN keyword combines the result of both


LEFT and RIGHT joins.

• 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

• The INNER JOIN keyword selects all rows from both


tables as long as there is a match between the columns
in both tables.

• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

16
Inner Join

• The (inner) join of these two tables:


SELECT b.Bno AS Bno,
Branch Property_For_Rent
b.city AS bcity, Bno Pno
p.Pno AS Pno,
p.city AS pcity
FROM Branch b
There are no rows corresponding to
Inner Join Property_For_Rent p branches in Bristol and Aberdeen.

ON b.city = p.city; Bno Pno

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

List branches and properties in same city and any unmatched


properties.
SELECT b.Bno AS Bno, The RIGHT JOIN keyword returns all rows from
b.city AS bcity, the right table (table2), with the matching rows in
the left table (table1). The result is NULL in the left
p.Pno AS Pno, side when there is no match.
p.city AS pcity Columns from first table are filled with NULLs.
FROM branch b
RIGHT JOIN property_for_rent p
ON b.city = p.city;
Bno Pno

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.

• Produce a simple true/false result.

• True if and only if there exists at least one row in the 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:


21
– (SELECT * ...)
Query using EXISTS
Find all staff who work in a London branch.
SELECT Sno, fName, lName, position
FROM Staff s
WHERE EXISTS
(SELECT *
FROM Branch b
WHERE s.Bno = b.Bno AND
b.city = 'London');

Sno

22
Query using EXISTS
• Note, search condition s.Bno = b.Bno 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 Sno, fName, lName, position
FROM Staff • Could also write this query using join construct:
SELECT Sno, fName, lName, position
WHERE true;
FROM Staff s, Branch b
WHERE s.Bno = b.Bno AND
city = 'London'; 23
Union, Intersect, and
Difference (Except)
• Union, Intersection, and Difference operations are used to
combine results of two or more tables 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. 24
Union, Intersect, and
Difference (Except)
• Format of set operator clause in each case is:
op [ALL] [CORRESPONDING [BY {column1 [, ...]}]]

• If CORRESPONDING BY specified, the set


operation performed on the named column(s).

• If CORRESPONDING specified but not BY clause,


the operation performed on common columns.

• If ALL specified, the result can include duplicate


rows.
25
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 Property_For_Rent
WHERE city IS NOT NULL); Produces the result tables
OR from both queries and
(SELECT * merges both tables together.
FROM Branch
WHERE city IS NOT NULL)
UNION CORRESPONDING BY city
(SELECT *
FROM Property_For_Rent
WHERE city IS NOT NULL);
This statement is not working in MySQL, however, it is in 26
some other versions of SQL.
Use of INTERSECT

List all cities where there is both a branch office and


a property.

(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

• Could rewrite this query without INTERSECT


operator:
SELECT b.city
FROM Branch b, Property_For_Rent p
WHERE b.city = p.city;
• Or:
SELECT DISTINCT city
FROM Branch b
WHERE EXISTS
(SELECT * FROM Property_For_Rent p
WHERE p.city = b.city); 28
Use of EXCEPT

List of all cities where there is a branch office


but no rental properties. (SELECT city FROM branch)
DIFFERENCE
(SELECT city FROM Branch) (SELECT city FROM
EXCEPT property_for_rent)

(SELECT city FROM Property_For_Rent);


The CORRESPONDING keyword is used only
when a set operator is specified. CORR causes
• Or PROC SQL to match the columns in table-
expressions by name and not by ordinal position.
(SELECT * FROM Branch)
EXCEPT CORRESPONDING BY city
(SELECT * FROM Property_For_Rent);
29
DML Basic Commands

We will now describe three Data Manipulation


Language (DML) SQL statements that are
available to modify the contents of the tables in
the database:

INSERT Adds new rows of data to a table


UPDATE Modifies existing data in a table
DELETE Removes rows of data from a table
30
INSERT

INSERT INTO TableName [ (columnList) ]


VALUES (dataValueList)

• columnList is optional; if omitted, SQL assumes a


list of all columns in their original CREATE
TABLE order.

• Any columns omitted must have been declared as


NULL when table was created, unless DEFAULT
was specified when creating column.
31
INSERT

• dataValueList must match columnList as


follows:

– number of items in each list must be same.

– must have a direct correspondence in position of


items in two lists.

– data type of each item in dataValueList must be


compatible with data type of corresponding column.
32
INSERT … VALUES

Insert a new row into Staff table supplying data


for all columns.
INSERT INTO staff

VALUES ('SG17', 'Alan', 'Brown',

'67 Endrick Rd, Glasgow G32 8QX', '0141-211-3001',


'Assistant', 'M', '2013-05-23', 8300, 'WN848391H', 'B3');
33
INSERT using Defaults

Insert a new row into Staff table supplying data for


all mandatory columns.
INSERT INTO Staff (Sno, fName, lName, position, salary, Bno, Sex,
DoB, NIN)
VALUES ('SG45', 'Derek', 'Monahan', 'Manager', 20000, 'B3', 'M',
'1975-05-23', 'DEF');

Or
INSERT INTO Staff VALUES ('SG44', 'Anne',
'Jones','Bradford', '44-435-567', 'Assistant', 'M', '1975-05-
23', 8100, 'PD2324032', 'B8'); 34
INSERT … SELECT

• Second form of INSERT allows multiple rows


to be copied from one or more tables to
another:

INSERT INTO TableName [ (columnList) ]


SELECT ...

35
INSERT … SELECT

Assume there is a table StaffPropCount that


contains names of staff and number of
properties they manage:
StaffPropCount(Sno, Fname, Lname, PropCnt)

Populate StaffPropCount using Staff and


Property_For_Rent tables.

Create table StaffPropCount (Sno varchar(4), Fname varchar(16),


Lname varchar(16), PropCnt INTEGER);
36
INSERT … SELECT
INSERT INTO StaffPropCount
(SELECT s.Sno, fName, lName,
COUNT(*)
FROM Staff s, Property_For_Rent p
WHERE s.Sno = p.Sno If second part of UNION is
GROUP BY s.Sno, fName, lName)
omitted, excludes those staff
who currently do not manage
UNION any properties.
(SELECT Sno, fName, lName, 0
FROM Staff Sno
WHERE Sno NOT IN
(SELECT DISTINCT Sno
FROM Property_For_Rent));
37
UPDATE
UPDATE TableName

SET columnName1 = dataValue1

[, columnName2 = dataValue2...]

[WHERE searchCondition]

• TableName can be name of a base table or an updatable view.

• SET clause specifies names of one or more columns that are to be updated.

• WHERE clause is optional:

– if omitted, named columns are updated for all rows in table;

– if specified, only those rows that satisfy searchCondition are updated.


38
• New dataValue(s) must be compatible with data type for corresponding column.
UPDATE All Rows
Give all staff a 3% pay increase.
UPDATE Staff
SET salary = salary * 1.03; (1 + 3.0 / 100)
Give all Managers a 5% pay increase.
UPDATE Staff
SET salary = salary * 1.05
WHERE position = 'Manager';

UPDATE Multiple Columns

Promote David Ford (Sno = 'SG14') to Manager and


change his salary to £18,000.
UPDATE Staff
SET position = 'Manager', salary = 18000
WHERE Sno = 'SG14'; 39
DELETE

DELETE FROM TableName

[WHERE searchCondition]

• TableName can be name of a base table or an


updatable view.

• searchCondition is optional; if omitted, all rows are


deleted from table. This does not delete table. If
search_condition is specified, only those rows that
satisfy condition are deleted.
40
DELETE Specific Rows

Delete all viewings that relate to property PG4.

DELETE FROM Viewing


WHERE Pno = 'PG4';

Delete all records from the Viewing table.

DELETE FROM Viewing;

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

You might also like