24
Session 4 SQL Structured Query Language

Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

  • View
    228

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

Session 4

SQL Structured Query Language

Page 2: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

SQL

• Modes of use– Interactive– Embedded

• Purpose– Create database– Create, Read, Update, Delete

Page 3: 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.

Page 4: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

Main SQL Statements

• SELECT• INSERT• UPDATE• DELETE

Page 5: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, 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 ;

Page 6: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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;

Page 7: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

Retrieve an Ordered List

SELECT Name, Address

FROM Employee

ORDER BY Name;

Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

Page 8: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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

Page 9: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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.

Page 10: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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.

Page 11: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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}

Page 12: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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

Page 13: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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

Page 14: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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}

Page 15: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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

Page 16: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

Range - Between

SELECT * FROM Employee

WHERE DOB

BETWEEN #1/10/2004#

AND #31/10/2004#;

Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

Page 17: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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}

Page 18: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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}

Page 19: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

Subqueries

• Cannot use ORDER BY in subqueries.• Cannot perform on multiple attributes.• Cannot use LIKE or BETWEEN.

Page 20: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

SELECT DISTINCT

Prevents duplicate tuples appearing in query results - compares whole ROWS only.

Page 21: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

Simple join queries

• Example join operation in SQL– SELECT * FROM Employee, TimeCard

WHERE Employee.ssn = TimeCard.ssn

Page 22: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

Outer join queries in Access

Page 23: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

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#

Page 24: Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

SQL

• exercises