50
Agenda TMA01 M876 Block 3 – Using SQL

Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Embed Size (px)

Citation preview

Page 1: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Agenda

• TMA01

• M876 Block 3 – Using SQL

Page 2: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Structured Query Language - SQL

• A non-procedural language to– Create database and relation structures.– Perform basic data management tasks, such

as insertion, modification and deletion of data from the relations.

– Perform both simple and complex queries to transform the raw data into information.

• ISO standard Portable.

• Easy to learn.

Page 3: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

DDL and DML

• Two major types of SQL command– Data Definition Language (DDL)

• Commands to define the database including creating, altering and dropping tables, and establishing constraints and creating views.

• E.g. CREATE TABLE, ALTER TABLE, CREATE VIEW, CREATE INDEX and so on.

– Data Manipulation Language(DML)• Commands to insert, update, delete and retrieve data

from the database.• E.g. INSERT, UPDATE, DELETE, SELECT and so on.

Page 4: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Terminology

Relational Model terminology

Relational Database terminology

Relation Table

Attribute Column

Domain Data type

Tuple Row

Page 5: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

STAFF

StaffID Name Salary

1111 Bill Gates 10000

2468 Bill Clinton 15000

1357 John Major 8000

Table Name Column Name

Row

Page 6: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

General form of a SELECT statement

SELECT [DISTINCT | ALL] {*|column_expression[AS new_name]][,…]} FROM table_name [alias][,…][WHERE condition][GROUP BY column list][HAVING condiion][ORDER BY coluum list]

Remarks:SELECT Specifies which columns are to appear in the outputFROM Specifies the table or tables to be used.WHERE Filters the rows subject to some conditions. GROUP BY Form groups of rows with the same column value.HAVING Filters the groups subject to some condition.ORDER BY Specifies the order of the output.

Page 7: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform
Page 8: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Retrieve all columns, all rows

SELECT sno, fname, lname, address, tel_no, position, sex, dob, salary, nin, bno FROM staff;

SELECT * FROM staff;

List full details of all staff.

Page 9: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Retrieve specific columns, all rows

SELECT sno, fname, lname, salary FROM staff;

sno fname lname salarySL21 John White 30000.00SG37 Ann Beech 12000.00SG14 David Ford 18000.00SA9 Mary Howe 9000.00SG5 Susan Brand 24000.00SL41 Julie Lee 9000.00

Produce a list of salaries for all staff, showing only the staff number, the first and last names, and the salary details.

Page 10: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of DISTINCT

SELECT pno FROM viewing;

pnoPA14PG4PG4PA14PG36

SELECT DISTINCT(pno) FROM viewing;

pnoPA14PG4PG36

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

Page 11: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Calculated fields

SELECT sno, fname, lname, salary/12 AS monthly_salary FROM staff;

sno fname lname monthly_salarySL21 John White 2500.00SG37 Ann Beech 1000.00SG14 David Ford 1500.00SA9 Mary Howe 750.00SG5 Susan Brand 2000.00SL41 Julie Lee 750.00

Produce a list of monthly salaries for all staff, showing the staffnumber, the first and last names and the salary details.

Page 12: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Comparison search condition

SELECT sno, fname, lname, position, salary FROM staff WHERE salary > 10000;

sno fname lname position salarySL21 John White Manager 30000.00SG37 Ann Beech Snr Asst 12000.00SG14 David Ford Deputy 18000.00SG5 Susan Brand Manager 24000.00

List all staff with a salary greater than 10,000.

Page 13: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Compound comparison search condition

SELECT bno, street, area, city, pcode FROM branchWHERE city = ‘London’ OR city = ‘Glasgow’;

bno street area city pcodeB5 22 Deer Rd Sidcup London SW1 4EHB3 163 Main St Partick Glassgow G11 9QXB2 56 Clover Dr London NW10 6EU

List addresses of all branch offices in London or Glasgow.

Page 14: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Range search condition (BETWEEN/NOT BETWEEN)

SELECT sno, fname, lname, position, salary FROM staff WHERE salary BETWEEN 20000 AND 30000;

sno fname lname position salarySL21 John White Manager 30000.00SG5 Susan Brand Manager 24000.00

List all staff with a salary between 20,000 and 30,000.

Page 15: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Set membership search condition (IN/NOT IN)

SELECT sno, fname, lname, position FROM staff WHERE position IN (‘Manager’,’Deputy’);

sno fname lname positionSL21 John White ManagerSG14 David Ford DeputySG5 Susan Brand Manager

List all Managers and Deputy Managers.

Page 16: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Pattern match search condition (LIKE/NOT LIKE)

SELECT sno, fname, lname, address, salary FROM staffWHERE address LIKE ‘%Glasgow%’;

sno fname lname address salarySG37 Ann Beech 81 George St, Glasgow PA1 2JR 12000.00SG14 David Ford 63 Ashbby St, Partick, Glasgow G11 18000.00SG5 Susan Brand 5 Gt Western Rd, Glasgow G12 24000.00

Find all staff with the string ‘Glasgow’ in their address.(Note: % precent character represents any sequence of zero or more characters _ underscore character represents any single character

Page 17: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

NULL search condition (IS NULL/IS NOT NULL)

SELECT rno, date FROM viewingWHERE pno = ‘PG4’ AND comment IS NULL;

pno dateCR56 26-May-98

List the details of all viewings on property PG4 where a commenthas not been supplied.

Page 18: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Single column ordering

SELECT sno, fname, lname, salary FROM staff ORDER BY salary DESC;

sno fname lname salarySL21 John White 30000.00SG5 Susan Brand 24000.00SG14 David Ford 18000.00SG37 Ann Beech 12000.00SA9 Mary Howe 9000.00SL41 Julie Lee 9000.00

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

Page 19: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Multiple column ordering

SELECT pno, type, rooms, rent FROM property_for_rent ORDER BY type, rent DESC;

pno type rooms rentPG16 Flat 4 450PL94 Flat 4 400PG36 Flat 3 375PG4 Flat 3 350PA14 House 6 650PG21 House 5 600

Produce an abbreviation list of properties arranged in order ofproperty type and then in descending order of rent.

Page 20: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of COUNT(*)

SELECT COUNT(*) AS count FROM property_for_rentWHERE rent > 350;

count5

How many properties cost more than 350 per month to rent?

Page 21: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of COUNT(DISTINCT)

SELECT COUNT(DISTINCT pno) AS count FROM viewingWHERE date BETWEEN ‘1-May-98’ AND ‘31-May-98’;

count2

How many different properties were viewed in May 1998?

Page 22: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of COUNT and SUM

SELECT COUNT(sno) AS count, SUM(salary) AS sum FROM staffWHERE position = ‘Manager’;

count sum2 54000.00

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

Page 23: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of MIN, MAX, AVG

SELECT MIN(salary) AS min, MAX(salary) as max, AVG(salary) AS avg FROM staff;

min max avg9000.00 30000.00 17000.00

Find the minimum, maximum, and average staff salary.

Page 24: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of GROUP BY

SELECT bno, COUNT(sno) AS count, SUM(salary) AS sum FROM staff GROUP BY bno ORDER BY bno;

bno count sumB3 3 54000.00B5 2 39000.00B7 1 9000.00

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

Page 25: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Mutiple grouping columns

SELECT s.bno, s.sno, COUNT(*) AS count FROM staff s, property_for_rent p WHERE s.sno = p.sno GROUP BY s.bno, s.sno ORDER BY s.bno, s.sno;

bno sno countB3 SG14 2B3 SG37 2B5 SL41 1B7 SA9 1

Find the number of properties handled by each staff members.

Page 26: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of HAVING

SELECT bno, COUNT(sno) AS count, SUM(salary) AS sum FROM staff GROUP BY bnoHAVING COUNT(sno) > 1 ORDER BY bno;

bno count sumB3 3 54000.00B5 2 39000.00

Find 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 27: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Using a subquery with equality

SELECT sno, fname, lname, position FROM staff WHERE bno =

(SELECT bno FROM branch WHERE street = ‘163 Main St’);

List the staff who work in the branch at ‘163 Main St’.

sno fname lname positionSG37 Ann Beech Snr AsstSG14 David Ford DeputySG5 Susan Brand Manager

Page 28: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Using a subquery with an aggregate function

SELECT sno, fname, lname, position, salary - (SELECT AVG(salary) FROM staff) AS sal_diff FROM staff WHERE salary > (SELECT AVG(salary)

FROM staff);

List all staff whose salary is greater than the average salary, and listby how much their salaries is greater than the average.

sno fname lname position sal_diff SL21 John White Manager 13000.00 SG14 David Ford Deputy 1000.00 SG5 Susan Brand Manager 7000.00

Page 29: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Nested subqueries Use of IN

SELECT pno, street, area, city, pcode, type, rooms, rent FROM property_for_rent WHERE sno IN

(SELECT sno FROM staff WHERE bno =

(SELECT bno FROM branch WHERE street = ‘163 Main St’));

List the properties that are handled by staff who work in the branch at ‘163 Main St’.

Page 30: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Simple Join

SELECT r.rno, fname, lname, pno, comment FROM renter r, viewing v WHERE r.rno = v.rno;

List the names of all renters who have viewed a property along with any comment supplied.

rno fname lname pno commentCR56 Aline Stewart PG36CR56 Aline Stewart PA14 too smallCR56 Aline Stewart PG4CR62 Mary Tregear PA14 no dining roomCR76 John Kay PG4 too remote

Page 31: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Sorting a join

SELECT s.bno, s.sno, fname, lname, pno FROM staff s, property_for_rent p WHERE s.sno = p.sno;

For each branch office, list the names of staff who manage properties, and the properties they manage.

bno sno fname lname pnoB3 SG14 David Ford PG4B3 SG14 David Ford PG16B5 SL41 Julie Lee PL94B3 SG37 Ann Beech PG21B3 SG37 Ann Beech PG36B7 SA9 Mary Howe PA14

Page 32: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Sorting a join (Cont.)

SELECT s.bno, s.sno, fname, lname, pno FROM staff s, property_for_rent p WHERE s.sno = p.sno ORDER BY s.bno, s.sno, pno;

bno sno fname lname pnoB3 SG14 David Ford PG16B3 SG14 David Ford PG4B3 SG37 Ann Beech PG21B3 SG37 Ann Beech PG36B5 SL41 Julie Lee PL94B7 SA9 Mary Howe PA14

Page 33: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Three-table join

SELECT b.bno, b.city, s.sno, fname, lname, pno FROM branch b, staff s, property_for_rent p WHERE b.bno = s.bno AND s.sno = p.sno ORDER BY b.bno, s.sno, pno;

For each branch, list the staff who manage properties, including the city in which the branch is located and the properties they manage.

bno city sno fname lname pnoB3 Glasgow SG14 David Ford PG16B3 Glasgow SG14 David Ford PG4B3 Glasgow SG37 Ann Beech PG21B3 Glasgow SG37 Ann Beech PG36B5 London SL41 Julie Lee PL94B7 Aberdeen SA9 Mary Howe PA14

Page 34: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Left outer join

SELECT b.*, p.* FROM branch1 b LEFT JOIN property_for_rent1 p ON b.bcity = p.pcity;

List the branch offices and properties that are in the same city along with any unmatched branches.

bno bcity pno pcityB3 Glasgow PG4 GlasgowB4 Bristol NULL NULLB2 London PL94 London

Page 35: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Right outer join

SELECT b.*, p.* FROM branch1 b RIGHT JOIN property_for_rent1 p ON b.bcity = p.pcity;

List the branch offices and properties that are in the same city and any unmatched properties.

bno bcity pno pcityNULL NULL PA14 AberdeenB3 Glasgow PG4 GlasgowB2 London PL94 London

Page 36: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Full outer join

SELECT b.*, p.* FROM branch1 b FULL JOIN property_for_rent1 p ON b.bcity = p.pcity;

List the branch offices and properties that are in the same city and any unmatched branches or properties.

bno bcity pno pcityNULL NULL PA14 AberdeenB3 Glasgow PG4 GlasgowB4 Bristol NULL NULLB2 London PL94 London

Page 37: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Query using EXISTS

SELECT sno, fname, lname, position FROM staff s WHERE EXISTS

(SELECT ‘Y' FROM branch b WHERE s.bno = b.bno AND city = ‘London’);

sno fname lname positionSL21 John White ManagerSL41 Julie Lee Assistant

Find all staff who work in a London branch.

Page 38: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of UNION

(SELECT area FROM branch WHERE area IS NOT NULL)UNION(SELECT area FROM property_for_rent WHERE area IS NOT NULL);

Construct a list of all areas where there is either a branch office or a rental property.

areaSidcupDycePartickLeighDeeKilburnHyndland

Page 39: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of INTERSECT

(SELECT city FROM branch)INTERSECT(SELECT city FROM property_for_rent);

Construct a list of all cities where there is both a branch office and a rental property.

cityAberdeenGlasgowLondon

Page 40: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Use of EXCEPT

(SELECT city FROM branch)EXCEPT(SELECT city FROM property_for_rent);

Construct a list of all cities where there is a branch office but no rental properties.

cityBristol

Page 41: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Add data to the database (INSERT)

INSERT INTO table_name [(column_list)]VALUES (data_value_list)

Page 42: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

INSERT … VALUES

INSERT INTO staffVALUES (‘SG16’, ‘Alan’, ‘Brown’, ’67 Endrick Rd, Glosgow G32 8QX’, ‘0141-211-3001’, ‘Assistant’, ’M’, DATE ‘1975-05025’, 8300, ‘WN848391H’, ‘B3’);

Insert a new record into the Staff table supplying data for all columns.

Page 43: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

INSERT uisng defaults

INSERT INTO staff (sno, fname, lname, position, salary, bno)VALUES (‘SG44’, ‘Anne’, ‘Jones’, ‘Assistant’, 8100, ‘B3’);

Insert a new record into the Staff table supplying data for all mandatory columns: Sno, FName, LName, Position, Salary, and Bno.

Page 44: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Modify data in the database (UPDATE)

UPDATE table_nameSET column_name1 = data_value1 [, column_name2 = data_value2 …][WHERE search_condition]

Page 45: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

UPDATE all rows

UPDATE staff SET salary = salary * 1.03;

Give all staff a 3% pay increase.

Page 46: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

UPDATE specific rows

UPDATE staff SET salary = salary * 1.05 WHERE position = ‘Manager’;

Give all Managers a 5% pay increase.

Page 47: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

UPDATE multiple columns

UPDATE staff SET position = ‘Manager’, salary = 18000 WHERE sno = ‘SG14’;

Promote David Ford (Sno = ‘SG14’) to Manager and change his salary to £18,000.

Page 48: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

Deleting data from the database (DELETE)

DELETE FROM table_name[WHERE search_condition]

Page 49: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

DELETE specific rows

DELETE FROM viewing WHERE pno = ‘PG4’;

Delete all viewings that relate to property PG4.

Page 50: Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform

DELETE all rows

DELETE FROM viewing;

Delete all records from the Viewing table.