Sql success ch04

350 views

Published on

An SQL course

Published in: Education, Travel, Lifestyle
  • Be the first to comment

  • Be the first to like this

Sql success ch04

  1. 1. SQL Success Chapter 4 slides Stéphane Faroult 1
  2. 2. 2
  3. 3. 3
  4. 4. 4
  5. 5. 5
  6. 6. 6
  7. 7. 7
  8. 8. 8
  9. 9. Constraints 7 9
  10. 10. movies movieid title 1 Casablanca 2 Goodfellas 3 Bronenosets Potyomkin 4 Blade Runner 5 Annie Hall 6 Ying hung boon sik 7 Sholay 8 On The Waterfront 9 Lawrence Of Arabia 10 The Third Man 11 Ladri di biciclette country year_released us 1942 us 1990 ru 1925 us 1982 us 1977 hk 1986 in 1975 us 1954 gb 1962 gb 1949 it 1948 countries country_code ru us in gb fr hk it ca au country_name Russia United States India United Kingdom France Hong Kong Italy Canada Australia continent EUROPE AMERICA ASIA EUROPE EUROPE ASIA EUROPE AMERICA OCEANIA 10
  11. 11. select title, country_name, year_released from movies join countries on country_code = country where country_code <> 'us' 11
  12. 12. select title, country_name, year_released from movies join countries on country_code = country where country_code <> 'us' 12
  13. 13. select title, country_name, year_released from movies join countries on country_code = country where country_code <> 'us' 13
  14. 14. select title, country_name, year_released from movies join countries on country_code = country where country_code <> 'us' 14
  15. 15. movies 1 2 3 4 5 6 7 8 9 10 11 Casablanca Goodfellas Bronenosets Potyomkin Blade Runner Annie Hall Ying hung boon sik Sholay On The Waterfront Lawrence Of Arabia The Third Man Ladri di biciclette us us ru us us hk in us gb gb it 1942 1990 1925 1982 1977 1986 1975 1954 1962 1949 1948 countries ru us in gb fr hk it ca au Russia United States India United Kingdom France Hong Kong Italy Canada Australia EUROPE AMERICA ASIA EUROPE EUROPE ASIA EUROPE AMERICA OCEANIA 15
  16. 16. movies joined to countries 1 2 3 4 5 6 7 8 9 10 11 Casablanca Goodfellas Bronenosets Potyomkin Blade Runner Annie Hall Ying hung boon sik Sholay On The Waterfront Lawrence Of Arabia The Third Man Ladri di biciclette us us ru us us hk in us gb gb it 1942 1990 1925 1982 1977 1986 1975 1954 1962 1949 1948 us us ru us us hk in us gb gb it United States United States Russia United States United States Hong Kong India United States United Kingdom United Kingdom Italy AMERICA AMERICA EUROPE AMERICA AMERICA ASIA ASIA AMERICA EUROPE EUROPE EUROPE 16
  17. 17. movies joined to countries 1 2 3 4 5 6 7 8 9 10 11 Casablanca Goodfellas Bronenosets Potyomkin Blade Runner Annie Hall Ying hung boon sik Sholay On The Waterfront Lawrence Of Arabia The Third Man Ladri di biciclette us us ru us us hk in us gb gb it 1942 1990 1925 1982 1977 1986 1975 1954 1962 1949 1948 us us ru us us hk in us gb gb it United States United States Russia United States United States Hong Kong India United States United Kingdom United Kingdom Italy AMERICA AMERICA EUROPE AMERICA AMERICA ASIA ASIA AMERICA EUROPE EUROPE EUROPE select title, country_name, year_released from movies join countries on country_code = country where country_code <> 'us' 17
  18. 18. movies joined to countries 1 2 3 4 5 6 7 8 9 10 11 Casablanca Goodfellas Bronenosets Potyomkin Blade Runner Annie Hall Ying hung boon sik Sholay On The Waterfront Lawrence Of Arabia The Third Man Ladri di biciclette us us ru us us hk in us gb gb it 1942 1990 1925 1982 1977 1986 1975 1954 1962 1949 1948 us us ru us us hk in us gb gb it United States United States Russia United States United States Hong Kong India United States United Kingdom United Kingdom Italy AMERICA AMERICA EUROPE AMERICA AMERICA ASIA ASIA AMERICA EUROPE EUROPE EUROPE select title, country_name, year_released from countries join movies on country_code = country where country_code <> 'us' 18
  19. 19. on column1_from_table1 = column5_from_table2 and column2_from_table1 = column1_from_table2 19
  20. 20. people peopleid 5 10 15 20 25 30 35 first_name Claude Lung Carol Ramesh David Ray Rutger surname Rains Ti Reed Sippy Lean Liotta Hauer born 1889 1946 1906 1947 1908 1954 1944 died 1967 1976 1991 credits movieid peopleid credited_as 1 5 A 6 10 A 10 15 D 7 20 D 9 25 D 137 25 D 2 30 A 4 35 A 20
  21. 21. First name and surname of all directors in the database? select first_name, surname from people join credits where credited_as = 'D' 21
  22. 22. First name and surname of all directors in the database? select first_name, surname from people join credits where credited_as = 'D' credits movieid peopleid credited_as 1 5 A 6 10 A 10 15 D 7 20 D 9 25 D 137 25 D 2 30 A 22 4 35 A
  23. 23. First name and surname of all directors in the database? select first_name, surname from people join credits where credited_as = 'D' credits movieid peopleid credited_as 1 5 A 6 10 A 10 15 D 7 20 D 9 25 D 137 25 D 2 30 A 23 4 35 A
  24. 24. First name and surname of all directors in the database? select first_name, surname from people join credits where credited_as = 'D' credits movieid peopleid credited_as 1 5 A 6 10 A 10 15 D 7 20 D 9 25 D 137 25 D 2 30 A 24 4 35 A
  25. 25. First name and surname of all directors in the database? select distinct first_name, surname from people join credits where credited_as = 'D' 25
  26. 26. First name and surname of all directors in the database? select distinct first_name, surname from people join credits on peopleid = peopleid ? where credited_as = 'D' 26
  27. 27. First name and surname of all directors in the database? select distinct first_name, surname from people natural join credits where credited_as = 'D' 27
  28. 28. First name and surname of all directors in the database? select distinct first_name, surname from people natural join credits where credited_as = 'D' constraints 28
  29. 29. First name and surname of all directors in the database? select distinct first_name, surname from people natural join credits where credited_as = 'D' constraints names 29
  30. 30. countries ctry name people id name ctry 30
  31. 31. countries ctry name people id name ctry 31
  32. 32. countries ctry name people id name ctry 32
  33. 33. countries ctry name people id name ctry 33
  34. 34. select distinct first_name, surname from people join credits using (peopleid) where credited_as = 'D' 34
  35. 35. select distinct first_name, surname from people join credits on credits.peopleid = people.peopleid where credited_as = 'D' 35
  36. 36. select distinct first_name, surname from people p join credits c on c.peopleid = p.peopleid where credited_as = 'D' 36
  37. 37. select distinct p.first_name, p.surname from people p join credits c on c.peopleid = p.peopleid where c.credited_as = 'D' 37
  38. 38. Self-Join 38
  39. 39. peopleid first_name surname ... 876 MICHAEL RACHEL KEMPSON VANESSA REDGRAVE motherid REDGRAVE 932 fatherid 1234 876 932 39
  40. 40. peopleid first_name surname ... 876 MICHAEL RACHEL KEMPSON VANESSA REDGRAVE motherid REDGRAVE 932 fatherid 1234 876 932 40
  41. 41. peopleid first_name surname ... 876 MICHAEL RACHEL KEMPSON VANESSA REDGRAVE motherid REDGRAVE 932 fatherid 1234 876 932 41
  42. 42. peopleid first_name surname ... 876 MICHAEL RACHEL KEMPSON VANESSA REDGRAVE motherid REDGRAVE 932 fatherid 1234 876 932 select c.first_name || ' ' || c.surname as person, f.first_name || ' ' || f.surname as father from people as c -- child join people as f -- father on f.peopleid = c.fatherid 42
  43. 43. select ... from ([join operation])x join ... 43
  44. 44. from table1 inner join table2 inner join tablen 44
  45. 45. British movie titles with director names? 45
  46. 46. 46
  47. 47. credits 47
  48. 48. credits A A D A A A D D A A D A 48
  49. 49. select m.title, p.surname 49
  50. 50. P.C. Barua 1935 50
  51. 51. 1935 P.C. Barua 51
  52. 52. P.C. Barua 1935 1936 52
  53. 53. P.C. Barua 1935 1936 1937 53
  54. 54. surname 54
  55. 55. select m.title, p.surname 55
  56. 56. select distinct m.title, p.surname 56
  57. 57. select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb' 57
  58. 58. select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb' 58
  59. 59. select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb' 59
  60. 60. select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb' 60
  61. 61. Actors in any movie you like? 61
  62. 62. select m.year_released, m.country, p.first_name, p.surname from people p join credits c on c.peopleid = p.peopleid join movies m on m.movieid = c.movieid and c.credited_as = 'A' 62
  63. 63. select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb' 63
  64. 64. select m.year_released, m.title, p.first_name, p.surname from credits c inner join movies m on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb' 64
  65. 65. select m.year_released, m.title, p.first_name, p.surname from credits c inner join movies m on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'gb' 65
  66. 66. select m.year_released, m.title, p.first_name, p.surname from credits c inner join people p on p.peopleid = c.peopleid inner join movies m on c.movieid = m.movieid where c.credited_as = 'D' and m.country = 'gb' 66
  67. 67. select m.year_released, m.title, p.first_name, p.surname from movies m, credits c, people p where c.movieid = m.movieid and p.peopleid = c.peopleid and c.credited_as = 'D' and m.country = 'gb' 67
  68. 68. select m.year_released, m.title, p.first_name, p.surname from movies m, credits c, people p where c.movieid = m.movieid and p.peopleid = c.peopleid and c.credited_as = 'D' and m.country = 'gb' 68
  69. 69. select m.year_released, m.title, p.first_name, p.surname from movies m, credits c, people p where c.movieid = m.movieid and p.peopleid = c.peopleid and c.credited_as = 'D' and m.country = 'gb' 69
  70. 70. select m.year_released, m.title, p.first_name, p.surname from movies m join credits c on c.movieid = m.movieid join people p on p.peopleid = c.peopleid where c.credited_as = 'D' and m.country = 'it' 70
  71. 71. select m.year_released, m.title from movies m where m.country = 'it' 71
  72. 72. movies people credits 72
  73. 73. movies people credits 73
  74. 74. movies people credits 74
  75. 75. movies people credits 75
  76. 76. select m.year_released, m.title, p.first_name, p.surname from movies m, credits c, people p where c.movieid = m.movieid and p.peopleid = c.peopleid and c.credited_as = 'D' and m.country = 'it' 76
  77. 77. Flickr: John Wigham 77
  78. 78. outer join 78
  79. 79. outer join inner 79
  80. 80.  80
  81. 81.   81
  82. 82.    82
  83. 83.     83
  84. 84. left outer join right outer join full outer join 84
  85. 85. left outer join right outer join full outer join 85
  86. 86. Country Name Number of movies 86
  87. 87. Country Name Number of movies 87
  88. 88. select country as country_code, count(*) as number_of_movies from movies group by country 88
  89. 89. select c.country_name, x.number_of_movies from countries c inner join (select country as country_code, select count(*) as number_of_movies from movies group by country) x country on x.country_code = c.country_code 89
  90. 90. select c.country_name, x.number_of_movies from countries c inner join (select country as country_code, count(*) as number_of_movies from movies group by country) x on x.country_code = c.country_code 90
  91. 91. select c.country_name, x.number_of_movies from countries c left outer join (select country as country_code, count(*) as number_of_movies from movies group by country) x on x.country_code = c.country_code 91
  92. 92. Display zero when we have no movies from a country? 92
  93. 93. select c.country_name, case when x.number_of_movies is null then 0 else x.number_of_movies end number_of_movies from countries c left outer join (select country as country_code, count(*) as number_of_movies from movies group by country) x on x.country_code = c.country_code 93
  94. 94. select c.country_name, coalesce(x.number_of_movies, 0) number_of_movies from countries c left outer join (select country as country_code, count(*) as number_of_movies from movies group by country) x on x.country_code = c.country_code 94
  95. 95. from table1 left outer join table2 on 95
  96. 96. from table1 left outer join table2 on 96
  97. 97. from table1 left outer join table2 on 97
  98. 98. from table1 left outer join table2 on 98
  99. 99. from table1 left outer join table2 on 99
  100. 100. from table1 left outer join table2 on 100
  101. 101. from table1 left outer join table2 on 101
  102. 102. from table1 left outer join table2 on 102
  103. 103. British movie titles with director names when available? 103
  104. 104. select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.role = 'D' and m.country = 'gb' 104
  105. 105. select m.year_released, m.title, p.first_name, p.surname from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid where c.role = 'D' and m.country = 'gb' 105
  106. 106. select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid) a and a.country = 'gb' 106
  107. 107. select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m inner join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid 107
  108. 108. select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid) a where a.credited_as = 'D' and a.country = 'gb' 108
  109. 109. select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m inner join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid 109
  110. 110. Which table will be fully shown? 110
  111. 111. Which table will be fully shown? credits movies people 111
  112. 112. Which table will be fully shown? credits movies people 112
  113. 113. Which table will be fully shown? credits movies people 113
  114. 114. Which table will be Miss? fully shown? credits movies people 114
  115. 115. select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid 115
  116. 116. from movies m left outer join credits c on c.movied = m.movieid movies credits 116
  117. 117. from movies m left outer join credits c on c.movied = m.movieid inner join people p on p.peopleid = c.peopleid movies credits 117
  118. 118. from movies m left outer join credits c on c.movied = m.movieid inner join people p on p.peopleid = c.peopleid movies credits 118
  119. 119. from movies m left outer join credits c on c.movied = m.movieid inner join people p on p.peopleid = c.peopleid movies credits people 119
  120. 120. from movies m left outer join credits c on c.movied = m.movieid inner join people p on p.peopleid = c.peopleid movies credits people 120
  121. 121. from movies m left outer join credits c on c.movied = m.movieid inner join people p on p.peopleid = c.peopleid movies credits people 121
  122. 122. from movies m left outer join credits c on c.movied = m.movieid inner join people p on p.peopleid = c.peopleid movies credits people 122
  123. 123. select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid 123
  124. 124. select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid ) a where a.credited_as = 'D' and a.country = 'gb' 124
  125. 125. select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid ) a where a.credited_as = 'D' and a.country = 'gb' 125
  126. 126. select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid ) a where ( a.credited_as = 'D' or a.credited_as is null) and a.country = 'gb' 126
  127. 127. select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid ) a where ( a.credited_as = 'D' or a.credited_as is null) and a.country = 'gb' 127
  128. 128. 128
  129. 129. 129
  130. 130. Hugh Grant 130
  131. 131. Movies 131
  132. 132. People Movies 132
  133. 133. People Movies Credits 133
  134. 134. People Movies movieid peopleid Credits 134
  135. 135. People Movies movieid peopleid A Credits 135
  136. 136. select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid ) a where ( a.credited_as = 'D' or a.credited_as is null) and a.country = 'gb' 136
  137. 137. select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as A from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid ) a where ( a.credited_as = 'D' or a.credited_as is null) and a.country = 'gb' 137
  138. 138. select a.year_released, a.title, a.first_name, a.surname from (select m.year_released, m.title, m.country, p.first_name, p.surname, c.credited_as A from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid ) a where ( a.credited_as = 'D' or a.credited_as is null) and a.country = 'gb' 138
  139. 139. select m.year_released, m.title, p.first_name, p.surname from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid where (c.credited_as = 'D' or c.credited_as is null) and m.country = 'gb' 139
  140. 140. select m.year_released, m.title, p.first_name, p.surname from movies m left outer join credits c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid where (c.credited_as = 'D' or c.credited_as is null) and m.country = 'gb' 140
  141. 141. Get movie titles and director name if available 141
  142. 142. Get movie titles and director name if available Get movie titles and people involved, then display if director known or no people found 142
  143. 143. select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m inner join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid 143
  144. 144. select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m inner join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid 144
  145. 145. select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m left outer join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid inner join people p on p.peopleid = c.peopleid 145
  146. 146. select m.year_released, m.title, p.first_name, p.surname from (select movieid, year_released, title from movies where country = 'gb') m left outer join (select movieid, peopleid from credits where credited_as = 'D') c on c.movieid = m.movieid left outer join people p on p.peopleid = c.peopleid 146
  147. 147. Filter close to tables 147
  148. 148. Joins 148
  149. 149. Joins filtering 149
  150. 150. Joins filtering qualifying 150
  151. 151. select m.title, m.year_released from movies m inner join countries c on c.country_code = m.country where c.country_name = '...' 151
  152. 152. select m.title, m.year_released from movies m inner join countries c on c.country_code = m.country where c.country_name = '...' Filtering 152
  153. 153. select m.title, c.country_name from movies m inner join countries c on c.country_code = m.country where m.year_released = ... Qualifying 153
  154. 154. UNLESS select m.title, c.country_name from movies m inner join countries c on c.country_code = m.country where m.date_released = ... 154
  155. 155. UNLESS select m.title, c.country_name from movies m inner join countries c on c.country_code = m.country where m.date_released = ... 155
  156. 156. select distinct m.title, c.country_name from movies m inner join countries c on c.country_code = m.country inner join credits cr on cr.movieid = m.movieid where m.year_released = ... 156
  157. 157. Outer joins 157
  158. 158. Outer joins qualifying 158
  159. 159. Outer joins filtering is null 159
  160. 160. Join 160
  161. 161. Join 161
  162. 162. Join MORE rows? 162
  163. 163. Join MORE rows? filtering 163
  164. 164. Join MORE rows? filtering qualifying 164
  165. 165. 165
  166. 166. filtering ASAP 166
  167. 167. filtering ASAP 167
  168. 168. filtering ASAP qualifying later 168
  169. 169. filtering ASAP qualifying later 169
  170. 170. MOVIES 170
  171. 171. US MOVIES 171
  172. 172. US MOVIES, 1940s 172
  173. 173. select * from movies where country_code = 'us' and year_released between 1940 and 1949 173
  174. 174. select * from movies where country_code = 'us' and year_released between 1940 and 1949 select * from (select * from movies where country_code = 'us') us_movies where year_released between 1940 and 1949 174
  175. 175. select * from movies where country_code = 'us' and year_released between 1940 and 1949 select * from movies where (country = 'us' or country = 'gb') and year_released between 1940 and 1949 175
  176. 176. Set operators 176
  177. 177. UNION 177
  178. 178. US and GB movies, 1940s 178
  179. 179. US and GB movies, 1940s select movieid, title, year_released, country from movies where country = 'us' and year_released between 1940 and 1949 179
  180. 180. US and GB movies, 1940s select movieid, title, year_released, country from movies where country = 'us' and year_released between 1940 and 1949 select movieid, title, year_released, country from movies where country = 'gb' and year_released between 1940 and 1949 180
  181. 181. US and GB movies, 1940s select movieid, title, year_released, country from movies where country = 'us' and year_released between 1940 and 1949 union select movieid, title, year_released, country from movies where country = 'gb' and year_released between 1940 and 1949 181
  182. 182. 182
  183. 183. 183
  184. 184. 184
  185. 185. 185
  186. 186. movies 186
  187. 187. movies premium_movies 187
  188. 188. select 'regular' as class, movieid, title, year_released from movies union select 'premium' as class, movieid, title, year_released from premium_movies 188
  189. 189. select 'regular' as class, movieid, title, year_released from movies union select 'premium' as class, movieid, title, year_released from premium_movies 189
  190. 190. select 'regular' as class, movieid, title, year_released from movies union all select 'premium' as class, movieid, title, year_released from premium_movies 190
  191. 191. current_year_data last_year_data 191
  192. 192. current_year_data last_year_data 192
  193. 193. current_year_data last_year_data 193
  194. 194. current_year_data last_year_data 194
  195. 195. last_year_data current_year_data 195
  196. 196. last_year_data current_year_data current_year_data 196
  197. 197. last_year_data current_year_data current_year_data 197
  198. 198. Last year’s views plus year-to-date views 198
  199. 199. select movieid, sum(view_count) as view_count from last_year_data group by movieid union select movieid, sum(view_count) as view_count from current_year_data group by movieid 199
  200. 200. select x.movieid, sum(x.view_count) as view_count from ( select movieid, sum(view_count) as view_count from last_year_data group by movieid union select movieid, sum(view_count) as view_count from current_year_data group by movieid ) x group by x.movieid 200
  201. 201. select x.movieid, sum(x.view_count) as view_count from ( select movieid, sum(view_count) as view_count from last_year_data group by movieid union select movieid, sum(view_count) as view_count from current_year_data group by movieid ) x group by x.movieid 201
  202. 202. select x.movieid, sum(x.view_count) as view_count from ( select movieid, sum(view_count) as view_count from last_year_data group by movieid union select movieid, sum(view_count) as view_count from current_year_data group by movieid ) x group by x.movieid 202
  203. 203. Goodfellas 2356 union Goodfellas 2356 203
  204. 204. Goodfellas 2356 union Goodfellas 2356 204
  205. 205. Goodfellas 2356 union all Goodfellas 2356 205
  206. 206. select x.movieid, sum(x.view_count) as view_count from (select 'last year' as period, movieid, sum(view_count) as view_count from last_year_data group by movieid union all select 'this year' as period, movieid, sum(view_count) as view_count from current_year_data group by movieid) x group by x.movieid 206
  207. 207. 207
  208. 208. union 208
  209. 209. intersect 209
  210. 210. except / minus 210
  211. 211. intersect except 211
  212. 212. intersect inner join except 212
  213. 213. intersect inner join except outer join 213
  214. 214. country codes that are both in movies and countries 214
  215. 215. select country_code from countries intersect select country from movies 215
  216. 216. select country_code from countries intersect select distinct country from movies 216
  217. 217. select c.country_code from countries c inner join ( select distinct country from movies ) m on m.country = c.country_code 217
  218. 218. select distinct country from movies 218
  219. 219. select country_code from countries except select distinct country from movies 219
  220. 220. select country_code from countries except select distinct country from movies 220
  221. 221. select c.country_code from countries c left outer join (select distinct country from movies) m on m.country = c.country_code where m.country is null 221
  222. 222. Equivalent columns 222
  223. 223. Equivalent columns Flickr: Tomáš Obšívač 223
  224. 224. Names of the countries in table country but not in table movies ? 224
  225. 225. Names of the countries in table country but not in table movies ? select c.country_ code from countries c left outer join (select distinct country from movies) m on m.country = c.country_code where m.country is null 225
  226. 226. Names of the countries in table country but not in table movies ? select c.country_ name from countries c left outer join (select distinct country from movies) m on m.country = c.country_code where m.country is null 226
  227. 227. select * from movies where country_code = 'us' 227
  228. 228. select * from (select * from movies select where country_code = 'us' us_movies 'us') where year_released between 1940 and 1949 228
  229. 229. select * from (select * from movies select where country_code = 'us' us_movies 'us') where year_released between 1940 and 1949 229
  230. 230. select * from (select * from movies select where country_code = 'us' us_movies 'us') where year_released between 1940 and 1949 230
  231. 231. 231
  232. 232. 232
  233. 233. 233
  234. 234. 234
  235. 235. 235
  236. 236. 236
  237. 237. select m.title, m.year_released, c.country_name from movies m join countries c on c.country_code = m.country where m.country <> 'us' 237
  238. 238. select m.title, m.year_released, (select c.country_name from countries c where c.country_code = m.country) as country_name from movies m where m.country <> 'us' 238
  239. 239. select m.title, m.year_released, (select c.country_name from countries c where c.country_code = m.country) as country_name from movies m where m.country <> 'us' 239
  240. 240. us ru in us us gb de in 240
  241. 241. us ru in us us gb de in select c.country_name from countries c where c.country_code = 'ru' 241
  242. 242. us ru in us us gb de in select c.country_name from countries c where c.country_code = 'in' 242
  243. 243. us ru in us us gb de in 243
  244. 244. us ru in us us gb de in 244
  245. 245. us ru in us us gb de in select c.country_name from countries c where c.country_code = 'gb' 245
  246. 246. us ru in us us gb de in select c.country_name from countries c where c.country_code = 'de' 246
  247. 247. us ru in us us gb de in select c.country_name from countries c where c.country_code = 'in' 247
  248. 248. select m.title, (select c.country_name from countries c where c.country_code = m.country) as country_name from movies m where m.country_code <> 'us' 248
  249. 249. select m.title, c.country_name from movies m left outer join countries c on c.country_code = m.country where m.country <> 'us' 249
  250. 250. from clause select where list clause 250
  251. 251. from clause select where list clause 251
  252. 252. from clause select where list clause 252
  253. 253. in ( ..., ..., ... ) 253
  254. 254. in ( ..., ..., ... ) select country, title from movies where (country = 'us' or country = 'gb') and year_released between 1940 and 1949 254
  255. 255. in ( ..., ..., ... ) select country, title from movies where country in ('us', 'gb') and year_released between 1940 and 1949 255
  256. 256. in ( ..., ..., ... ) 256
  257. 257. in (select col from ... where ...) 257
  258. 258. select country, year_released, title from movies where country in (select country_code from countries 258
  259. 259. (col1, col2) in (select col3, col4 from t 259
  260. 260. in distinct 260
  261. 261. select country, year_released, title from movies where country in (select country_code from countries 261
  262. 262. select m.country, m.year_released, m.title from movies m inner join (select country_code from countries on c.country_code = m.country 262
  263. 263. select m.country, m.year_released, m.title from movies m inner join (select country_code from countries on c.country_code = m.country 263
  264. 264. Demonstrably unique no distinct 264
  265. 265. select country, title from ... inner join (select distinct ... from ...) on ... 265
  266. 266. select country, title from ... inner join (select distinct ... from ...) on ... where col in (select distinct ... from ... ) 266
  267. 267. Explicit is better than Implicit 267
  268. 268. duplicate rows? 268
  269. 269. duplicate rows? nulls? 269
  270. 270. col in ('a', 'b', 'c') = (col = 'a' or col = 'b' or col = 'c') 270
  271. 271. col in ('a', 'b', null) = (col = 'a' or col = 'b' or col = null) 271
  272. 272. col in ('a', 'b', null) = (col = 'a' or col = 'b' or col = null) always false 272
  273. 273. Augustus de Morgan (1806-1871) 273
  274. 274. col not in ('a', 'b', null) = (col <> 'a' and col <> 'b' and col <> null) 274
  275. 275. col not in ('a', 'b', null) = always false (col <> 'a' and col <> 'b' and col <> null) 275
  276. 276. col not in ('a', 'b', null) = (col <> 'a' and col <> 'b' and col <> null) 276
  277. 277. select * from people where first_name not in (select first_name from people where born < 1950) 277
  278. 278. select * from people where first_name not in (select first_name from people where born < 1950 and first_name is not null) 278
  279. 279. select p.* from people p left outer join (select first_name, from people where born <= 1950) p2 on p2.first_name = p.first_name where p2.first_name is null 279
  280. 280. select p.* from people p left outer join (select first_name, from people where born <= 1950) p2 on p2.first_name = p.first_name where p2.first_name is null 280
  281. 281. select p.* from people p left outer join (select first_name, from people where born <= 1950) p2 on p2.first_name = p.first_name where p2.first_name is null and p.born >= 1950 281
  282. 282. from clause select where list clause 282
  283. 283. exists 283
  284. 284. exists not exists 284
  285. 285. and exists (select ... ...) 285
  286. 286. select distinct m.title from movies m where exists (select null from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid) 286
  287. 287. select distinct m.title from movies m where exists (select null from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid) 287
  288. 288. Flickr: Kevin N. Murphy 288
  289. 289. select m.title from movies m where exists (select c.movieid from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid) 289
  290. 290. select m.title from movies m where exists (select p.surname from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid) 290
  291. 291. select m.title from movies m where exists (select 1 from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid) 291
  292. 292. select m.title from movies m where exists (select 'Insert anything here' from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid) 292
  293. 293. select m.title from movies m where exists (select null from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970 and c.movieid = m.movieid) 293
  294. 294. movies (select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid ) 294
  295. 295. movies (select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid ) 295
  296. 296. movies (select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid ) 296
  297. 297. movies (select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid ) 297
  298. 298. movies (select null from credits c inner join people p on p.peopleid = c.peopleid where c.created_as = 'A' and p.born >= 1970 and c.movieid = current movieid ) 298
  299. 299. select distinct m.title from movies m where m.movieid in (select distinct c.movieid from credits c inner join people p on p.peopleid = c.peopleid where c.credited_as = 'A' and p.born >= 1970) 299
  300. 300. correlated uncorrelated 300
  301. 301. correlated uncorrelated from 301
  302. 302. correlated uncorrelated from 302
  303. 303. It depends ... Flickr: Alan Cleaver 303
  304. 304. Joins on common values. Inner: match only Outer: complete with nulls 304
  305. 305. Joins on common values. Inner: match only Outer: complete with nulls Order of tables and place of conditions matter with outer joins, not with inner joins. 305
  306. 306. Joins on common values. Inner: match only Outer: complete with nulls Order of tables and place of conditions matter with outer joins, not with inner joins. Set operators also allow to combine datasets. Only UNION provides functionality that no operator provide. 306
  307. 307. Joins on common values. Inner: match only Outer: complete with nulls Order of tables and place of conditions matter with outer joins, not with inner joins. Set operators also allow to combine datasets. Only UNION provides functionality that no operator provide. Subqueries can appear in the SELECT (correlated), in the FROM (uncorrelated), in the WHERE (either). Beware of nulls. 307

×