Академический Документы
Профессиональный Документы
Культура Документы
EC601
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
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
SELECT is the most frequently used SQL command and has the following general form:
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 ]
RESULT
USE OF DISTINCT
QUESTION List the property numbers of all properties that have been viewed.
USE OF DISTINCT
SYNTAX
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
RESULT
#
Use True or False =True
RESULT
RANGE SEARCH CONDITION (BETWEEN/NOT BETWEEN) QUESTION List all staff with a salary between RM20,000 and RM30,000.
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;
RESULT
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;
RESULT
SQL has two special pattern-matching symbols: % percent character represents any sequence of zero or more characters (wildcard). _ underscore character represents any single character.
SYNTAX
SELECT ownerNo, fName, lName, address, telNo FROM PrivateOwner WHERE address LIKE %Glasgow%;
RESULT
SYNTAX
SELECT clientNo, viewDate FROM Viewing WHERE propertyNo = PG4 AND comment IS NULL;
RESULT
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
RESULT
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
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