Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

DBDI / SQL 30/05/2007

Lecture’s Objectives /1 - SQL


• Purpose and importance of SQL.
• How to retrieve data from database using
SELECT and:
– Use compound WHERE conditions.
DBDI/ Lecture 8 – Sort query results using ORDER BY.
Relational DB Languages – Join tables together.
SQL – Use of Aliases
Dr. Ala Al-Zobaidie
The slides are based on the textbook Database Systems by Connolly & Begg

30/05/2007 DBDI / SQL 2

Purpose and importance /1 - SQL Purpose and importance /2 - SQL


• DB Language:
– Create the DB & relation structures; • SQL is a transform-oriented language with
– Perform various operations 2 major components:
– Handle simple & complicated queries – DDL
– DML
• Perform these tasks with: • SQL did not contain flow of control
– minimal user effort &
commands.
– command structure/syntax must be easy to
learn. – Programming,
– job-control language,
• Must be portable. – interactively by the decisions of user.
30/05/2007 DBDI / SQL 3 30/05/2007 DBDI / SQL 4

Objectives of SQL /1 Objectives of SQL /2


Consists of standard English words:
• SQL is relatively easy to learn:
CREATE TABLE Staff(staffNo VARCHAR(5),
– non-procedural - you specify what lName VARCHAR(15),
information you require, rather than how to salary DECIMAL(7,2));
get it;
– it is essentially free-format. INSERT INTO Staff VALUES ('SG16', 'Brown',
8300);

SELECT staffNo, lName, salary


FROM Staff
WHERE salary > 10000;

30/05/2007 DBDI / SQL 5 30/05/2007 DBDI / SQL 6

Lecture 8 1
DBDI / SQL 30/05/2007

Objectives of SQL /3 History of SQL


• In 1974Æ 'Structured English Query Language'
• Can be used by range of users including DBAs,
(SEQUEL).
management, application developers, and other
types of end users. • 1976 Æ SEQUEL/2
• IBM produced a prototype DBMS called System R,
based on SEQUEL/2.
• An ISO standard now exists for SQL, making it • Roots of SQL predates System R project.
both the formal and de facto standard language • In late 70s Æ ORACLE.
for relational databases. • In 1987 Æ ANSI & ISO
• In 1989, ISO Æ 'Integrity Enhancement Feature'.
• In 1992 Æ SQL2 or SQL/92.
• In 1999 Æ SQL3

30/05/2007 DBDI / SQL 7 30/05/2007 DBDI / SQL 8

Importance of SQL Writing SQL Commands /1


• Part of application architectures SQL statement consists of reserved words
and user-defined words.
• It is strategic choice of many large and
– Reserved words
organizations
– fixed part of SQL & must be spelt exactly as
required & cannot be split across lines.
• SQL is Federal Information Processing
Standard (FIPS)
– User-defined words
• SQL is used in other standards (e.g. IRDS – made up by user & represent names of
various database objects such as relations,
& RDA Standards).
columns, views.
30/05/2007 DBDI / SQL 9 30/05/2007 DBDI / SQL 10

Writing SQL Commands /2 SELECT Statement


• case insensitive except literal
• indentation & lineation
SELECT [DISTINCT | ALL]
• extended BNF notation
– Upper-case letters Æ Reserved Words. {* | [columnExpression [AS newName]] [,...] }
– Lower-case letters Æ user-defined words. FROM TableName [alias] [, ...]
– | Æ a choice
– { } Æ a required element
[WHERE condition]
– [ ] Æ a optional element [GROUP BY columnList] [HAVING condition]
– … Æ repetition [ORDER BY columnList]
• Literals are constants used in SQL statements.
– non-numeric literals Æ enclosed in single quotes
(e.g. ‘London’).
– numeric literals Æ not be enclosed in quotes (e.g.
650.00).
30/05/2007 DBDI / SQL 11 30/05/2007 DBDI / SQL 12

Lecture 8 2
DBDI / SQL 30/05/2007

Example 5.1 All Columns, All Rows Example 5.1 All Columns, All Rows

List full details of all staff.

SELECT staffNo, fName, lName, address,


position, sex, DOB, salary, branchNo
FROM Staff;
• Can use * as an abbreviation for 'all columns':

SELECT *
FROM Staff;

30/05/2007 DBDI / SQL 13 30/05/2007 DBDI / SQL 14

Example 5.2 Specific Columns, All Example 5.2 Specific Columns, All
Rows Rows
Produce a list of salaries for all staff,
showing only staff number, first and last
names, and salary.

SELECT staffNo, fName, lName, salary


FROM Staff;

30/05/2007 DBDI / SQL 15 30/05/2007 DBDI / SQL 16

Example 5.3 Use of DISTINCT Example 5.3 Use of DISTINCT

List the property numbers of all properties • Use DISTINCT to eliminate duplicates:
that have been viewed. SELECT DISTINCT propertyNo
FROM Viewing;
SELECT propertyNo
FROM Viewing;

30/05/2007 DBDI / SQL 17 30/05/2007 DBDI / SQL 18

Lecture 8 3
DBDI / SQL 30/05/2007

Example 5.4 Calculated Fields Example 5.4 Calculated Fields

Produce a list of monthly salaries for all staff, • To name column, use AS clause:
showing staff number, first and last names, and
salary details.
SELECT staffNo, fName, lName, salary/12 SELECT staffNo, fName, lName,
FROM Staff; salary/12 AS monthlySalary
FROM Staff;

30/05/2007 DBDI / SQL 19 30/05/2007 DBDI / SQL 20

Example 5.5 Comparison Search Example 5.6 Compound Comparison


Condition Search Condition
List all staff with a salary greater than 10,000. List addresses of all branch offices in
SELECT staffNo, fName, lName, position, salary London or Glasgow.
FROM Staff SELECT *
WHERE salary > 10000;
FROM Branch
WHERE city = 'London' OR city = 'Glasgow';

30/05/2007 DBDI / SQL 21 30/05/2007 DBDI / SQL 22

Example 5.7 Range Search Condition Example 5.7 Range Search Condition

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;

• BETWEEN test includes the endpoints of range.

30/05/2007 DBDI / SQL 23 30/05/2007 DBDI / SQL 24

Lecture 8 4
DBDI / SQL 30/05/2007

Example 5.7 Range Search Condition Example 5.8 Set Membership

• Also a negated version NOT BETWEEN. List all managers and supervisors.
• BETWEEN does not add much to SQL's
SELECT staffNo, fName, lName, position
expressive power Could also write:
FROM Staff
SELECT staffNo, fName, lName, position, WHERE position IN ('Manager', ‘Supervisor');
salary
FROM Staff
WHERE salary>=20000 AND salary <= 30000;

• Useful, though, for a range of values.

30/05/2007 DBDI / SQL 25 30/05/2007 DBDI / SQL 26

Example 5.8 Set Membership Example 5.9 Pattern Matching


• There is a negated version (NOT IN). Find all owners with the string 'Glasgow' in their
• IN does not add much to SQL's expressive address.
power.
• Could have expressed this as: SELECT clientNo, fName, lName, address, telNo
FROM PrivateOwner
SELECT staffNo, fName, lName, position WHERE address LIKE '%Glasgow%';
FROM Staff
WHERE position='Manager' OR
position=‘Supervisor';
• IN is more efficient when set contains many
values.

30/05/2007 DBDI / SQL 27 30/05/2007 DBDI / SQL 28

Example 5.9 Pattern Matching Example 5.10 NULL Search Condition


List details of all viewings on property PG4 where
• SQL has two special pattern matching a comment has not been supplied.
symbols:
• There are 2 viewings for property PG4, one with
– %: sequence of zero or more characters; and one without a comment.
– _ (underscore): any single character. • Have to test for null explicitly using special
keyword IS NULL:
• LIKE '%Glasgow%' means a sequence of
characters of any length containing SELECT clientNo, viewDate
'Glasgow'. FROM Viewing
WHERE propertyNo = 'PG4' AND
comment IS NULL;
30/05/2007 DBDI / SQL 29 30/05/2007 DBDI / SQL 30

Lecture 8 5
DBDI / SQL 30/05/2007

Example 5.10 NULL Search Condition Example 5.11 Single Column Ordering

List salaries for all staff, arranged in


descending order of salary.

SELECT staffNo, fName, lName, salary


FROM Staff
ORDER BY salary DESC;
• Negated version (IS NOT NULL) can test
for non-null values.

30/05/2007 DBDI / SQL 31 30/05/2007 DBDI / SQL 32

Example 5.11 Single Column Ordering Example 5.12 Multiple Column Ordering

Produce abbreviated list of properties in


order of property type.

SELECT propertyNo, type, rooms, rent


FROM PropertyForRent
ORDER BY type;

30/05/2007 DBDI / SQL 33 30/05/2007 DBDI / SQL 34

Example 5.12 Multiple Column Ordering Example 5.12 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:

SELECT propertyNo, type, rooms, rent


FROM PropertyForRent
ORDER BY type, rent DESC;

30/05/2007 DBDI / SQL 35 30/05/2007 DBDI / SQL 36

Lecture 8 6
DBDI / SQL 30/05/2007

Example 5.12 Multiple Column Ordering Using Aliases in SQL


List the name of the manager of the employee ALLEN.
(Ref Q4 of tutorial 7)

Emp (EmpNo, Ename, Job, Mgr, HireDate, Sal, Comm, DeptNo);

Select ename from emp


where empno = (
Select mgr from emp where ename =‘ALLEN’);
(return ‘BLAKE’)

Select m.ename from emp e, emp m


where m.empno = e.mgr
and e.ename= ‘ALLEN’;
(return ‘BLAKE’)

30/05/2007 DBDI / SQL 37 30/05/2007 DBDI / SQL 38

Summary
• Purpose of SQL
• History and Standards
• Basic Syntax of a Select Statement
• The basic 5 Search Conditions
• Examples from DreamHome
• Aliases
• Nested Query
30/05/2007 DBDI / SQL 39

Lecture 8 7

You might also like