Вы находитесь на странице: 1из 56

Database System

EC601

ISMA SHAMSURIA BT ISMAIL


Department of Electrical Engineering (LEVEL 1) The Polytechnic of Merlimau, Melaka 019-4211728 ext:1163

CHAPTER 3.3.5
STURUCTURE QUERY LANGUAGE (SQL)
CLO 1 : Explain the basic concepts of database model using entityrelationship diagram (ERD) and translating completed data

models by applying normalization technique in logical database


designs. CLO 2 : Apply Structured Query Language (SQL) for database manipulation using a database management system in practical works inclusive of a report within stipulated time frame.

INTRODUCTION OF SQL

SQL, or Structured Query Language, is a language that is used to manipulate and report the data in relational databases. SQL is an open standard database language, supported by ANSI (American National Standards Institute). SQL does not just work with Microsoft Access or even with just Microsoft products - Oracle, Sybase, Microsoft SQL Server, MySQL, IBM DB2, Microsoft Access, and Lotus Approach, and many others all support SQL.

INTRODUCTION OF SQL
SQL provides 1. A data denition language (DDL) 2. A data manipulation language (DML) 3. A data control language (DCL) In addition SQL Can be used from inside other languages. (e.g. PHP or Java) Is often extended to provide common programming constructs (such as if-then tests, loops, variables, etc.)

FEATURES OF SQL
1. Standardized (with some variation). 2. Written language. 3. Can be used in programming code (Visual BASIC, C++, Java, etc.). 4. Any QBE command can be expressed in SQL. 5. SQL can be used to modify as well as query a database.

DATA MANIPULATION (DML)


SELECT INSERT UPDATE DELETE
to query data in the database

to insert data into a table

to update data in a table

to delete data from a table

DATA MANIPULATION

SELECT is the most frequently used SQL command and has the following general form:

SELECT [DISTINCT | ALL] {* | [columnExpression [AS newName]] [, . . . ]}

FROM TableName [alias] [, . . . ] [WHERE condition] [ORDER BY columnList] [GROUP BY columnList] ***DISTINCT - To eliminate duplicates from the list of values [HAVING condition] being reported.

DATA MANIPULATION
SYNTAX
SELECT FROM [ WHERE ]

PURPOSE
specifies which columns are to appear in the output specifies the table or tables to be used filters the rows subject to some condition specifies the order of the output forms groups of rows with the same column value filters the groups subject to some condition

[ ORDER BY ]
[ GROUP BY ] [ HAVING ]

EXAMPLE OF USES DATA MANIPULATION

SELECT FROM WHERE ORDER BY GROUP BY HAVING

Fname, Lname, Age STAFF Program=DTK Regist_No Program HPNM>=3;

RETRIEVE ALL COLUMN, ALL ROWS


QUESTION List full details of all staff.

RETRIEVE ALL COLUMN, ALL ROWS


SYNTAX SELECT staffNo, fName, lName, position, sex, DOB, salary, branchNo FROM Staff; OR
SELECT * FROM Staff;

RETRIEVE ALL COLUMN, ALL ROWS


RESULT

RETRIEVE SPECIFIC COLUMN, ALL ROWS


QUESTION Produce a list of salaries for all staff, showing only the staff number, the first and last names, and the salary details.

RETRIEVE SPECIFIC COLUMN, ALL ROWS


SYNTAX SELECT staffNo, fName, lName, salary FROM Staff;

RESULT

USE OF DISTINCT
QUESTION List the property numbers of all properties that have been viewed.

USE OF DISTINCT
SYNTAX

SELECT DISTINCT propertyNo FROM Viewing;

RESULT

CALCULATED FIELDS
QUESTION Produce a list of monthly salaries for all staff, showing the staff number, the first and last names, and the salary details.

CALCULATED FIELDS
SYNTAX SELECT staffNo, fName, lName, salary/12 FROM Staff;

RESULT

COMPARISON SEARCH CONDITION


QUESTION List all staff with a salary greater than RM10,000.

COMPARISON SEARCH CONDITION


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

RESULT

How to express the value depends on the data type?

Handling Values in Where Clauses


Data Type Text, Memo Instruction Example Enclose value in ' ' ='Canada' <15

Number, Currency, Just use value Autonumber Date/Time Yes/No

Enclose value in # =#12/25/1995#

#
Use True or False =True

COMPARISON SEARCH CONDITION

COMPARISON SEARCH CONDITION


More complex predicates can be generated using the logical operators AND, OR, and NOT, with parentheses (if needed or desired) to show the order of evaluation. The rules for evaluating a conditional expression are: an expression is evaluated left to right; subexpressions in brackets are evaluated first; NOTs are evaluated before ANDs and ORs; ANDs are evaluated before ORs.

COMPOUND COMPARISON SEARCH CONDITION


QUESTION List the addresses of all branch offices in London or Glasgow.

COMPOUND COMPARISON SEARCH CONDITION


SYNTAX
SELECT * FROM Branch WHERE city = London OR city = Glasgow;

RESULT

RANGE SEARCH CONDITION (BETWEEN/NOT BETWEEN) QUESTION List all staff with a salary between RM20,000 and RM30,000.

RANGE SEARCH CONDITION (BETWEEN/NOT BETWEEN

SYNTAX
SELECT staffNo, fName, lName, position, salary FROM Staff WHERE salary BETWEEN 20000 AND 30000;

OR
SELECT staffNo, fName, lName, position, salary FROM Staff WHERE salary > = 20000 AND salary < = 30000;

RANGE SEARCH CONDITION (BETWEEN/NOT BETWEEN

RESULT

SET MEMBERSHIP SEARCH CONDITION (IN/NOT IN)


QUESTION List all managers and supervisors.

SET MEMBERSHIP SEARCH CONDITION (IN/NOT IN)

SYNTAX
SELECT staffNo, fName, lName, position FROM Staff WHERE position IN (Manager, Supervisor);

0R
SELECT staffNo, fName, lName, position FROM Staff WHERE position = Manager OR position = Supervisor;

SET MEMBERSHIP SEARCH CONDITION (IN/NOT IN)

RESULT

PATTERN MATCH SEARCH CONDITION (LIKE/NOT LIKE)

SQL has two special pattern-matching symbols: % percent character represents any sequence of zero or more characters (wildcard). _ underscore character represents any single character.

PATTERN MATCH SEARCH CONDITION (LIKE/NOT LIKE)


address LIKE H% means the first character must be H, but the rest of the string can be anything. address LIKE H_ _ _ means that there must be exactly four characters in the string, the first of which must be an H. address LIKE %e means any sequence of characters, of length at least 1, with the last character an e. address LIKE %Glasgow% means a sequence of characters of any length containing Glasgow. address NOT LIKE H% means the first character cannot be an H.

PATTERN MATCH SEARCH CONDITION (LIKE/NOT LIKE)

QUESTION Find all owners with the string


Glasgow in their address.

PATTERN MATCH SEARCH CONDITION (LIKE/NOT LIKE)

SYNTAX
SELECT ownerNo, fName, lName, address, telNo FROM PrivateOwner WHERE address LIKE %Glasgow%;

RESULT

NULL SEARCH CONDITION (IS NULL/IS NOT NULL)


QUESTION List the details of all viewings on property PG4 where a comment has not been supplied.

NULL SEARCH CONDITION (IS NULL/IS NOT NULL)

SYNTAX
SELECT clientNo, viewDate FROM Viewing WHERE propertyNo = PG4 AND comment IS NULL;

RESULT

USING THE SQL AGGREGATE FUNCTIONS

COUNT

returns the number of values in a specified column returns the sum of the values in a specified column returns the average of the values in a specified column returns the smallest value in a specified column returns the largest value in a specified column

SUM
AVG

MIN
MAX

USE OF COUNT(*)
QUESTION How many properties cost more than RM350 per month to rent?

USE OF COUNT(*)
SYNTAX
SELECT COUNT(*) AS myCount FROM PropertyForRent WHERE rent > 350;

RESULT

USE OF COUNT(DISTINCT)
QUESTION How many different properties were viewed in May 2004?

USE OF COUNT(DISTINCT)
SYNTAX
SELECT COUNT(DISTINCT propertyNo) AS myCount FROM Viewing WHERE viewDate BETWEEN 1-May-04 AND 31-May-04;

RESULT

USE OF COUNT AND SUM


QUESTION Find the total number of Managers and the sum of their salaries.

USE OF COUNT AND SUM


SYNTAX
SELECT COUNT(staffNo) AS myCount, SUM(salary) AS mySum FROM Staff WHERE position = Manager;

RESULT

USE OF MIN, MAX, AVG


QUESTION Find the minimum, maximum, and
average staff salary.

USE OF MIN, MAX, AVG


SYNTAX
SELECT MIN(salary) AS myMin, MAX(salary) AS myMax, AVG(salary) AS myAvg FROM Staff;

RESULT

SINGLE-COLUMN ORDERING
QUESTION Produce a list of salaries for all staff, arranged in descending order of salary.

SINGLE-COLUMN ORDERING
SYNTAX
SELECT staffNo, fName, lName, salary FROM Staff ORDER BY salary DESC;

RESULT

MULTIPLE COLUMN ORDERING


QUESTION Produce an abbreviated list of properties arranged in order of property type.

MULTIPLE COLUMN ORDERING


SYNTAX SELECT propertyNo, type, rooms, rent FROM PropertyForRent ORDER BY type; RESULT

MULTIPLE COLUMN ORDERING


QUESTION Produce an abbreviated list of properties arranged in order of property type.

MULTIPLE COLUMN ORDERING


SYNTAX SELECT propertyNo, type, rooms, rent FROM PropertyForRent ORDER BY type, rent DESC; RESULT

USE OF GROUP BY
QUESTION Find the number of staff working in
each branch and the sum of their salaries.

USE OF GROUP BY
SYNTAX
SELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS mySum FROM Staff GROUP BY branchNo ORDER BY branchNo;

RESULT

USE OF HAVING
QUESTION For each branch office with more than one member of staff, find the number of staff working in each branch and the sum of their salaries.

USE OF HAVING
SYNTAX
SELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS mySum FROM Staff GROUP BY branchNo HAVING COUNT(staffNo) > 1 ORDER BY branchNo;

RESULT

Вам также может понравиться