W3 SQL Recap
W3 SQL Recap
W3 SQL Recap
5-3
Advanced Database Systems
If you have mastered this topic, you should be able to use the
following terms correctly in your assignments and exams:
If you have mastered this topic, you should be able to use the following terms correctly in your assignments and
exams:
Produce a list of salaries for all staff, showing only staff number, first and last names, and salary.
List the property numbers of all properties that have been viewed.
SELECT propertyNo
FROM Viewing;
Produce a list of monthly salaries for all staff, showing staff number, first and last names,
and salary details.
SELECT *
FROM Branch
WHERE city = ‘London’ OR city = ‘Glasgow’;
List details of all viewings on property PG4 where a comment has not been
supplied.
• There are 2 viewings for property PG4, one with and one without a comment.
• Have to test for null explicitly using special keyword IS NULL:
SELECT clientNo, viewDate
FROM Viewing
WHERE propertyNo = ‘PG4’ AND
comment IS NULL;
• Four flats in this list - as no minor sort key specified, system arranges these
rows in any order it chooses.
• To arrange in order of rent, specify minor order:
• Aggregate functions can be used only in SELECT list and in HAVING clause.
How many properties cost more than £350 per month to rent?
SELECT COUNT(*) AS count
FROM PropertyForRent
WHERE rent > 350;
• All column names in SELECT list must appear in GROUP BY clause unless
name is used only in an aggregate function.
• If WHERE is used with GROUP BY, WHERE is applied first, then groups are
formed from remaining rows satisfying predicate.
• ISO considers two nulls to be equal for purposes of GROUP BY.
SUM(salary) AS sum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;
• HAVING clause is designed for use with GROUP BY to restrict groups that
appear in final result table.
• Similar to WHERE, but WHERE filters individual rows whereas HAVING
filters groups.
• Column names in HAVING clause must also appear in the GROUP BY list or
be contained within an aggregate function.
SELECT branchNo,
COUNT(staffNo) AS count,
SUM(salary) AS sum
FROM Staff
GROUP BY branchNo
HAVING COUNT(staffNo) > 1
ORDER BY branchNo;
• Inner SELECT finds branch number for branch at ‘163 Main St’ (‘B003’).
• Outer SELECT then retrieves details of all staff who work at this branch.
• Outer SELECT then becomes:
SELECT staffNo, fName, lName, position
FROM Staff
WHERE branchNo = ‘B003’;
List all staff whose salary is greater than the average salary, and show by how much.
• Can use subqueries provided result columns come from same table.
• If result 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).
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.
• In each case, FROM replaces original FROM and WHERE. However, first
produces table with two identical clientNo columns.
For each branch, list numbers and names of staff who manage properties, and
properties they manage.
For each branch, list staff who manage properties, including city in which branch is located
and properties they manage.
4. If DISTINCT has been specified, eliminate any duplicate rows from the result
table.
• 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.
(SELECT * ...)
Insert a new row into Staff table supplying data for all columns.
Insert a new row into Staff table supplying data for all mandatory columns.
INSERT INTO Staff (staffNo, fName, lName,
position, salary, branchNo)
VALUES (‘SG44’, ‘Anne’, ‘Jones’,
‘Assistant’, 8100, ‘B003’);
• Or
INSERT INTO Staff
VALUES (‘SG44’, ‘Anne’, ‘Jones’, ‘Assistant’, NULL,
NULL, 8100, ‘B003’);
• Second form of INSERT allows multiple rows to be copied from one or more tables to
another:
Assume there is a table StaffPropCount that contains names of staff and number of
properties they manage:
StaffPropCount(staffNo, fName, lName, propCnt)
• If second part of UNION is omitted, excludes those staff who currently do not manage
any properties.
UPDATE TableName
SET columnName1 = dataValue1
[, columnName2 = dataValue2...]
[WHERE searchCondition]
UPDATE Staff
SET salary = salary*1.03;
UPDATE Staff
SET position = ‘Manager’, salary = 18000
WHERE staffNo = ‘SG14’;
Q&A