Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform."— Presentation transcript:

1

2 Agenda TMA01 M876 Block 3 – Using SQL

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

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

5 Terminology

6 Table NameColumn Name Row

7 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: SELECTSpecifies which columns are to appear in the output FROMSpecifies the table or tables to be used. WHEREFilters the rows subject to some conditions. GROUP BYForm groups of rows with the same column value. HAVINGFilters the groups subject to some condition. ORDER BYSpecifies the order of the output.

8

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

10 Retrieve specific columns, all rows SELECT sno, fname, lname, salary FROM staff; Produce a list of salaries for all staff, showing only the staff number, the first and last names, and the salary details.

11 Use of DISTINCT SELECT pno FROM viewing; SELECT DISTINCT(pno) FROM viewing; List the property numbers of all properties that have been viewed.

12 Calculated fields SELECT sno, fname, lname, salary/12 AS monthly_salary FROM staff; Produce a list of monthly salaries for all staff, showing the staff number, the first and last names and the salary details.

13 Comparison search condition SELECT sno, fname, lname, position, salary FROM staff WHERE salary > 10000; List all staff with a salary greater than 10,000.

14 Compound comparison search condition SELECT bno, street, area, city, pcode FROM branch WHERE city = ‘London’ OR city = ‘Glasgow’; List addresses of all branch offices in London or Glasgow.

15 Range search condition (BETWEEN/NOT BETWEEN) SELECT sno, fname, lname, position, salary FROM staff WHERE salary BETWEEN 20000 AND 30000; List all staff with a salary between 20,000 and 30,000.

16 Set membership search condition (IN/NOT IN) SELECT sno, fname, lname, position FROM staff WHERE position IN (‘Manager’,’Deputy’); List all Managers and Deputy Managers.

17 Pattern match search condition (LIKE/NOT LIKE) SELECT sno, fname, lname, address, salary FROM staff WHERE address LIKE ‘%Glasgow%’; 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

18 NULL search condition (IS NULL/IS NOT NULL) SELECT rno, date FROM viewing WHERE pno = ‘PG4’ AND comment IS NULL; List the details of all viewings on property PG4 where a comment has not been supplied.

19 Single column ordering SELECT sno, fname, lname, salary FROM staff ORDER BY salary DESC; Produce a list of salaries for all staff, arranged in descending order of salary.

20 Multiple column ordering SELECT pno, type, rooms, rent FROM property_for_rent ORDER BY type, rent DESC; Produce an abbreviation list of properties arranged in order of property type and then in descending order of rent.

21 Use of COUNT(*) SELECT COUNT(*) AS count FROM property_for_rent WHERE rent > 350; How many properties cost more than 350 per month to rent?

22 Use of COUNT(DISTINCT) SELECT COUNT(DISTINCT pno) AS count FROM viewing WHERE date BETWEEN ‘1-May-98’ AND ‘31-May-98’; How many different properties were viewed in May 1998?

23 Use of COUNT and SUM SELECT COUNT(sno) AS count, SUM(salary) AS sum FROM staff WHERE position = ‘Manager’; Find the total number of Managers and the sum of their salaries.

24 Use of MIN, MAX, AVG SELECT MIN(salary) AS min, MAX(salary) as max, AVG(salary) AS avg FROM staff; Find the minimum, maximum, and average staff salary.

25 Use of GROUP BY SELECT bno, COUNT(sno) AS count, SUM(salary) AS sum FROM staff GROUP BY bno ORDER BY bno; Find the number of staff working in each branch and the sum of their salaries.

26 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; Find the number of properties handled by each staff members.

27 Use of HAVING SELECT bno, COUNT(sno) AS count, SUM(salary) AS sum FROM staff GROUP BY bno HAVING COUNT(sno) > 1 ORDER BY bno; 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

28 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’.

29 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 list by how much their salaries is greater than the average.

30 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’.

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

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

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

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

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

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

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

38 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’); Find all staff who work in a London branch.

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

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

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

42 Add data to the database (INSERT) INSERT INTO table_name [(column_list)] VALUES (data_value_list)

43 INSERT … VALUES INSERT INTO staff VALUES (‘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.

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

45 Modify data in the database (UPDATE) UPDATE table_name SET column_name1 = data_value1 [, column_name2 = data_value2 …] [WHERE search_condition]

46 UPDATE all rows UPDATE staff SET salary = salary * 1.03; Give all staff a 3% pay increase.

47 UPDATE specific rows UPDATE staff SET salary = salary * 1.05 WHERE position = ‘Manager’; Give all Managers a 5% pay increase.

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

49 Deleting data from the database (DELETE) DELETE FROM table_name [WHERE search_condition]

50 DELETE specific rows DELETE FROM viewing WHERE pno = ‘PG4’; Delete all viewings that relate to property PG4.

51 DELETE all rows DELETE FROM viewing; Delete all records from the Viewing table.


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

Similar presentations


Ads by Google