Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiple Table Queries Part 2

Similar presentations


Presentation on theme: "Multiple Table Queries Part 2"— Presentation transcript:

1 Multiple Table Queries Part 2

2 Instructor Table Class Table Instructor_ID Name 1 Doug Waterman 2
Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Table Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777

3 List Instructor names and the names of the classes they teach
Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Ask yourself: Class - Which tables contain the information I need? Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 - How are the tables related to one another?

4 List Instructor names and the names of the classes they teach
Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski select i.name as ‘Instructor Name’, c.name as ‘Class Name’ from instructor i join class c on c.instructor_id = i.instructor_id Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 table aliases Results Instructor Name Class Name Kris Windorski SQL Doug Waterman VB.net Glen Orsburn Terri Keane C++ Accounting

5 List Instructor names and the names of the classes they teach
Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski select i.name as ‘Instructor Name’, c.name as ‘Class Name’ from instructor i, class c where c.instructor_id = i.instructor_id Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Instructor Name Class Name Kris Windorski SQL Doug Waterman VB.net Glen Orsburn Terri Keane C++ Accounting

6 Joining in the “from” clause
Joining in the “where” clause 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

7 List the names of instructors teaching SQL
Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski select i.name as ‘Instructor Name’ from instructor i join class c on c.instructor_id = i.instructor_id where c.name = ‘SQL’ Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Instructor Name Kris Windorski Glen Orsburn How can I get rid of the duplicate Instructor Name?

8 List the names of instructors teaching SQL (distinct list)
Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski select distinct i.name as ‘Instructor Name’ from instructor i join class c on c.instructor_id = i.instructor_id where c.name = ‘SQL’ Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Instructor Name Kris Windorski Glen Orsburn

9 List the names of instructors teaching SQL (distinct list)
Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski select distinct i.name as ‘Instructor Name’ from instructor i join class c on c.instructor_id = i.instructor_id and c.name = ‘SQL’ Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Instructor Name Kris Windorski Glen Orsburn

10 Filtering in the where clause
Filtering in the from clause 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’ 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

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

12 List the names of instructors and a count of classes they are teaching
Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski 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 Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Instructor Name Class Count Doug Waterman 1 Glen Orsburn Kris Windorski 3 Terri Keane

13 List the names of instructors and a count of classes they are teaching
List the names of instructors and a count of classes they are teaching. Do not include Accounting classes in your class counts. Instructor Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 What needs to be added to this query? - a filter excluding Accounting classes in a “where” clause or in the “join” 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

14 List the names of instructors and a count of classes they are teaching
List the names of instructors and a count of classes they are teaching. Do not include Accounting classes in your class counts. Instructor Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class 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 Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Instructor Name Class Count Doug Waterman 1 Glen Orsburn Kris Windorski 2 Terri Keane

15 List the names of instructors and a count of classes they are teaching
List the names of instructors and a count of classes they are teaching. Do not include Accounting classes in your class counts. Instructor Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski 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 Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Instructor Name Class Count Doug Waterman 1 Glen Orsburn Kris Windorski 2 Terri Keane

16 List the names of instructors and a count of classes they are teaching
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 Instructor Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class What needs to be added to this query? Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 - a “having” clause excluding groups that do not have a class count greater then 1 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

17 List the names of instructors and a count of classes they are teaching
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 Instructor Instructor_ID Name 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 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 Results Instructor Name Class Count Kris Windorski 2

18 Let’s add another table!
Department Table Dept_ID Name 1 IT 2 Adjunct Instructor Table Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Table Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777

19 List department names and the names of the classes they offer
Dept_ID Name 1 IT 2 Adjunct Instructor Ask yourself: Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski - Which tables contain the information I need? - How are the tables related to one another? Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777

20 List department names and the names of the classes they offer
Dept_ID Name 1 IT 2 Adjunct Instructor 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 class c on c.instructor_id = i.instructor_id Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Department Name Class Name Adjunct SQL IT VB.net C++ Accounting

21 List department names and the names of the classes they offer
Dept_ID Name 1 IT 2 Adjunct Instructor 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 Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Department Name Class Name Adjunct SQL IT VB.net C++ Accounting

22 List department names and count of classes they offer
Dept_ID Name 1 IT 2 Adjunct Instructor select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id class c on c.instructor_id = i.instructor_id Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results ERROR!! Query is missing a “group by” clause

23 List department names and count of classes they offer
Dept_ID Name 1 IT 2 Adjunct Instructor select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id class c on c.instructor_id = i.instructor_id group by d.name Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Department Name Class Count Adjunct 3 IT

24 Department List department names and count of classes they offer. Only list departments offering more then 3 classes. Dept_ID Name 1 IT 2 Adjunct Instructor What needs to be added to this query? Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski - a “having” clause excluding groups that do not have a class count greater then 3 select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id class c on c.instructor_id = i.instructor_id group by d.name Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777

25 Department List department names and count of classes they offer. Only list departments offering more then 3 classes. Dept_ID Name 1 IT 2 Adjunct Instructor select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id class c on c.instructor_id = i.instructor_id group by d.name having count(*) > 3 Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Department Name Class Count

26 Department 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. Dept_ID Name 1 IT 2 Adjunct Instructor What needs to be added to this query? Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski - a filter excluding Accounting classes in a “where” clause or in the “join” select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id class c on c.instructor_id = i.instructor_id group by d.name having count(*) > 2 Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777

27 Department 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. Dept_ID Name 1 IT 2 Adjunct Instructor select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id class c on c.instructor_id = i.instructor_id and c.name <> ‘Accounting’ group by d.name having count(*) > 2 Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Department Name Class Count IT 3

28 Department 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. Dept_ID Name 1 IT 2 Adjunct Instructor select d.name as ‘Department Name’, count(*) as ‘Class Count’ from department d join instructor i on i.dept_id = d.dept_id class c on c.instructor_id = i.instructor_id where c.name <> ‘Accounting’ group by d.name having count(*) > 2 Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Department Name Class Count IT 3

29 What will the following query return?
Department What will the following query return? Dept_ID Name 1 IT 2 Adjunct 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’ class c on c.instructor_id = i.instructor_id Instructor Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results Department Name Instructor Name Class Name Adjunct Kris Windorski SQL Accounting

30 What will the following query return?
Department What will the following query return? Dept_ID Name 1 IT 2 Adjunct select * from department d join instructor i on i.dept_id = d.dept_id class c on c.instructor_id = i.instructor_id where c.class_nbr = ‘99999’ Instructor Instructor_ID Name Dept_ID 1 Doug Waterman 2 Terri Keane 3 Glen Orsburn 4 Kris Windorski Class Class_NBR Instructor_ID Name 1111 4 SQL 4444 1 VB.net 2222 3 5555 2 C++ 99999 Accounting 7777 Results dept_id name instructor_id Dept_ID class_nbr 2 Adjunct 4 Kris Windorski 99999 Accounting

31 Which tables contain the information I need?
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)

32 For every boat, list the marina number, slip number, boat name, owner number, owner’s first name, and owner’s last name 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

34 Which tables contain the information I need?
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)

35 For every completed or open service request with a category description of “routine engine maintenance”, list the slip id, request description, and status 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"

Similar presentations


Ads by Google