5.0SQL PART1 A
5.0SQL PART1 A
LANGUAGE (SQL)
1
Database Language
Allow user to:
– create the database and relation structures
– perform basic data management tasks, such as the
insertion, modification, and deletion of data from the
relations
– perform both simple and complex queries
perform the above tasks with minimal user effort
command structure and syntax must be easy to
learn
must be portable; which conform to some standard
SQL 2
SQL
is an example of a transform-oriented
language, or language designed to use
relations to transform inputs into required
outputs
has 2 major components:
Free-format
4
SQL
Consists of standard English words:
– CREATE TABLE Staff(staffNo VARCHAR(5),
lName VARCHAR(15),
salary DECIMAL(7,2));
– INSERT INTO Staff VALUES ('SG16', 'Brown',
8300);
– SELECT staffNo, lName, salary
FROM Staff
WHERE salary > 10000;
5
SQL
Can be used by range of users including
DBAs, management, application
developers, and other types of end users.
6
Writing SQL Commands
SQL statement consists of reserved words
and user-defined words.
8
Literals
Literals are constants used in SQL
statements.
9
Writing SQL Commands
Use extended form of BNF notation:
– Upper-case letters represent reserved words.
– Lower-case letters represent user-defined words.
– a vertical bar (|) indicates a choice among
alternatives.
– Curly braces indicate a required element.
– Square brackets indicate an optional element.
– an ellipsis (…) indicates optional repetition (0 or
more).
10
SQL DML
SELECT – to query data in the database
INSERT – to insert data into table
11
SQL DML - SELECT
Simple queries
Sorting Results
Aggregate Function
Grouping Results
Subqueries
ANY and ALL
Multi-Table Queries
Combining Result Tables
12
Simple Queries
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM TableName [alias] [, ...]
[WHERE condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
13
SELECT Statement
FROM Specifies table(s) to be used.
WHERE Filters rows.
GROUP BY Forms groups of rows with same
column value.
HAVING Filters groups subject to some
condition.
SELECT Specifies which columns are to
appear in output.
ORDER BY Specifies the order of the output.
14
SELECT Statement
Order of the clauses cannot be changed.
15
Simple Queries - Retrieve all columns, all
rows
Command: List full details of all staff.
16
17
Simple Queries - Retrieve specific columns,
all rows
Command: Produce a list of salaries for all
18
Simple Queries - Use of DISTINCT
Command: List the property numbers of all
properties that have been viewed.
1. SELECT propertyNo
FROM Viewing;
19
Simple Queries - Calculated Fields
Command: Produce a list of monthly salaries for all staff,
showing staff number, first and last names, and salary
details.
1. SELECT staffNo, fName, lName, salary/12
FROM Staff;
20
Simple Queries - Comparison Search
Condition
Command: List all staff with a salary
21
Simple Queries - Compound Comparison
Search Condition
Command: List addresses of all branch offices in
London or Glasgow.
SELECT *
FROM Branch
WHERE city = 'London' OR city = 'Glasgow';
22
Simple Queries - Range Search Condition
Command: List all staff with a salary between
20,000 and 30,000.
SELECT staffNo, fName, lName, position, salary
FROM Staff
WHERE salary BETWEEN 20000 AND 30000;
24
Simple Queries - Set Membership
Command: List all managers and
supervisors.
SELECT staffNo, fName, lName, position
FROM Staff
WHERE position IN ('Manager', ‘Supervisor');
25
Simple Queries - Set Membership
There is a negated version (NOT IN).
IN does not add much to SQL's expressive
power.
Could have expressed this as:
SELECT staffNo, fName, lName, position
FROM Staff
WHERE position='Manager' OR
position=‘Supervisor';
IN is more efficient when set contains many
values.
26
Simple Queries - Pattern Matching
Command: Find all owners with the string
'Glasgow' in their address.
SELECT clientNo, fName, lName, address, telNo
FROM PrivateOwner
WHERE address LIKE '%Glasgow%';
27
Simple Queries - Pattern Matching
SQL has two special pattern matching
symbols:
– %: sequence of zero or more characters;
– _ (underscore): any single character.
LIKE '%Glasgow%' means a sequence of
characters of any length containing
'Glasgow'.
28
Simple Queries - NULL Search
Condition
Command: 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;
29
Simple Queries - NULL Search
Condition
30
Sorting results - Single Column
Ordering
Command: List salaries for all staff, arranged in
descending order of salary.
SELECT staffNo, fName, lName, salary
FROM Staff
ORDER BY salary DESC;
31
Sorting results - Multiple Column
Ordering
Command: Produce abbreviated list of properties
in order of property type.
SELECT propertyNo, type, rooms, rent
FROM PropertyForRent
ORDER BY type;
32
Sorting results - Multiple Column
Ordering
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:
33