Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiple Table Queries Part 2 1. Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net.

Similar presentations


Presentation on theme: "Multiple Table Queries Part 2 1. Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net."— Presentation transcript:

1 Multiple Table Queries Part 2 1

2 Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL 2 Instructor Table Class Table

3 Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL List Instructor names and the names of the classes they teach Ask yourself: - Which tables contain the information I need ? - How are the tables related to one another? 3 Instructor Class

4 List Instructor names and the names of the classes they teach select i.name as ‘Instructor Name’, c.name as ‘Class Name’ from instructor i join class c on c.instructor_id = i.instructor_id 4 Instructor NameClass Name Kris WindorskiSQL Doug WatermanVB.net Glen OrsburnSQL Terri KeaneC++ Kris WindorskiAccounting Kris WindorskiSQL Results Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class table aliases

5 List Instructor names and the names of the classes they teach select i.name as ‘Instructor Name’, c.name as ‘Class Name’ from instructor i, class c where c.instructor_id = i.instructor_id 5 Instructor NameClass Name Kris WindorskiSQL Doug WatermanVB.net Glen OrsburnSQL Terri KeaneC++ Kris WindorskiAccounting Kris WindorskiSQL Results Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

6 select i.name as ‘Instructor Name’, c.name as ‘Class Name’ from instructor i join class c on c.instructor_id = i.instructor_id select i.name as ‘Instructor Name’, c.name as ‘Class Name’ from instructor i, class c where c.instructor_id = i.instructor_id Joining in the “from” clauseJoining in the “where” clause 6

7 List the names of instructors teaching SQL select i.name as ‘Instructor Name’ from instructor i join class c on c.instructor_id = i.instructor_id where c.name = ‘SQL’ 7 Instructor Name Kris Windorski Glen Orsburn Kris Windorski Results How can I get rid of the duplicate Instructor Name? Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

8 List the names of instructors teaching SQL (distinct list) select distinct i.name as ‘Instructor Name’ from instructor i join class c on c.instructor_id = i.instructor_id where c.name = ‘SQL’ 8 Instructor Name Kris Windorski Glen Orsburn Results Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

9 List the names of instructors teaching SQL (distinct list) select distinct i.name as ‘Instructor Name’ from instructor i join class c on c.instructor_id = i.instructor_id and c.name = ‘SQL’ 9 Instructor Name Kris Windorski Glen Orsburn Results Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

10 select i.name as ‘Instructor Name’ from instructor i join class c on c.instructor_id = i.instructor_id where c.name = ‘SQL’ select i.name as ‘Instructor Name’ from instructor i join class c on c.instructor_id = i.instructor_id and c.name = ‘SQL’ Filtering in the where clauseFiltering in the from clause When a query only has inner joins a filter placed in the “where” clause will yield the same result as placing the filter in the “from” clause 10

11 List the names of instructors and a count of classes they are teaching select i.name as ‘Instructor Name’, count(*) as ‘Class Count’ from instructor i join class c on c.instructor_id = i.instructor_id 11 Results ERROR!! Query is missing a “group by” clause Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

12 List the names of instructors and a count of classes they are teaching select i.name as ‘Instructor Name’, count(*) as ‘Class Count’ from instructor i join class c on c.instructor_id = i.instructor_id group by i.name 12 Results Instructor NameClass Count Doug Waterman1 Glen Orsburn1 Kris Windorski3 Terri Keane1 Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

13 List the names of instructors and a count of classes they are teaching. Do not include Accounting classes in your class counts. select i.name as ‘Instructor Name’, count(*) as ‘Class Count’ from instructor i join class c on c.instructor_id = i.instructor_id group by i.name 13 What needs to be added to this query? - a filter excluding Accounting classes in a “where” clause or in the “join” Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

14 List the names of instructors and a count of classes they are teaching. Do not include Accounting classes in your class counts. select i.name as ‘Instructor Name’, count(*) as ‘Class Count’ from instructor i join class c on c.instructor_id = i.instructor_id where c.name <> ‘Accounting’ group by i.name 14 Instructor NameClass Count Doug Waterman1 Glen Orsburn1 Kris Windorski2 Terri Keane1 Results Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

15 List the names of instructors and a count of classes they are teaching. Do not include Accounting classes in your class counts. select i.name as ‘Instructor Name’, count(*) as ‘Class Count’ from instructor i join class c on c.instructor_id = i.instructor_id and c.name <> ‘Accounting’ group by i.name 15 Instructor NameClass Count Doug Waterman1 Glen Orsburn1 Kris Windorski2 Terri Keane1 Results Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

16 List the names of instructors and a count of classes they are teaching. Do not include Accounting classes in your class counts, and only display instructors teaching more then 1 class select i.name as ‘Instructor Name’, count(*) as ‘Class Count’ from instructor i join class c on c.instructor_id = i.instructor_id and c.name <> ‘Accounting’ group by i.name 16 What needs to be added to this query? - a “having” clause excluding groups that do not have a class count greater then 1 Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

17 List the names of instructors and a count of classes they are teaching. Do not include Accounting classes in your class counts, and only display instructors teaching more then 1 class select i.name as ‘Instructor Name’, count(*) as ‘Class Count’ from instructor i join class c on c.instructor_id = i.instructor_id and c.name <> ‘Accounting’ group by i.name having count(*) > 1 17 Instructor NameClass Count Kris Windorski2 Results Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Class

18 Let’s add another table! 18 Dept_IDName 1IT 2Adjunct Department Table Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Instructor Table Class Table Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2

19 Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL List department names and the names of the classes they offer Ask yourself: - Which tables contain the information I need ? - How are the tables related to one another? 19 Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

20 List department names and the names of the classes they offer 20 select distinct d.name as ‘Department Name’, c.name as ‘Class Name’ from department d join instructor i on i.dept_id = d.dept_id join class c on c.instructor_id = i.instructor_id Department NameClass Name AdjunctSQL ITVB.net ITSQL ITC++ AdjunctAccounting Results Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

21 List department names and the names of the classes they offer 21 select distinct d.name as ‘Department Name’, c.name as ‘Class Name’ from department d, instructor i, class c where i.dept_id = d.dept_id and c.instructor_id = i.instructor_id Department NameClass Name AdjunctSQL ITVB.net ITSQL ITC++ AdjunctAccounting Results Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

22 List department names and count of classes they offer 22 select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id join class c on c.instructor_id = i.instructor_id Results ERROR!! Query is missing a “group by” clause Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

23 List department names and count of classes they offer 23 select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id join class c on c.instructor_id = i.instructor_id group by d.name Results Department NameClass Count Adjunct3 IT3 Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

24 List department names and count of classes they offer. Only list departments offering more then 3 classes. 24 select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id join class c on c.instructor_id = i.instructor_id group by d.name What needs to be added to this query? - a “having” clause excluding groups that do not have a class count greater then 3 Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

25 List department names and count of classes they offer. Only list departments offering more then 3 classes. 25 select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id join class c on c.instructor_id = i.instructor_id group by d.name having count(*) > 3 Results Department NameClass Count Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

26 List department names and count of classes they offer. Do not include Accounting in your class counts, and only list departments offering more then 2 classes. 26 select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id join class c on c.instructor_id = i.instructor_id group by d.name having count(*) > 2 What needs to be added to this query? - a filter excluding Accounting classes in a “ where ” clause or in the “join” Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

27 List department names and count of classes they offer. Do not include Accounting in your class counts, and only list departments offering more then 2 classes. 27 select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id join class c on c.instructor_id = i.instructor_id and c.name <> ‘Accounting’ group by d.name having count(*) > 2 Results Department NameClass Count IT3 Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

28 List department names and count of classes they offer. Do not include Accounting in your class counts, and only list departments offering more then 2 classes. 28 select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id join class c on c.instructor_id = i.instructor_id where c.name <> ‘Accounting’ group by d.name having count(*) > 2 Results Department NameClass Count IT3 Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

29 What will the following query return? 29 select d.name as ‘Department Name’, i.name as ‘Instructor Name’, c.name as ‘Class Name’ from department d join instructor i on i.dept_id = d.dept_id and i.name = ‘Kris Windorski’ join class c on c.instructor_id = i.instructor_id Results Department NameInstructor NameClass Name AdjunctKris WindorskiSQL AdjunctKris WindorskiAccounting AdjunctKris WindorskiSQL Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

30 What will the following query return? 30 select * from department d join instructor i on i.dept_id = d.dept_id join class c on c.instructor_id = i.instructor_id where c.class_nbr = ‘99999’ Results dept_idnameinstructor_idnameDept_IDclass_nbrinstructor_idname 2Adjunct4Kris Windorski Accounting Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 77774SQL Dept_IDName 1IT 2Adjunct Instructor_IDNameDept_ID 1Doug Waterman1 2Terri Keane1 3Glen Orsburn1 4Kris Windorski2 Department Instructor Class

31 For every boat, list the marina number, slip number, boat name, owner number, owner’s first name, and owner’s last name Which tables contain the information I need? – a_marina_slip (marina_num, slip_num, boat_name, & owner_num) – a_owner (owner_num, first_name, & last_name) How are the tables related to one another? – by owner_num (a_marina_slip.owner_num = a_owner.owner_num) 31

32 For every boat, list the marina number, slip number, boat name, owner number, owner’s first name, and owner’s last name 32 select marina_num, slip_num, boat_name, a_marina_slip.owner_num, first_name, last_name from a_marina_slip join a_owner on a_marina_slip.owner_num = a_owner.owner_num

33 33

34 For every completed or open service request with a category description of “routine engine maintenance”, list the slip id, request description, and status Which tables contain the information I need? – a_service_request (slip_id, description, staus) – a_service_category (category_description) How are the tables related to one another? – by category_num (a_service_request.category_num = a_service_category.category_num) 34

35 For every completed or open service request with a category description of “routine engine maintenance”, list the slip id, request description, and status 35 select slip_id, description, status from a_service_request r join a_service_category c on r.category_num = c.category_num and category_description = 'routine engine maintenance'


Download ppt "Multiple Table Queries Part 2 1. Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net."

Similar presentations


Ads by Google