Chapter 3.3.5-SQL Dml

56
Database System EC601 ISMA SHAMSURIA BT ISMAIL Department of Electrical Engineering (LEVEL 1) The Polytechnic of Merlimau, Melaka 019-4211728 ext:1163

description

Chapter 3.3.5-SQL DmlPoliteknik Merlimau MelakaEC601 Database System

Transcript of Chapter 3.3.5-SQL Dml

Page 1: Chapter 3.3.5-SQL Dml

Database System EC601 

ISMA SHAMSURIA BT ISMAILDepartment of Electrical Engineering (LEVEL 1)

The Polytechnic of Merlimau, Melaka019-4211728

ext:1163

Page 2: Chapter 3.3.5-SQL Dml

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. 

Page 3: Chapter 3.3.5-SQL Dml

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.

Page 4: Chapter 3.3.5-SQL Dml

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

Page 5: Chapter 3.3.5-SQL Dml

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.

Page 6: Chapter 3.3.5-SQL Dml

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

Page 7: Chapter 3.3.5-SQL Dml

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.

Page 8: Chapter 3.3.5-SQL Dml

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

Page 9: Chapter 3.3.5-SQL Dml

EXAMPLE OF USES DATA MANIPULATION

SELECT Fname, Lname, AgeFROM STAFFWHERE Program=DTKORDER BY Regist_NoGROUP BY ProgramHAVING HPNM>=3;

Page 10: Chapter 3.3.5-SQL Dml

RETRIEVE ALL COLUMN, ALL ROWS

QUESTION List full details of all staff.

Page 11: Chapter 3.3.5-SQL Dml

RETRIEVE ALL COLUMN, ALL ROWS

SYNTAX

SELECT staffNo, fName, lName, position, sex, DOB, salary, branchNoFROM Staff;

SELECT *FROM Staff;

OR

Page 12: Chapter 3.3.5-SQL Dml

RETRIEVE ALL COLUMN, ALL ROWS

RESULT

Page 13: Chapter 3.3.5-SQL Dml

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.

Page 14: Chapter 3.3.5-SQL Dml

RETRIEVE SPECIFIC COLUMN, ALL ROWS

SYNTAX

SELECT staffNo, fName, lName, salaryFROM Staff;

RESULT

Page 15: Chapter 3.3.5-SQL Dml

USE OF DISTINCT

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

Page 16: Chapter 3.3.5-SQL Dml

USE OF DISTINCT

SYNTAX SELECT DISTINCT propertyNoFROM Viewing;

RESULT

Page 17: Chapter 3.3.5-SQL Dml

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.

Page 18: Chapter 3.3.5-SQL Dml

CALCULATED FIELDS

SYNTAXSELECT staffNo, fName, lName, salary/12FROM Staff;

RESULT

Page 19: Chapter 3.3.5-SQL Dml

COMPARISON SEARCH CONDITION

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

Page 20: Chapter 3.3.5-SQL Dml

COMPARISON SEARCH CONDITION

SYNTAXSELECT staffNo, fName, lName, position, salaryFROM StaffWHERE salary > 10000;

RESULT

Page 21: Chapter 3.3.5-SQL Dml

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?

Page 22: Chapter 3.3.5-SQL Dml

COMPARISON SEARCH CONDITION

Page 23: Chapter 3.3.5-SQL Dml

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.

Page 24: Chapter 3.3.5-SQL Dml

COMPOUND COMPARISON SEARCH CONDITION

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

Page 25: Chapter 3.3.5-SQL Dml

COMPOUND COMPARISON SEARCH CONDITION

SYNTAXSELECT *FROM BranchWHERE city = ‘London’ OR city = ‘Glasgow’;

RESULT

Page 26: Chapter 3.3.5-SQL Dml

RANGE SEARCH CONDITION (BETWEEN/NOT BETWEEN)

QUESTION List all staff with a salary between RM20,000 and RM30,000.

Page 27: Chapter 3.3.5-SQL Dml

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;

Page 28: Chapter 3.3.5-SQL Dml

RANGE SEARCH CONDITION (BETWEEN/NOT BETWEEN

RESULT

Page 29: Chapter 3.3.5-SQL Dml

SET MEMBERSHIP SEARCH CONDITION (IN/NOT IN)

QUESTION List all managers and supervisors.

Page 30: Chapter 3.3.5-SQL Dml

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’;

Page 31: Chapter 3.3.5-SQL Dml

SET MEMBERSHIP SEARCH CONDITION (IN/NOT IN)

RESULT

Page 32: Chapter 3.3.5-SQL Dml

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.

Page 33: Chapter 3.3.5-SQL Dml

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.

Page 34: Chapter 3.3.5-SQL Dml

PATTERN MATCH SEARCH CONDITION (LIKE/NOT LIKE)

QUESTION Find all owners with the string ‘Glasgow’ in their address.

Page 35: Chapter 3.3.5-SQL Dml

PATTERN MATCH SEARCH CONDITION (LIKE/NOT LIKE)

SYNTAXSELECT ownerNo, fName, lName, address, telNoFROM PrivateOwnerWHERE address LIKE ‘%Glasgow%’;

RESULT

Page 36: Chapter 3.3.5-SQL Dml

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.

Page 37: Chapter 3.3.5-SQL Dml

NULL SEARCH CONDITION (IS NULL/IS NOT NULL)

SYNTAXSELECT clientNo, viewDateFROM ViewingWHERE propertyNo = ‘PG4’ AND comment IS NULL;

RESULT

Page 38: Chapter 3.3.5-SQL Dml

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

Page 39: Chapter 3.3.5-SQL Dml

USE OF COUNT(*)

QUESTION How many properties cost more than RM350 per month to rent?

Page 40: Chapter 3.3.5-SQL Dml

USE OF COUNT(*)

SYNTAX SELECT COUNT(*) AS myCountFROM PropertyForRentWHERE rent > 350;

RESULT

Page 41: Chapter 3.3.5-SQL Dml

USE OF COUNT(DISTINCT)

QUESTION How many different properties were viewed in May 2004?

Page 42: Chapter 3.3.5-SQL Dml

USE OF COUNT(DISTINCT)

SYNTAXSELECT COUNT(DISTINCT propertyNo) AS myCountFROM ViewingWHERE viewDate BETWEEN ‘1-May-04’ AND ‘31-May-04’;

RESULT

Page 43: Chapter 3.3.5-SQL Dml

USE OF COUNT AND SUM

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

Page 44: Chapter 3.3.5-SQL Dml

USE OF COUNT AND SUM

SYNTAXSELECT COUNT(staffNo) AS myCount, SUM(salary) AS mySumFROM StaffWHERE position = ‘Manager’;

RESULT

Page 45: Chapter 3.3.5-SQL Dml

USE OF MIN, MAX, AVG

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

Page 46: Chapter 3.3.5-SQL Dml

USE OF MIN, MAX, AVG

SYNTAXSELECT MIN(salary) AS myMin, MAX(salary) AS myMax, AVG(salary) AS myAvgFROM Staff;

RESULT

Page 47: Chapter 3.3.5-SQL Dml

SINGLE-COLUMN ORDERING

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

Page 48: Chapter 3.3.5-SQL Dml

SINGLE-COLUMN ORDERING

SYNTAX SELECT staffNo, fName, lName, salaryFROM StaffORDER BY salary DESC;

RESULT

Page 49: Chapter 3.3.5-SQL Dml

MULTIPLE COLUMN ORDERING

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

Page 50: Chapter 3.3.5-SQL Dml

MULTIPLE COLUMN ORDERING

SYNTAX

SELECT propertyNo, type, rooms, rentFROM PropertyForRentORDER BY type;

RESULT

Page 51: Chapter 3.3.5-SQL Dml

MULTIPLE COLUMN ORDERING

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

Page 52: Chapter 3.3.5-SQL Dml

MULTIPLE COLUMN ORDERING

SYNTAX

SELECT propertyNo, type, rooms, rentFROM PropertyForRentORDER BY type, rent DESC;

RESULT

Page 53: Chapter 3.3.5-SQL Dml

USE OF GROUP BY

QUESTION Find the number of staff working in each branch and the sum of their salaries.

Page 54: Chapter 3.3.5-SQL Dml

USE OF GROUP BY

SYNTAXSELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS mySumFROM StaffGROUP BY branchNoORDER BY branchNo;

RESULT

Page 55: Chapter 3.3.5-SQL Dml

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.

Page 56: Chapter 3.3.5-SQL Dml

USE OF HAVING

SYNTAXSELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS mySumFROM StaffGROUP BY branchNoHAVING COUNT(staffNo) > 1ORDER BY branchNo;

RESULT