View
228
Download
1
Embed Size (px)
Citation preview
Session 4
SQL Structured Query Language
SQL
• Modes of use– Interactive– Embedded
• Purpose– Create database– Create, Read, Update, Delete
Access SQL
Where to practice your SQL.
Main SQL Statements
• SELECT• INSERT• UPDATE• DELETE
Rules & Conventions
• SQL is free-format, like HTML, but typing clauses on separate lines makes it easier to read.
• Type reserved words in capitals.• All non-numeric data must be enclosed in single
quotes e.g. 'myname'.• The order of the clauses in a SELECT statement
cannot be changed.• Close all queries with a semi-colon ;
List all Employees
SELECT EmpNo, Bno, Name, Address, DOB, Salary
FROM Employee;
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
Corresponds to field names in the
database. Tip: don't leave spaces in field names when
creating the DB.
* = Wildcard character.
In SQL this is used to return all records.
Corresponds to table names in the database.
SELECT *FROM Employee;
Retrieve an Ordered List
SELECT Name, Address
FROM Employee
ORDER BY Name;
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
How Many Employees Sharethe Same Postcode?
SELECT Postcode, COUNT(*) AS Total
FROM Employee
GROUP BY Postcode;
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
Postcode TotalB15 1BQ 6B3 1LX 3B3 2AS 2B4 6DD 1
Employee's Monthly Salary?
SELECT Name, Salary/12
FROM Employee;
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
Name Expr1001Smith2666.66Foster 2333.33Jones 2208.33Patel 2083.33Chen 2583.33
MathematicalOperators+ Add- Subtract* Multiply/ Divide
Note the default column name.
SQL – Aggregate Functions•MIN
Returns lowest value in a set of values
•MAXReturns highest value in a set of values
•SUMProvides the sum of a set of values.
•COUNT•AVG
Returns average of a set of values. Nulls are ignored.
Total Salary Bill for the Year
SELECT SUM(Salary) AS TotalSalaries
FROM Employee;
TotalSalaries
£142,500.00
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
Comparison Operators
= equals
< is less than
> is greater than
<= is less than orequal to
>= is greater than or equal to
<> is not equal to
!= is not equal to
Where
Five basic search conditions • Comparison Compare the value of one
expression to the value of another expression• Range Test whether the value of an expression
falls within a specified range of values• Set Membership Test whether the value of an
expression equals one of a set of values• Pattern Match Test whether a string matches a
specified pattern• Null Test whether a column has a null value
Comparison - Salary Above £N
SELECT Name, Salary
FROM Employee
WHERE Salary >= 27000
ORDER BY Salary; Name SalarySmith £32,000.00Chen £31,000.00Foster£28,000.00
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
Pattern Match – Postcode = EC1
SELECT Name, Postcode
FROM Employees
WHERE Postcode LIKE 'EC1*';
Name Postcode
Caroline Cummins EC1A 7DDPeter Long EC1A 4ABAndrew Rawstron EC16 8TY
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
* = MS Access
% = ISO SQL
Range - Between
SELECT * FROM Employee
WHERE DOB
BETWEEN #1/10/2004#
AND #31/10/2004#;
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
Set Membership - Find all Employees who are Secretariesor Managers
SELECT * FROM Employee
WHERE Position IN (‘Secretary’, ‘Manager’);
SELECT * FROM Employee
WHERE Position = ‘Secretary’ OR Position = ‘Manager’;
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
Subquery – Lowest Paid Worker
SELECT Name, Salary
FROM Employee
WHERE Salary =
(SELECT MIN(Salary)
FROM Employee);
Name Salary
Jones £25,000.00
Employee {Empno, Bno, Name, Address, DOB, Position, Salary}
Subqueries
• Cannot use ORDER BY in subqueries.• Cannot perform on multiple attributes.• Cannot use LIKE or BETWEEN.
SELECT DISTINCT
Prevents duplicate tuples appearing in query results - compares whole ROWS only.
Simple join queries
• Example join operation in SQL– SELECT * FROM Employee, TimeCard
WHERE Employee.ssn = TimeCard.ssn
Outer join queries in Access
Queries with multiple operators
• Example of SQL statement with 3 joins:
SELECT lastName, firstName, title, dateRented
FROM Movie, Video, PreviousRental, Customer
WHERE Movie.movieID = Video.movieID AND Customer.accountID = PreviousRental.accountID AND Video.videoID = PreviousRental.videoIDAND dateRented > #01/12/2003#
SQL
• exercises