360 likes | 566 Views
Single Table Queries Part 2. Using Functions. Tourism_Revenue Table. Select min(Revenues) as ‘Minimum Revenues’ from Tourism_Revenue. Result Set. Tourism_Revenue Table. Select count(*) as ‘Record Count’ from Tourism_Revenue. Result Set. NFL_Fans Table. select NFL_Team,
E N D
Using Functions A Guide to MySQL
Tourism_Revenue Table Select min(Revenues) as ‘Minimum Revenues’ from Tourism_Revenue Result Set
Tourism_Revenue Table Select count(*) as ‘Record Count’ from Tourism_Revenue Result Set
NFL_Fans Table select NFL_Team, avg(Age) as 'Avg Fan Age' from NFL_Fans group by NFL_Team Result Set
NFL_Fans Table select NFL_Team, count(*) as 'Fan Count' from NFL_Fans group by NFL_Team Result Set
NFL_Fans Table select NFL_Team, count(*) as 'Fan Count' from NFL_Fans group by NFL_Team having count(*) > 2 Result Set
where vs. having • where clause • filters out data not meeting a certain criteria • data that is not filtered out can then be placed into groups • having clause • filters out groups not meeting a certain criteria
select Political_Party, count(*) as 'Member count' from USA where State = 'WI' group by Political_Party having count(*) > 10000 order by 1
U.S. Population: 305,709,250 select Political_Party, count(*) as 'Member count' from USA where State = 'WI' group by Political_Party having count(*) > 10000 order by 1 WI Population: 5,363,675 Would it make a whole lot of sense for us to have everyone in the U.S. get into groups based on Political Party if we really only want to put Wisconsin people into groups? No. We only care about 5.3 million people, so there’s no reason to have the other 300 million + people get into groups.
U.S. Population: 305,709,250 select Political_Party, count(*) as 'Member count' from USA where State = 'WI' group by Political_Party having count(*) > 10000 order by 1 WI Population: 5,363,675 Would it make a whole lot of sense for us to have everyone in the U.S. get into groups based on Political Party if we really only want to put Wisconsin people into groups? No. We only care about 5.3 million people, so there’s no reason to have the other 300 million + people get into groups. So we filter out people from the other 49 states before we even create groups (where clause)
U.S. Population: 305,709,250 select Political_Party, count(*) as 'Member count' from USA where State = 'WI' group by Political_Party having count(*) > 10000 order by 1 WI Population: 5,363,675 Can we have Political Party groups with 10,000 or less members go home before we even place the people of Wisconsin into groups? No. We need to put people into groups so that we can count how many are in each group.
U.S. Population: 305,709,250 select Political_Party, count(*) as 'Member count' from USA where State = 'WI' group by Political_Party having count(*) > 10000 order by 1 WI Population: 5,363,675 Can we have Political Party groups with 10,000 or less members go home before we even place the people of Wisconsin into groups? No. We need to put people into groups so that we can count how many are in each group. So we place the people from Wisconsin into groups based on Political Party (group by clause)
U.S. Population: 305,709,250 select Political_Party, count(*) as 'Member count' from USA where State = 'WI' group by Political_Party having count(*) > 10000 order by 1 WI Population: 5,363,675 Let’s say we have 20 political parties in Wisconsin, but only 2 have member counts greater then 10,000. Would it make sense to have all 20 parties line up in alphabetical order if we only care about seeing in order the 2 that have more then 10,000 members? No. I struggle enough with the alphabet, so I’d much rather order just the 2 groups instead of all 20.
U.S. Population: 305,709,250 select Political_Party, count(*) as 'Member count' from USA where State = 'WI' group by Political_Party having count(*) > 10000 order by 1 WI Population: 5,363,675 Let’s say we have 20 political parties in Wisconsin, but only 2 have member counts greater then 10,000. Would it make sense to have all 20 parties line up in alphabetical order if we only care about seeing in order the 2 that have more then 10,000 members? No. I struggle enough with the alphabet, so I’d much rather order just the 2 groups instead of all 20. So we get rid of the groups that do not have more then 10,000 members (having clause)
U.S. Population: 305,709,250 select Political_Party, count(*) as 'Member count' from USA where State = 'WI' group by Political_Party having count(*) > 10000 order by 1 WI Population: 5,363,675 Now that we only have Wisconsin people in our groups, and we only have the groups with more then 10,000 members being returned, let’s sort the data!
U.S. Population: 305,709,250 select Political_Party, count(*) as 'Member count' from USA where State = 'WI' group by Political_Party having count(*) > 10000 order by 1 WI Population: 5,363,675 Now that we only have Wisconsin people in our groups, and we only have the groups with more then 10,000 members being returned, let’s sort the data! (Order by clause)
Instructor Table What will the following query return? select dept_id, count(*) from instructor Error Query is missing a “group by” clause
Instructor Table What will the following query return? select dept_id, count(*) from instructor group by dept_id Results
Nesting Queries • Query results require two or more steps • Subquery: an inner query placed inside another query • Outer query uses subquery results A Guide to MySQL
A_Owner Table Let’s say we want to find slip information for all boats owned by people with a first name of Mary We know first name information does not exist in the Marina Slip table, which is where the slip information exists, but it does exist in the Owner table A_Marina_Slip Table We also know the Owner table and the Marina Slip table are related to each other by OWNER_NUM
A_Owner Table We can write a query against the Owner table using the filter information provided (First_Name = ‘Mary’) to bring back Owner_Num select owner_num from a_owner where first_name = 'Mary' A_Marina_Slip Table
A_Owner Table We can write a query against the Owner table using the filter information provided (First_Name = ‘Mary’) to bring back Owner_Num select owner_num from a_owner where first_name = 'Mary' A_Marina_Slip Table
A_Owner Table We can use the result set from the query against the Owner table in a query against the Marina Slip table select owner_num from a_owner where first_name = 'Mary' Select * from a_marina_slip where owner_num in ( A_Marina_Slip Table )
A_Owner Table We can use the result set from the query against the Owner table in a query against the Marina Slip table select owner_num from a_owner where first_name = 'Mary' Select * from a_marina_slip where owner_num in ( A_Marina_Slip Table Result Set )
Select * from a_marina_slip where owner_num in ( select owner_num from a_owner where first_name = 'Mary' ) Outer query Subquery The result of the subquery is used by the outer query
A_Owner Table Let’s say we want to find owner information for people who own a Sprite 4000 A_Marina_Slip Table
A_Owner Table A_Marina_Slip Table
A_Owner Table Select * from a_owner where owner_num = ( select owner_num from a_marina_slip where boat_type = 'Sprite 4000' ) A_Marina_Slip Table Query will error because the subquery returns more then one record.
A_Owner Table Select * from a_owner where owner_num in ( select owner_num from a_marina_slip where boat_type = 'Sprite 4000' ) A_Marina_Slip Table Result Set
Null • A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown.
select slip_id, next_service_date from a_service_request
select slip_id, next_service_date from a_service_request where next_service_date is null
select slip_id, next_service_date from a_service_request where next_service_date is not null
Using the tables in Alexamara Marina Return the marina number, slip number, and boat name of every slip who’s rental fee is greater then average of all rental fees.
What does ETL stand for? • Extract, Transform, and Load • ETL is a term/concept frequently associated with Data Warehousing • Examples of ETL Tools • InformaticaPowerCenter • SSIS (SQL Server Integration Services; MS SQL Server 2005 and above) • DTS (Data Transformation Services; MS SQL Server 2000) • IBM CognosDecisionStream