Database System EC601
ISMA SHAMSURIA BT ISMAILDepartment of Electrical Engineering (LEVEL 1)
The Polytechnic of Merlimau, Melaka019-4211728
ext:1163
STURUCTURE QUERY LANGUAGE (SQL)
CHAPTER 3.3.5
CLO 1 : Explain the basic concepts of database model using entity-
relationship 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 definition 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• to query data in the
database
INSERT• to insert data into a
table
UPDATE • to update data in a table
DELETE• 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] [HAVING condition]***DISTINCT - To eliminate duplicates from the list of values
being reported.
DATA MANIPULATION
filters the groups subject to some condition
[ HAVING … ]
forms groups of rows with the same column value
[ GROUP BY … ]
specifies the order of the output[ ORDER BY … ]
filters the rows subject to some condition
[ WHERE … ]
specifies the table or tables to be used
FROM …
specifies which columns are to appear in the output
SELECT …
PURPOSESYNTAX
EXAMPLE OF USES DATA MANIPULATION
SELECT Fname, Lname, AgeFROM STAFFWHERE Program=DTKORDER BY Regist_NoGROUP BY ProgramHAVING 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, branchNoFROM Staff;
SELECT *FROM Staff;
OR
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, salaryFROM Staff;
RESULT
USE OF DISTINCT
QUESTION List the property numbers of all properties that have been viewed.
USE OF DISTINCT
SYNTAX SELECT DISTINCT propertyNoFROM 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
SYNTAXSELECT staffNo, fName, lName, salary/12FROM Staff;
RESULT
COMPARISON SEARCH CONDITION
QUESTION List all staff with a salary greater than RM10,000.
COMPARISON SEARCH CONDITION
SYNTAXSELECT staffNo, fName, lName, position, salaryFROM StaffWHERE salary > 10000;
RESULT
Handling Values in Where Clauses
Data Type Instruction Example
Text, Memo Enclose value in ' ' ='Canada'
Number, Currency, Autonumber
Just use value <15
Date/Time Enclose value in # #
=#12/25/1995#
Yes/No Use True or False =True
How to express the value depends on the data type?
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
SYNTAXSELECT *FROM BranchWHERE 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
SYNTAXSELECT staffNo, fName, lName, position, salaryFROM StaffWHERE salary BETWEEN 20000 AND 30000;
OR
SELECT staffNo, fName, lName, position, salaryFROM StaffWHERE 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, positionFROM StaffWHERE position IN (‘Manager’, ‘Supervisor’);
0R
SELECT staffNo, fName, lName, positionFROM StaffWHERE 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)
SYNTAXSELECT ownerNo, fName, lName, address, telNoFROM PrivateOwnerWHERE 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)
SYNTAXSELECT clientNo, viewDateFROM ViewingWHERE propertyNo = ‘PG4’ AND comment IS NULL;
RESULT
USING THE SQL AGGREGATE FUNCTIONS
COUNT • returns the number of values in a specified column
SUM • returns the sum of the values in a specified column
AVG • returns the average of the values in a specified column
MIN • returns the smallest value in a specified column
MAX • returns the largest value in a specified column
USE OF COUNT(*)
QUESTION How many properties cost more than RM350 per month to rent?
USE OF COUNT(*)
SYNTAX SELECT COUNT(*) AS myCountFROM PropertyForRentWHERE rent > 350;
RESULT
USE OF COUNT(DISTINCT)
QUESTION How many different properties were viewed in May 2004?
USE OF COUNT(DISTINCT)
SYNTAXSELECT COUNT(DISTINCT propertyNo) AS myCountFROM ViewingWHERE 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
SYNTAXSELECT COUNT(staffNo) AS myCount, SUM(salary) AS mySumFROM StaffWHERE position = ‘Manager’;
RESULT
USE OF MIN, MAX, AVG
QUESTION Find the minimum, maximum, and average staff salary.
USE OF MIN, MAX, AVG
SYNTAXSELECT MIN(salary) AS myMin, MAX(salary) AS myMax, AVG(salary) AS myAvgFROM 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, salaryFROM StaffORDER 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, rentFROM PropertyForRentORDER 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, rentFROM PropertyForRentORDER 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
SYNTAXSELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS mySumFROM StaffGROUP BY branchNoORDER 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
SYNTAXSELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS mySumFROM StaffGROUP BY branchNoHAVING COUNT(staffNo) > 1ORDER BY branchNo;
RESULT