0% found this document useful (0 votes)
176 views2 pages

Mysql Exercise 10: Answer Key Question 1

This document contains the answers to 12 questions about analyzing and summarizing data from tables in a MySQL database. The questions involve selecting, filtering, grouping, and aggregating data using SQL queries. Example queries showcase the use of CASE statements, IF functions, counts, and other SQL features to clean, categorize and summarize the data in insightful ways.

Uploaded by

jueguito lol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views2 pages

Mysql Exercise 10: Answer Key Question 1

This document contains the answers to 12 questions about analyzing and summarizing data from tables in a MySQL database. The questions involve selecting, filtering, grouping, and aggregating data using SQL queries. Example queries showcase the use of CASE statements, IF functions, counts, and other SQL features to clean, categorize and summarize the data in insightful ways.

Uploaded by

jueguito lol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Managing

 Big  Data  with  MySQL    


Dr.Jana  Schaich  Borg,  Duke  University  
 
 
MySQL  Exercise  10:  Answer  Key

Question  1:
%%sql  
SELECT  DISTINCT  user_guid,  country  
FROM  users
WHERE  country  IS  NOT  NULL;;

Question  2:
%%sql  
SELECT  IF(cleaned_users.country='US','In  US','Outside  US')  AS  user_location,  
count(cleaned_users.user_guid)  AS  num_guids      
FROM  (SELECT  DISTINCT  user_guid,  country                
           FROM  users              
           WHERE  user_guid  IS  NOT  NULL  AND  country  IS  NOT  NULL)  AS  cleaned_users    
GROUP  BY  user_location;;  

Question  3:
%%sql  
SELECT  dog_guid,  dog_fixed,  
         CASE  dog_fixed
                   WHEN  "1"  THEN  "neutered"
                   WHEN  "0"  THEN  "not  neutered"
         END  AS  neutered
FROM  dogs
LIMIT  200;;

Question  4:
%%sql  
SELECT  dog_guid,  exclude,  
         CASE  exclude
                   WHEN  "1"  THEN  "exclude"
                   ELSE  "keep"
         END  AS  exclude_cleaned
FROM  dogs
LIMIT  200;;

Question  5:
%%sql  
SELECT  dog_guid,  exclude,  IF(exclude="1","exclude","keep")  AS  exclude_cleaned
FROM  dogs
LIMIT  200;;
 
Question  6:
%%sql  SELECT  
dog_guid,  weight,  
         CASE  
                   WHEN  weight<=0  THEN  "very  small"
                   WHEN  weight>10  AND  weight<=30  THEN  "small"
                   WHEN  weight>30  AND  weight<=50  THEN  "medium"
                   WHEN  weight>50  AND  weight<=85  THEN  "large"
                   WHEN  weight>85  THEN  "very  large"
         END  AS  weight_grouped
FROM  dogs
LIMIT  200;;

Question  7:
4871  unique  dog_guids  in  Group  1

Question  8:
3461  unique  dog_guids  in  Group  1

Question  9:
46  unique  dog_guids  in  Group  1

Question  10:
%%sql  
SELECT  d.dog_guid  AS  dogID,  d.breed_type  AS  breed_type,  count(c.created_at)  AS  
numtests,
         IF(d.breed_type='Pure  Breed','pure_breed',  'not_pure_breed')  AS  pure_breed
FROM  dogs  d,  complete_tests  c
WHERE  d.dog_guid=c.dog_guid
GROUP  BY  dogID,  breed_type,  pure_breed
LIMIT  50;;  
 
**Note  that  “breed_type”  and  “pure_breed”  are  technically  optional  in  this  query  (above),  
since  dogID  should  be  unique.

Question  11:
%%sql  
SELECT  COUNT(DISTINCT  user_guid),  
                       CASE  
                                 WHEN  (state="NY"  OR  state="NJ")  THEN  "Group  1-­NY/NJ"
                                 WHEN  (state="NC"  OR  state="SC")  THEN  "Group  2-­NC/SC"
                                 WHEN  state="CA"  THEN  "Group  3-­CA"
                                 ELSE  "Group  4-­Other"
                       END  AS  state_group
FROM  users
WHERE  country="US"  AND  state  IS  NOT  NULL
GROUP  BY  state_group;;

Question  12:
%%sql  
SELECT  COUNT(DISTINCT  dog_guid)
FROM  dogs
WHERE  dna_tested=1  AND  (dimension='stargazer'  OR  dimension='socialite');;
 

You might also like