0% found this document useful (0 votes)
12 views

5.0SQL PART1 A

Uploaded by

syafuedu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

5.0SQL PART1 A

Uploaded by

syafuedu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

STRUCTURED QUERY

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:

– DDL: for defining the d/b structure and


controlling access to data
– DML: for retrieving and updating data
3
SQL
 Itis a non-procedural language: you
specify what information you require,
rather than how to get it.

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

 An ISO standard now exists for SQL,


making it both the formal and de facto
standard language for relational databases.

6
Writing SQL Commands
 SQL statement consists of reserved words
and user-defined words.

– Reserved words are a fixed part of SQL and


must be spelt exactly as required and
cannot be split across lines.
– User-defined words are made up by user
and represent names of various database
objects such as relations, columns, views.
7
Writing SQL Commands
 Most components of an SQL statement are case
insensitive, except for literal character data.
 More readable with indentation and lineation:
– Each clause should begin on a new line.
– Start of a clause should line up with start of
other clauses.
– If clause has several parts, should each appear
on a separate line and be indented under start
of clause.

8
Literals
 Literals are constants used in SQL
statements.

 All non-numeric literals must be enclosed


in single quotes (e.g. ‘London’).

 All numeric literals must not be enclosed in


quotes (e.g. 650.00).

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

 UPDATE – to update data in a table

 DELETE – to delete data from a 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.

 Only SELECT and FROM are mandatory.

15
Simple Queries - Retrieve all columns, all
rows
 Command: List full details of all staff.

1. SELECT staffNo, fName, lName, address, position,


sex, DOB, salary, branchNo
FROM Staff;
2. Can use * as an abbreviation for 'all columns':
SELECT *
FROM Staff;

16
17
Simple Queries - Retrieve specific columns,
all rows
 Command: 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;

18
Simple Queries - Use of DISTINCT
 Command: List the property numbers of all
properties that have been viewed.
1. SELECT propertyNo
FROM Viewing;

2.Use DISTINCT to eliminate duplicates:

SELECT DISTINCT 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;

2. To name column, use AS clause:

SELECT staffNo, fName, lName, salary/12


AS monthlySalary
FROM Staff;

20
Simple Queries - Comparison Search
Condition
 Command: List all staff with a salary

greater than 10,000.


SELECT staffNo, fName, lName, position, salary
FROM Staff
WHERE salary > 10000;

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;

 BETWEEN test includes the endpoints of range. 23


Simple Queries - Range Search Condition
 Also a negated version NOT BETWEEN.
 BETWEEN does not add much to SQL's
expressive power Could also write:

SELECT staffNo, fName, lName, position, salary


FROM Staff
WHERE salary>=20000 AND salary <= 30000;

 Useful, though, for a range of values.

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

 Negated version (IS NOT NULL) can test


for non-null values.

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:

SELECT propertyNo, type, rooms, rent


FROM PropertyForRent
ORDER BY type, rent
DESC;

33

You might also like