Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL IS 240 – Database Management Lecture #15 – 2004-04-08 Prof. M. E. Kabay, PhD, CISSP Norwich University

Similar presentations


Presentation on theme: "SQL IS 240 – Database Management Lecture #15 – 2004-04-08 Prof. M. E. Kabay, PhD, CISSP Norwich University"— Presentation transcript:

1 SQL IS 240 – Database Management Lecture #15 – 2004-04-08 Prof. M. E. Kabay, PhD, CISSP Norwich University mkabay@norwich.edu

2 2 Copyright © 2004 M. E. Kabay. All rights reserved. Topics We have demo on screen Then you do in-class exercises Finally we review together Your challenges Learn syntax Given syntax, predict results Given need, compose syntax

3 3 Copyright © 2004 M. E. Kabay. All rights reserved. Todays Data for Practice

4 4 Copyright © 2004 M. E. Kabay. All rights reserved. Projections You need to choose columns (fields) SELECT columns FROMsource E.g., SELECTLastName, FirstName FROM Patient

5 5 Copyright © 2004 M. E. Kabay. All rights reserved. Projections (contd) If you dont want duplicate rows with respect to a particular column, use the DISTINCT modifier for each column affected: SELECTDISTINCT column FROMsource E.g., SELECTDISTINCT LastName, FirstName FROMPatient

6 6 Copyright © 2004 M. E. Kabay. All rights reserved. Selections You need to choose specific rows according to some condition SELECTcolumn-list FROMsource WHEREcondition E.g., SELECTPatient, MedicalStaff FROMPatientDoctor WHEREHospital='Waterbury'

7 7 Copyright © 2004 M. E. Kabay. All rights reserved. More About the WHERE Clause Boolean logic uses AND operator WHERE condition1 AND condition2 Logical OR for a single column uses the IN operator E.g., WHERE column IN ['value1','value2'…] Can exclude rows using NOT IN operator E.g., WHERE column NOT IN ['value1','value2'…]

8 8 Copyright © 2004 M. E. Kabay. All rights reserved. More About the WHERE Clause Ranges are inclusive and use the BETWEEN operator; e.g., WHERE column BETWEEN lower AND upper Generic lookups are possible using LIKE operator; e.g., WHERE column LIKE 'string' Placeholders (wildcards) in string include Single character represented by _ One or more characters represented by % MS-Access uses ? and *, respectively NULL placeholder represents empty value

9 9 Copyright © 2004 M. E. Kabay. All rights reserved. SORTING ORDER BY clause tells SQL how to sort; e.g., ORDER BY field1, field2... Must SELECT all columns used in ORDER BY Can specify sort order for each field ASCending DESCending; e.g, ORDER BY field1 ASC, field2 DESC

10 10 Copyright © 2004 M. E. Kabay. All rights reserved. Simple Math Functions COUNT SUM AVG MAX MIN can be used in the SELECT clause only Result in a "relation" of one row and one column with the result in that cell E.g., SELECT COUNT (MedicalStaff) FROMPatientDoctor If desired, can stipulate that unique values of the target column be used; e.g., SELECT COUNT (DISTINCT MedicalStaff) FROMPatientDoctor

11 11 Copyright © 2004 M. E. Kabay. All rights reserved. Grouping Rows Can apply math functions to groups that have the same value of a specific field; e.g., How many patients does each doctor have? SELECTMedicalStaff, COUNT(*) FROMPatientDoctor GROUP BYMedicalStaff Can group of rows; e.g, List the number of patients for doctors who have more than 1 patient SELECTMedicalStaff, COUNT(*) FROMPatientDoctor GROUP BYMedicalStaff HAVINGCOUNT(*)>1

12 12 Copyright © 2004 M. E. Kabay. All rights reserved. In-Class Exercises Write your name at the top of the first page Form groups of 2 or more people if you wish Complete all the exercises on the separate handout sheets Discuss your reasoning freely with each other and with the instructor at any time We will review the answers together at the end of the class

13 13 Copyright © 2004 M. E. Kabay. All rights reserved. Required Homework Reread Chapter 9 using the full SURVEY- QUESTION-READ-RECITE-REVIEW phases of SQ3R For Tuesday 13 April, Survey-Question Chapter 10 in preparation for the class lecture on DB application design For Thursday 15 April For a total of 64 points or 10% of the homework grade: Complete and hand in printed replies to Group 1 Questions 9.1 through 9.32

14 14 Copyright © 2004 M. E. Kabay. All rights reserved. Optional Homework By Thursday 29 April For 18 extra points, use the Northwind database and complete the SQL for exercise 9.33 For 38 extra points, complete all the problems in the FiredUp Project Questions on pp. 254 & 255

15 15 Copyright © 2004 M. E. Kabay. All rights reserved. DISCUSSION


Download ppt "SQL IS 240 – Database Management Lecture #15 – 2004-04-08 Prof. M. E. Kabay, PhD, CISSP Norwich University"

Similar presentations


Ads by Google