Presentation is loading. Please wait.

Presentation is loading. Please wait.

7 7 Chapter 7 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 7th Edition Peter Rob & Carlos Coronel.

Similar presentations


Presentation on theme: "7 7 Chapter 7 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 7th Edition Peter Rob & Carlos Coronel."— Presentation transcript:

1 7 7 Chapter 7 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 7th Edition Peter Rob & Carlos Coronel

2 7 7 SQL 4Standardized 4Data Manipulation and Data Definition 4Can be embedded into general programming languages 4Statements specify what is to be done, NOT how to do it

3 7 7 DML - Retrieval 4SELECT statement - can do MANY different things u List whole table SELECT * FROM STUDENT u Relational Algebra PROJECT SELECT StdID,LNAME,FNAME FROM STUDENT l Unlike RA, duplicates aren’t (automatically) removed. E.g. SELECT MAJOR FROM STUDENT l If we want duplicates removed, specify DISTINCT after SELECT SELECT DISTINCT MAJOR FROM STUDENT

4 7 7 DML - RESTRICT 4Relational Algebra RESTRICT/SELECT u e.g. list all students who are freshmen SELECT * FROM STUDENT WHERE YEAR = ‘Fr’ 4PROJECT and RESTRICT together SELECT StdID,LNAME,FNAME FROM STUDENT WHERE YEAR = ‘Fr’

5 7 7 More Retrieval 4WHERE condition can be as complicated as you need it to be. E.g. freshmen with poor grades SELECT StdID,LNAME,FNAME FROM STUDENT WHERE YEAR = ‘Fr’ AND GPA < 2.5

6 7 7 Ordering 4With a little extra complexity, we can get our output in order by some particular attribute(s) E.g. order students by major SELECT StdID,LNAME,FNAME, MAJOR FROM STUDENT ORDER BY MAJOR DESC

7 7 7 The Microsoft Access QBE and its SQL

8 7 7 Joining Tables 4As you know, an important task with relational databases is relating info from more than one table (for instance, natural join in RA) E.g. show all students with the class indices of enrollments SELECT StudID,LNAME,FNAME, INDEX FROM STUDENT, ENROLLMENTS WHERE StudID = Enrollments.Student

9 7 7 Restrict with Join 4Sometimes you don’t want the entire join - you might want to restrict the results (sometimes called select- project-join) E.g. show all students enrolled in a particular section SELECT StdID,LNAME,FNAME FROM STUDENT, ENROLLMENTS WHERE STUDENT. StdID = ENROLLMENTS.Student AND ENROLLMENTS.INDEX = 70238

10 7 7 Multi-way Join 4We can join more than two tables together (which is a good thing; joining students with enrollments was a little unsatisfying because we didn’t see any info about the section. Let’s show all students with the section info for sections they enrolled in SELECT STUDENT.*, SECTION.* FROM STUDENT, ENROLLMENTS, SECTION WHERE STUDENT.StdID = ENROLLMENTS.Student AND ENROLLMENTS.INDEX = SECTION.INDEX

11 7 7 Alias 4Alternate name (for a table) 4SELECT STUDENT.*, SECTION.* FROM STUDENT A, ENROLLMENTS B, SECTION C WHERE A.StdID = B.student AND B.index = C.index; 4 Here A,B, and C are aliases 4Used as a convenience, not necessary

12 7 7 Queries 4Special Operators u BETWEEN - used to define range limits. u IS NULL - used to check whether an attribute value is null u LIKE - used to check for similar character strings. u IN - used to check whether an attribute value matches a value contained within a (sub)set of listed values. u EXISTS - used to check whether an attribute has a value. In effect, EXISTS is the opposite of IS NULL. l Can also be used to check if a subquery returns any rows

13 7 7 4Special Operators BETWEEN is used to define range limits. SELECT * FROM STUDENT WHERE GPA BETWEEN 2.0 AND 2.1; Queries

14 7 7 4Special Operators IS NULL is used to check whether an attribute value is null. SELECT INDEX, DEPT, CLASS, TIME FROM SECTION WHERE ROOM IS NULL; Queries

15 7 7 4Special Operators LIKE is used to check for similar character strings. SELECT * FROM CATALOG_CLASS WHERE TITLE LIKE ‘%Lang%’; u % stands for 0 or more char wildcard u _ stands for a one char wildcard u e.g. WHERE TITLE LIKE ‘%Network%’ finds classes whose title includes the substring ‘Network’ u NOTE: While SQL commands are not case-sensitive, SQL strings are Queries

16 7 7 4Special Operators IN is used to check whether an attribute value matches a value contained within a (sub)set of listed values. SELECT * FROM ENROLLMENT WHERE INDEX IN (66415, 66421); EXISTS is used to check whether an attribute has value. SELECT * FROM SECTIONs WHERE PROFESSOR EXISTS; Queries

17 Can do computation 4e.g. SELECT SECTION.*,stop - enroll FROM SECTION 4gives for all sections, the number of remaining seats 4E.g. SELECT room.*, capacity / NumbStudentWorkstations AS StdPerComp FROM room 4Gives number of students per computer in the rooms 4Usual mathematical operators  + - * / ^

18 7 7 Some Basic SQL Numeric Functions Some SQL Numeric Aggregate Functions

19 7 7 Aggregate Functions - AVG 4e.g. find ave GPA for all students SELECT AVG(GPA) FROM STUDENT 4What is the average GPA of all freshmen SELECT AVG(GPA) FROM STUDENT WHERE YEAR = ‘Fr’

20 7 7 COUNT 4How many sections are offered SELECT COUNT(*) FROM SECTION 4How many computer science majors are there? SELECT COUNT(*) FROM STUDENT WHERE MAJOR = ‘CS’ 4How many distinct classes are being offered SELECT COUNT(DISTINCT DEPT,CLASS) FROM SECTION

21 7 7 Group By - Subtotals 4Total Enrollments by Dept SELECT DEPT, SUM(enroll) FROM SECTION GROUP BY DEPT

22 7 7 4Grouping Data GROUP BY

23 7 7 Improper Use of the GROUP BY Clause

24 7 7 Having 4Total Enrollments by Dept for depts offering more than 10 sections SELECT DEPT, SUM(enroll) FROM SECTION GROUP BY DEPT HAVING COUNT(*) > 10 4Average Evening Enrollments by Depts with low ave enrollment SELECT DEPT, AVG(enroll) FROM SECTION WHERE Sect >= 40 GROUP BY DEPT HAVING AVG(enroll) < 15

25 7 7 Having 4Total Enrollments by Dept for depts offering more than 10 sections SELECT DEPT, SUM(credits) FROM CATALOGCOURSE GROUP BY DEPT HAVING COUNT(*) > 10 4Average Highest Enrollment (capacity) for upper division courses by Depts – for depts with many upper division sections SELECT DEPT, AVG(MaxEnroll) FROM SECTIONS WHERE Course >= 200 GROUP BY DEPT HAVING COUNT(*) >= 10

26 7 7 An Application of the HAVING Clause

27 7 7 Nested Queries 4Sometimes the result of one query can be used in another query - thus we can have nested queries 4e.g. find students whose GPA is above average SELECT * FROM STUDENT WHERE GPA > ( SELECT AVG(GPA) FROM STUDENT )

28 7 7 Nested Queries 4If is fairly common for a nested query to use the set operation IN - which tests whether a value is a member of a set of values. So for instance, suppose we want to know all sections in which there are any freshman SELECT DISTINCT CLASSINDEX FROM ENROLLMENTS WHERE STDSSN IN ( SELECT SSN FROM STUDENT WHERE YEAR = ‘Fr’ )

29 7 7 Left Outer Join SELECT P_CODE, VENDOR.V_CODE, V_NAME FROM VENDOR LEFT JOIN PRODUCT ON VENDOR.V_CODE = PRODUCT.V_CODE

30 7 7 The Left Outer Join Results

31 7 7 Right Outer Join SELECT PRODUCT.P_CODE, VENDOR.V_CODE, V_NAME FROM VENDOR RIGHT JOIN PRODUCT ON VENDOR.V_CODE = PRODUCT.V_CODE

32 7 7 The Right Outer Join Results

33 7 7 End Queries – Next - Updates


Download ppt "7 7 Chapter 7 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 7th Edition Peter Rob & Carlos Coronel."

Similar presentations


Ads by Google