Guest User

Untitled

a guest
Nov 15th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. Even more aliasing
  2. Let's practice your newfound aliasing skills some more before moving on!
  3.  
  4. Recall: SQL assumes that if you divide an integer by an integer, you want to get an integer back.
  5.  
  6. This means that the following will erroneously result in 400.0:
  7.  
  8. SELECT 45 / 10 * 100.0;
  9. This is because 45 / 10 evaluates to an integer (4), and not a decimal number like we would expect.
  10.  
  11. So when you're dividing make sure at least one of your numbers has a decimal place:
  12.  
  13. SELECT 45 * 100.0 / 10;
  14. The above now gives the correct answer of 450.0 since the numerator (45 * 100.0) of the division is now a decimal!
  15.  
  16. Get the percentage of people who are no longer alive. Alias the result as percentage_dead. Remember to use 100.0 and not 100!:
  17. -- get the count(deathdate) and multiply by 100.0
  18. -- then divide by count(*)
  19. SELECT COUNT (deathdate) * 100.0/ COUNT (*) AS percentage_dead
  20. FROM people;
  21.  
  22. Get the number of years between the newest film and oldest film. Alias the result as difference:
  23. -- get the count(deathdate) and multiply by 100.0
  24. -- then divide by count(*)
  25. SELECT MAX (release_year) - MIN (release_year) AS difference
  26. FROM films;
  27.  
  28. Get the number of decades the films table covers. Alias the result as number_of_decades.
  29. The top half of your fraction should be enclosed in parentheses:
  30. SELECT (MAX(release_year) - MIN(release_year)) / 10.0
  31. AS number_of_decades
  32. FROM films
Add Comment
Please, Sign In to add comment