diff --git a/lab-10.html b/lab-10.html new file mode 100644 index 0000000..f0e1e68 --- /dev/null +++ b/lab-10.html @@ -0,0 +1,6364 @@ + + + + + + + + + + +lab-10 + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+

lab-10

+
+ + + +
+ +
+
Author
+
+

Ellya Gholmieh

+
+
+ + + +
+ + + +
+ + +
+

Setup

+
+
# install.packages(c("RSQLite", "DBI"))
+
+library(RSQLite)
+library(DBI)
+
+# Initialize a temporary in memory database
+con <- dbConnect(SQLite(), ":memory:")
+
+# Download tables
+actor <- read.csv("https://raw.githubusercontent.com/ivanceras/sakila/master/csv-sakila-db/actor.csv")
+rental <- read.csv("https://raw.githubusercontent.com/ivanceras/sakila/master/csv-sakila-db/rental.csv")
+customer <- read.csv("https://raw.githubusercontent.com/ivanceras/sakila/master/csv-sakila-db/customer.csv")
+payment <- read.csv("https://raw.githubusercontent.com/ivanceras/sakila/master/csv-sakila-db/payment_p2007_01.csv")
+
+# Copy data.frames to database
+dbWriteTable(con, "actor", actor)
+dbWriteTable(con, "rental", rental)
+dbWriteTable(con, "customer", customer)
+dbWriteTable(con, "payment", payment)
+
+dbListTables(con)
+
+
[1] "actor"    "customer" "payment"  "rental"  
+
+
+
+
+

Exercise 1

+

Edit the code below to retrieve the actor ID, first name and last name for all actors using the actor table. Sort by last name and then by first name (note that the code chunk below is set up to run SQL code rather than R code).

+
+
actorids <- dbGetQuery(con,
+    "SELECT actor_id, first_name, last_name
+    FROM actor
+    ORDER BY last_name, first_name")
+
+print(actorids)
+
+
    actor_id  first_name    last_name
+1         58   CHRISTIAN       AKROYD
+2        182      DEBBIE       AKROYD
+3         92     KIRSTEN       AKROYD
+4        118        CUBA        ALLEN
+5        145         KIM        ALLEN
+6        194       MERYL        ALLEN
+7         76    ANGELINA      ASTAIRE
+8        112     RUSSELL       BACALL
+9        190      AUDREY       BAILEY
+10        67     JESSICA       BAILEY
+11       115    HARRISON         BALE
+12       187       RENEE         BALL
+13        47       JULIA    BARRYMORE
+14       158      VIVIEN     BASINGER
+15       174     MICHAEL       BENING
+16       124    SCARLETT       BENING
+17        14      VIVIEN       BERGEN
+18       121        LIZA      BERGMAN
+19        91 CHRISTOPHER        BERRY
+20        60       HENRY        BERRY
+21        12        KARL        BERRY
+22       189        CUBA        BIRCH
+23        25       KEVIN        BLOOM
+24       185     MICHAEL       BOLGER
+25        37         VAL       BOLGER
+26        98       CHRIS      BRIDGES
+27        39      GOLDIE        BRODY
+28       159       LAURA        BRODY
+29       167    LAURENCE      BULLOCK
+30        40      JOHNNY         CAGE
+31        11        ZERO         CAGE
+32       181     MATTHEW       CARREY
+33        86        GREG      CHAPLIN
+34         3          ED        CHASE
+35       176         JON        CHASE
+36       183     RUSSELL        CLOSE
+37        16        FRED      COSTNER
+38       129       DARYL     CRAWFORD
+39        26         RIP     CRAWFORD
+40        49        ANNE       CRONYN
+41       104    PENELOPE       CRONYN
+42       105      SIDNEY        CROWE
+43        57        JUDE       CRUISE
+44       201         TOM       CRUISE
+45       203         TOM       CRUISE
+46       205         TOM       CRUISE
+47       207         TOM       CRUISE
+48        80       RALPH         CRUZ
+49        81    SCARLETT        DAMON
+50         4    JENNIFER        DAVIS
+51       101       SUSAN        DAVIS
+52       110       SUSAN        DAVIS
+53        48     FRANCES    DAY-LEWIS
+54        35        JUDY         DEAN
+55       143       RIVER         DEAN
+56       148       EMILY          DEE
+57       138     LUCILLE          DEE
+58       107        GINA    DEGENERES
+59        41       JODIE    DEGENERES
+60       166        NICK    DEGENERES
+61        89    CHARLIZE        DENCH
+62       123    JULIANNE        DENCH
+63       160       CHRIS         DEPP
+64       100     SPENCER         DEPP
+65       109   SYLVESTER         DERN
+66       173        ALAN     DREYFUSS
+67        36        BURT      DUKAKIS
+68       188        ROCK      DUKAKIS
+69       106     GROUCHO        DUNST
+70        19         BOB      FAWCETT
+71       199       JULIA      FAWCETT
+72        10   CHRISTIAN        GABLE
+73       165          AL      GARLAND
+74       184    HUMPHREY      GARLAND
+75       127       KEVIN      GARLAND
+76       154       MERYL       GIBSON
+77        46      PARKER     GOLDBERG
+78       139        EWAN      GOODING
+79       191     GREGORY      GOODING
+80        71        ADAM        GRANT
+81       179          ED      GUINESS
+82         1    PENELOPE      GUINESS
+83        90        SEAN      GUINESS
+84        32         TIM      HACKMAN
+85       175     WILLIAM      HACKMAN
+86       202         TOM        HANKS
+87       204         TOM        HANKS
+88       206         TOM        HANKS
+89       208         TOM        HANKS
+90       152         BEN       HARRIS
+91       141        CATE       HARRIS
+92        56         DAN       HARRIS
+93        97         MEG        HAWKE
+94       151    GEOFFREY       HESTON
+95       169     KENNETH      HOFFMAN
+96        79         MAE      HOFFMAN
+97        28       WOODY      HOFFMAN
+98       161      HARVEY         HOPE
+99       134        GENE      HOPKINS
+100      113      MORGAN      HOPKINS
+101       50     NATALIE      HOPKINS
+102      132        ADAM       HOPPER
+103      170        MENA       HOPPER
+104       65      ANGELA       HUDSON
+105       52      CARMEN         HUNT
+106      140      WHOOPI         HURT
+107      131        JANE      JACKMAN
+108      119      WARREN      JACKMAN
+109      146      ALBERT    JOHANSSON
+110        8     MATTHEW    JOHANSSON
+111       64         RAY    JOHANSSON
+112       82       WOODY        JOLIE
+113       43        KIRK     JOVOVICH
+114      130       GRETA       KEITEL
+115      198        MARY       KEITEL
+116       74       MILLA       KEITEL
+117       55         FAY       KILMER
+118      153      MINNIE       KILMER
+119      162       OPRAH       KILMER
+120       45       REESE       KILMER
+121       23      SANDRA       KILMER
+122      103     MATTHEW        LEIGH
+123        5      JOHNNY LOLLOBRIGIDA
+124      157       GRETA       MALDEN
+125      136          ED    MANSFIELD
+126       22       ELVIS         MARX
+127       77        CARY  MCCONAUGHEY
+128       70    MICHELLE  MCCONAUGHEY
+129      114      MORGAN    MCDORMAND
+130      177        GENE     MCKELLEN
+131       38         TOM     MCKELLEN
+132      128        CATE      MCQUEEN
+133       27       JULIA      MCQUEEN
+134       42         TOM      MIRANDA
+135      178        LISA       MONROE
+136      120    PENELOPE       MONROE
+137        7       GRACE       MOSTEL
+138       99         JIM       MOSTEL
+139       61   CHRISTIAN       NEESON
+140       62       JAYNE       NEESON
+141        6       BETTE    NICHOLSON
+142      125      ALBERT        NOLTE
+143      150       JAYNE        NOLTE
+144      122       SALMA        NOLTE
+145      108      WARREN        NOLTE
+146       34      AUDREY      OLIVIER
+147       15        CUBA      OLIVIER
+148       69     KENNETH      PALTROW
+149       21     KIRSTEN      PALTROW
+150       33       MILLA         PECK
+151       30      SANDRA         PECK
+152       87     SPENCER         PECK
+153       73        GARY         PENN
+154      133     RICHARD         PENN
+155       88     KENNETH        PESCI
+156      171     OLYMPIA     PFEIFFER
+157       51        GARY      PHOENIX
+158       54    PENELOPE      PINKETT
+159       84       JAMES         PITT
+160       75        BURT        POSEY
+161       93       ELLEN      PRESLEY
+162      135        RITA     REYNOLDS
+163      142        JADA        RYDER
+164      195       JAYNE  SILVERSTONE
+165      180        JEFF  SILVERSTONE
+166       78     GROUCHO      SINATRA
+167       31       SISSY     SOBIESKI
+168       44        NICK     STALLONE
+169       24     CAMERON       STREEP
+170      116         DAN       STREEP
+171      192        JOHN       SUVARI
+172        9         JOE        SWANK
+173      155         IAN        TANDY
+174       66        MARY        TANDY
+175       59      DUSTIN       TAUTOU
+176      193        BURT       TEMPLE
+177       53        MENA       TEMPLE
+178      149     RUSSELL       TEMPLE
+179      200       THORA       TEMPLE
+180      126     FRANCES        TOMEI
+181       18         DAN         TORN
+182       94     KENNETH         TORN
+183      102      WALTER         TORN
+184       20     LUCILLE        TRACY
+185      117       RENEE        TRACY
+186       17       HELEN       VOIGHT
+187       95       DARYL     WAHLBERG
+188        2        NICK     WAHLBERG
+189      196        BELA       WALKEN
+190       29        ALEC        WAYNE
+191      163 CHRISTOPHER         WEST
+192      197       REESE         WEST
+193      172     GROUCHO     WILLIAMS
+194      137      MORGAN     WILLIAMS
+195       72        SEAN     WILLIAMS
+196       83         BEN       WILLIS
+197       96        GENE       WILLIS
+198      164    HUMPHREY       WILLIS
+199      168        WILL       WILSON
+200      147         FAY      WINSLET
+201       68         RIP      WINSLET
+202      144      ANGELA  WITHERSPOON
+203      156         FAY         WOOD
+204       13         UMA         WOOD
+205       63     CAMERON         WRAY
+206      111     CAMERON    ZELLWEGER
+207      186       JULIA    ZELLWEGER
+208       85      MINNIE    ZELLWEGER
+
+
+
+
+

Exercise 2

+

Retrieve the actor ID, first name, and last name for actors whose last name equals ‘WILLIAMS’ or ‘DAVIS’.

+
+
actorWD <- dbGetQuery(con,
+    "SELECT actor_id, first_name, last_name
+    FROM actor
+    WHERE last_name IN ('WILLIAMS', 'DAVIS')")
+
+print(actorWD)
+
+
  actor_id first_name last_name
+1        4   JENNIFER     DAVIS
+2       72       SEAN  WILLIAMS
+3      101      SUSAN     DAVIS
+4      110      SUSAN     DAVIS
+5      137     MORGAN  WILLIAMS
+6      172    GROUCHO  WILLIAMS
+
+
+
+
+

Exercise 3

+

Write a query against the rental table that returns the IDs of the customers who rented a film on July 5, 2005 (use the rental.rental_date column, and you can use the date() function to ignore the time component). Include a single row for each distinct customer ID.

+
+
rentaldate <- dbGetQuery(con,
+    "SELECT customer_id, rental_date
+    FROM rental
+    WHERE date(rental_date) = '2005-07-05'")
+
+print(rentaldate)
+
+
   customer_id         rental_date
+1          565 2005-07-05 22:49:24
+2          242 2005-07-05 22:51:44
+3           37 2005-07-05 22:56:33
+4           60 2005-07-05 22:57:34
+5          594 2005-07-05 22:59:53
+6            8 2005-07-05 23:01:21
+7          490 2005-07-05 23:02:37
+8          476 2005-07-05 23:05:17
+9          322 2005-07-05 23:05:44
+10         298 2005-07-05 23:08:53
+11         382 2005-07-05 23:11:43
+12         138 2005-07-05 23:13:07
+13         520 2005-07-05 23:13:22
+14         536 2005-07-05 23:13:51
+15         114 2005-07-05 23:23:11
+16         111 2005-07-05 23:25:54
+17         296 2005-07-05 23:29:55
+18         586 2005-07-05 23:30:36
+19         349 2005-07-05 23:32:49
+20         397 2005-07-05 23:33:40
+21         369 2005-07-05 23:37:13
+22         421 2005-07-05 23:41:08
+23         142 2005-07-05 23:44:37
+24         169 2005-07-05 23:46:19
+25         348 2005-07-05 23:47:30
+26         553 2005-07-05 23:50:04
+27         295 2005-07-05 23:59:15
+
+
+
+
+

Exercise 4

+
+

Exercise 4.1

+

Construct a query that retrieves all rows from the payment table where the amount is either 1.99, 7.99, 9.99.

+
+
paymentamount <- dbGetQuery(con,
+    "SELECT *
+    FROM payment
+    WHERE amount IN ('1.99', '7.99', '9.99')")
+
+print(paymentamount)
+
+
    payment_id customer_id staff_id rental_id amount               payment_date
+1        16050         269        2         7   1.99 2007-01-24 21:40:19.996577
+2        16056         270        1       193   1.99 2007-01-26 05:10:14.996577
+3        16081         282        2        48   1.99 2007-01-25 04:49:12.996577
+4        16103         294        1       595   1.99 2007-01-28 12:28:20.996577
+5        16133         307        1       614   1.99 2007-01-28 14:01:54.996577
+6        16158         316        1      1065   1.99 2007-01-31 07:23:22.996577
+7        16160         318        1       224   9.99 2007-01-26 08:46:53.996577
+8        16161         319        1        15   9.99 2007-01-24 23:07:48.996577
+9        16180         330        2       967   7.99 2007-01-30 17:40:32.996577
+10       16206         351        1      1137   1.99 2007-01-31 17:48:40.996577
+11       16210         354        2       158   1.99 2007-01-25 23:55:37.996577
+12       16240         369        2       913   7.99 2007-01-30 09:33:24.996577
+13       16275         386        1       583   7.99 2007-01-28 10:17:21.996577
+14       16277         387        1       697   7.99 2007-01-29 00:32:30.996577
+15       16289         391        1       891   7.99 2007-01-30 06:11:38.996577
+16       16302         400        2       516   1.99 2007-01-28 01:40:13.996577
+17       16306         401        2       811   1.99 2007-01-29 17:59:08.996577
+18       16307         402        2       801   1.99 2007-01-29 16:04:16.996577
+19       16314         407        1       619   7.99 2007-01-28 14:20:52.996577
+20       16320         411        2       972   1.99 2007-01-30 18:49:33.996577
+21       16348         429        2       601   7.99 2007-01-28 12:36:48.996577
+22       16354         432        2       326   7.99 2007-01-26 23:38:37.996577
+23       16360         435        1       757   7.99 2007-01-29 08:58:13.996577
+24       16362         436        1        45   7.99 2007-01-25 04:28:05.996577
+25       16373         439        1       786   9.99 2007-01-29 13:45:54.996577
+26       16393         449        1       849   7.99 2007-01-29 23:51:33.996577
+27       16400         452        1       726   1.99 2007-01-29 04:33:55.996577
+28       16401         454        1       735   7.99 2007-01-29 06:36:39.996577
+29       16405         457        2      1024   7.99 2007-01-31 01:58:45.996577
+30       16414         463        1       560   1.99 2007-01-28 07:21:28.996577
+31       16416         464        2       373   1.99 2007-01-27 06:44:51.996577
+32       16426         469        2       506   7.99 2007-01-28 00:37:45.996577
+33       16428         469        2       936   1.99 2007-01-30 12:21:15.996577
+34       16439         474        1       816   7.99 2007-01-29 18:55:05.996577
+35       16446         479        1       709   7.99 2007-01-29 02:16:27.996577
+36       16449         480        2       822   9.99 2007-01-29 20:04:26.996577
+37       16469         493        1       543   7.99 2007-01-28 05:12:00.996577
+38       16475         497        1      1100   7.99 2007-01-31 12:31:47.996577
+39       16484         501        1       605   1.99 2007-01-28 13:07:36.996577
+40       16489         503        2       109   1.99 2007-01-25 17:08:46.996577
+41       16521         516        1       571   1.99 2007-01-28 08:46:07.996577
+42       16546         533        2       190   1.99 2007-01-26 04:39:54.996577
+43       16561         541        1      1021   7.99 2007-01-31 01:44:41.996577
+44       16566         543        2       476   1.99 2007-01-27 21:00:02.996577
+45       16575         547        2      1094   1.99 2007-01-31 11:32:15.996577
+46       16583         550        2       922   7.99 2007-01-30 10:24:21.996577
+47       16584         551        2       155   7.99 2007-01-25 23:43:31.996577
+48       16615         569        1       647   1.99 2007-01-28 17:51:18.996577
+49       16617         570        2      1060   7.99 2007-01-31 06:50:09.996577
+50       16618         571        1       228   9.99 2007-01-26 09:22:54.996577
+51       16620         572        2       559   7.99 2007-01-28 07:07:28.996577
+52       16659         596        2       782   1.99 2007-01-29 13:07:23.996577
+53       16669         204        1      1016   1.99 2007-01-31 01:18:09.996577
+54       16680           3        1       435   1.99 2007-01-27 15:45:35.996577
+55       16684           5        1      1142   1.99 2007-01-31 18:15:04.996577
+56       16702          14        1       346   9.99 2007-01-27 03:03:07.996577
+57       16717          19        2       110   9.99 2007-01-25 17:12:15.996577
+58       16724          20        2       546   1.99 2007-01-28 05:44:51.996577
+59       16736          25        1        90   7.99 2007-01-25 12:59:51.996577
+60       16743          29        2       194   1.99 2007-01-26 05:20:59.996577
+61       16773          49        2        96   1.99 2007-01-25 15:00:45.996577
+62       16790          53        2       856   9.99 2007-01-30 00:29:47.996577
+63       16796          55        1      1027   9.99 2007-01-31 02:14:45.996577
+64       16802          57        2       152   9.99 2007-01-25 23:09:36.996577
+65       16805          58        2       276   7.99 2007-01-26 15:44:33.996577
+66       16811          60        2       706   1.99 2007-01-29 01:34:15.996577
+67       16822          67        2       331   9.99 2007-01-27 00:50:52.996577
+68       16825          69        2       765   1.99 2007-01-29 10:07:00.996577
+69       16828          71        1       272   9.99 2007-01-26 14:55:37.996577
+70       16837          76        2       574   1.99 2007-01-28 09:12:54.996577
+71       16840          77        1       419   1.99 2007-01-27 13:43:37.996577
+72       16856          85        1       690   9.99 2007-01-28 23:23:19.996577
+73       16858          86        1        66   1.99 2007-01-25 08:03:38.996577
+74       16877         101        1       468   9.99 2007-01-27 19:41:36.996577
+75       16880         102        2       562   1.99 2007-01-28 07:29:47.996577
+76       16881         103        1       240   7.99 2007-01-26 11:08:49.996577
+77       16882         103        1       658   9.99 2007-01-28 18:51:49.996577
+78       16886         105        2       473   7.99 2007-01-27 20:05:00.996577
+79       16899         109        1      1061   7.99 2007-01-31 06:56:24.996577
+80       16902         110        1       515   7.99 2007-01-28 01:38:36.996577
+81       16903         110        2       538   1.99 2007-01-28 04:49:31.996577
+82       16922         120        2        68   7.99 2007-01-25 08:15:57.996577
+83       16944         131        2       944   7.99 2007-01-30 13:54:50.996577
+84       16960         142        1       575   9.99 2007-01-28 09:24:35.996577
+85       16966         146        2       762   7.99 2007-01-29 09:44:17.996577
+86       16980         154        2       865   7.99 2007-01-30 02:08:10.996577
+87       16990         159        2       549   1.99 2007-01-28 06:04:03.996577
+88       16999         162        1       285   1.99 2007-01-26 18:10:06.996577
+89       17002         164        2      1011   1.99 2007-01-31 00:34:05.996577
+90       17004         166        1       662   1.99 2007-01-28 19:37:57.996577
+91       17015         171        2       804   9.99 2007-01-29 16:38:50.996577
+92       17026         176        1       663   1.99 2007-01-28 19:51:28.996577
+93       17027         176        1      1062   7.99 2007-01-31 07:06:46.996577
+94       17038         184        1       567   1.99 2007-01-28 08:24:46.996577
+95       17042         186        1       581   1.99 2007-01-28 09:48:55.996577
+96       17044         187        1       252   7.99 2007-01-26 13:08:19.996577
+97       17050         192        1       895   1.99 2007-01-30 07:19:09.996577
+98       17054         194        2       677   7.99 2007-01-28 21:28:34.996577
+99       17058         196        1      1053   1.99 2007-01-31 05:41:10.996577
+100      17062         197        2       649   1.99 2007-01-28 18:04:11.996577
+101      17072         199        1       499   7.99 2007-01-27 23:33:33.996577
+102      17073         200        2       270   9.99 2007-01-26 14:49:22.996577
+103      17077         209        2       340   9.99 2007-01-27 02:23:51.996577
+104      17083         214        1       242   1.99 2007-01-26 11:33:34.996577
+105      17130         239        1      1022   7.99 2007-01-31 01:45:11.996577
+106      17135         241        1       627   7.99 2007-01-28 15:33:09.996577
+107      17142         244        1       797   1.99 2007-01-29 15:40:43.996577
+108      17145         245        1       519   7.99 2007-01-28 01:50:59.996577
+109      17157         248        2       330   7.99 2007-01-27 00:43:56.996577
+110      17169         251        1       309   1.99 2007-01-26 21:06:36.996577
+111      17201         266        1        86   1.99 2007-01-25 12:04:38.996577
+
+
+
+
+

Exercise 4.2

+

Construct a query that retrieves all rows from the payment table where the amount is greater then 5.

+
+
payment5 <- dbGetQuery(con,
+    "SELECT *
+    FROM payment
+    WHERE amount > 5")
+
+print(payment5)
+
+
    payment_id customer_id staff_id rental_id amount               payment_date
+1        16052         269        2       678   6.99 2007-01-28 21:44:14.996577
+2        16058         271        1      1096   8.99 2007-01-31 11:59:15.996577
+3        16060         272        1       405   6.99 2007-01-27 12:01:05.996577
+4        16061         272        1      1041   6.99 2007-01-31 04:14:49.996577
+5        16068         274        1       394   5.99 2007-01-27 09:54:37.996577
+6        16073         276        1       860  10.99 2007-01-30 01:13:42.996577
+7        16074         277        2       308   6.99 2007-01-26 20:30:05.996577
+8        16082         282        2       282   6.99 2007-01-26 17:24:52.996577
+9        16086         284        1      1145   6.99 2007-01-31 18:42:11.996577
+10       16087         286        2        81   6.99 2007-01-25 10:43:45.996577
+11       16092         288        2       427   6.99 2007-01-27 14:38:30.996577
+12       16094         288        2       565   5.99 2007-01-28 07:54:57.996577
+13       16102         293        2      1034   8.99 2007-01-31 03:22:06.996577
+14       16106         296        1       511   5.99 2007-01-28 01:32:30.996577
+15       16112         299        1       332   5.99 2007-01-27 00:55:36.996577
+16       16113         299        2       606   8.99 2007-01-28 13:17:05.996577
+17       16118         301        2       227   5.99 2007-01-26 09:20:12.996577
+18       16121         302        2        92   5.99 2007-01-25 14:07:12.996577
+19       16125         304        1       135  10.99 2007-01-25 20:27:24.996577
+20       16130         306        2       672   6.99 2007-01-28 20:33:55.996577
+21       16134         307        1       970   6.99 2007-01-30 18:18:54.996577
+22       16136         309        2       218   6.99 2007-01-26 07:55:35.996577
+23       16139         311        2       274   5.99 2007-01-26 15:17:17.996577
+24       16140         311        2       544   6.99 2007-01-28 05:31:26.996577
+25       16143         311        2      1128   6.99 2007-01-31 16:17:52.996577
+26       16147         312        2      1079   6.99 2007-01-31 09:16:43.996577
+27       16152         314        1        80   5.99 2007-01-25 10:40:33.996577
+28       16154         315        1       537   8.99 2007-01-28 04:49:21.996577
+29       16157         316        1       644   8.99 2007-01-28 17:27:38.996577
+30       16159         317        1       107   6.99 2007-01-25 16:56:35.996577
+31       16160         318        1       224   9.99 2007-01-26 08:46:53.996577
+32       16161         319        1        15   9.99 2007-01-24 23:07:48.996577
+33       16165         321        1       620   5.99 2007-01-28 14:23:11.996577
+34       16171         323        1       878   5.99 2007-01-30 04:17:39.996577
+35       16173         325        1       131   5.99 2007-01-25 20:11:12.996577
+36       16174         326        1       875   6.99 2007-01-30 04:06:50.996577
+37       16177         327        1       653   6.99 2007-01-28 18:40:46.996577
+38       16180         330        2       967   7.99 2007-01-30 17:40:32.996577
+39       16184         332        1      1000   6.99 2007-01-30 22:54:22.996577
+40       16186         334        1        13   6.99 2007-01-24 22:51:21.996577
+41       16187         334        1       431   8.99 2007-01-27 14:59:31.996577
+42       16188         337        1       374   6.99 2007-01-27 06:54:56.996577
+43       16190         337        1       839   8.99 2007-01-29 22:56:38.996577
+44       16192         339        1       876   5.99 2007-01-30 04:09:48.996577
+45       16196         344        2       813   5.99 2007-01-29 18:43:00.996577
+46       16208         353        2      1103   6.99 2007-01-31 12:52:44.996577
+47       16218         359        1       284   8.99 2007-01-26 17:50:10.996577
+48       16223         361        1       368   5.99 2007-01-27 06:10:55.996577
+49       16227         364        1       462   5.99 2007-01-27 18:39:02.996577
+50       16228         365        2       120   5.99 2007-01-25 18:06:13.996577
+51       16230         366        2       911   6.99 2007-01-30 09:18:48.996577
+52       16233         368        1        64   5.99 2007-01-25 07:49:55.996577
+53       16234         368        1       125   5.99 2007-01-25 19:17:16.996577
+54       16240         369        2       913   7.99 2007-01-30 09:33:24.996577
+55       16242         371        2       286   6.99 2007-01-26 18:13:17.996577
+56       16244         371        1       384   5.99 2007-01-27 08:46:46.996577
+57       16253         375        2       307   8.99 2007-01-26 20:16:39.996577
+58       16268         383        1       766   8.99 2007-01-29 10:15:28.996577
+59       16275         386        1       583   7.99 2007-01-28 10:17:21.996577
+60       16277         387        1       697   7.99 2007-01-29 00:32:30.996577
+61       16287         391        1       317   5.99 2007-01-26 21:52:22.996577
+62       16289         391        1       891   7.99 2007-01-30 06:11:38.996577
+63       16294         396        2       641   5.99 2007-01-28 17:14:13.996577
+64       16297         399        2        10   5.99 2007-01-24 22:30:47.996577
+65       16298         399        2       694   6.99 2007-01-29 00:18:09.996577
+66       16301         400        2       171   6.99 2007-01-26 01:42:41.996577
+67       16303         400        2       894   5.99 2007-01-30 06:59:57.996577
+68       16310         404        2      1081   5.99 2007-01-31 09:24:58.996577
+69       16314         407        1       619   7.99 2007-01-28 14:20:52.996577
+70       16316         408        2        59   5.99 2007-01-25 07:25:08.996577
+71       16318         409        1       310   6.99 2007-01-26 21:09:33.996577
+72       16334         417        2       630   8.99 2007-01-28 15:53:17.996577
+73       16342         425        2      1098   5.99 2007-01-31 12:20:14.996577
+74       16344         427        2        82   6.99 2007-01-25 10:46:12.996577
+75       16346         429        2       150   5.99 2007-01-25 22:57:05.996577
+76       16348         429        2       601   7.99 2007-01-28 12:36:48.996577
+77       16354         432        2       326   7.99 2007-01-26 23:38:37.996577
+78       16355         432        1       550   5.99 2007-01-28 06:07:42.996577
+79       16356         432        1       897   8.99 2007-01-30 07:38:27.996577
+80       16357         433        2       146   8.99 2007-01-25 22:35:37.996577
+81       16358         433        1       691  10.99 2007-01-28 23:29:52.996577
+82       16359         434        2       508   5.99 2007-01-28 01:09:16.996577
+83       16360         435        1       757   7.99 2007-01-29 08:58:13.996577
+84       16362         436        1        45   7.99 2007-01-25 04:28:05.996577
+85       16364         436        1       848   5.99 2007-01-29 23:48:19.996577
+86       16367         437        1       666   5.99 2007-01-28 20:17:17.996577
+87       16370         438        1      1138   6.99 2007-01-31 17:58:53.996577
+88       16373         439        1       786   9.99 2007-01-29 13:45:54.996577
+89       16377         442        2       558   6.99 2007-01-28 07:07:09.996577
+90       16378         442        1       632   5.99 2007-01-28 16:06:16.996577
+91       16380         444        1       201   8.99 2007-01-26 05:42:11.996577
+92       16392         449        2       325   5.99 2007-01-26 23:38:21.996577
+93       16393         449        1       849   7.99 2007-01-29 23:51:33.996577
+94       16401         454        1       735   7.99 2007-01-29 06:36:39.996577
+95       16405         457        2      1024   7.99 2007-01-31 01:58:45.996577
+96       16411         461        1       684   6.99 2007-01-28 22:41:41.996577
+97       16420         468        2       101   6.99 2007-01-25 15:45:30.996577
+98       16422         468        2       296   6.99 2007-01-26 19:03:45.996577
+99       16426         469        2       506   7.99 2007-01-28 00:37:45.996577
+100      16439         474        1       816   7.99 2007-01-29 18:55:05.996577
+101      16446         479        1       709   7.99 2007-01-29 02:16:27.996577
+102      16448         480        1       720   6.99 2007-01-29 03:45:56.996577
+103      16449         480        2       822   9.99 2007-01-29 20:04:26.996577
+104      16450         481        2      1109   5.99 2007-01-31 13:40:41.996577
+105      16451         482        1       259   8.99 2007-01-26 14:01:12.996577
+106      16454         483        2       742   6.99 2007-01-29 07:04:56.996577
+107      16459         486        1       909   8.99 2007-01-30 09:12:04.996577
+108      16464         490        2       585   6.99 2007-01-28 10:19:11.996577
+109      16469         493        1       543   7.99 2007-01-28 05:12:00.996577
+110      16475         497        1      1100   7.99 2007-01-31 12:31:47.996577
+111      16477         498        1       429   8.99 2007-01-27 14:49:52.996577
+112      16480         500        1       112   8.99 2007-01-25 17:25:50.996577
+113      16481         500        1       389   8.99 2007-01-27 09:14:07.996577
+114      16490         503        1       353   5.99 2007-01-27 04:32:05.996577
+115      16493         504        2       136   5.99 2007-01-25 20:30:56.996577
+116      16501         506        1       547   8.99 2007-01-28 05:52:54.996577
+117      16510         509        1       831   8.99 2007-01-29 21:18:51.996577
+118      16511         510        1        75   8.99 2007-01-25 09:42:00.996577
+119      16512         510        1       372   5.99 2007-01-27 06:42:24.996577
+120      16518         515        2       187   8.99 2007-01-26 04:11:03.996577
+121      16519         515        2       292   6.99 2007-01-26 18:50:38.996577
+122      16525         520        1       962   6.99 2007-01-30 17:13:43.996577
+123      16526         522        2       426   5.99 2007-01-27 14:25:23.996577
+124      16531         525        1       437   5.99 2007-01-27 16:15:48.996577
+125      16537         528        1       533   5.99 2007-01-28 04:43:12.996577
+126      16539         528        2       793   5.99 2007-01-29 15:12:34.996577
+127      16547         533        1       615   5.99 2007-01-28 14:04:18.996577
+128      16548         534        2       304   5.99 2007-01-26 19:49:54.996577
+129      16555         536        1       929   6.99 2007-01-30 11:01:05.996577
+130      16561         541        1      1021   7.99 2007-01-31 01:44:41.996577
+131      16565         543        1       243   6.99 2007-01-26 11:34:31.996577
+132      16571         546        1       197   5.99 2007-01-26 05:27:47.996577
+133      16572         546        1       482   6.99 2007-01-27 21:21:28.996577
+134      16574         547        2       443   8.99 2007-01-27 17:03:46.996577
+135      16576         548        2       177   6.99 2007-01-26 02:42:55.996577
+136      16583         550        2       922   7.99 2007-01-30 10:24:21.996577
+137      16584         551        2       155   7.99 2007-01-25 23:43:31.996577
+138      16594         556        2       772   5.99 2007-01-29 11:36:32.996577
+139      16603         562        1      1139   5.99 2007-01-31 18:03:18.996577
+140      16605         563        2       773   5.99 2007-01-29 11:46:31.996577
+141      16606         564        2       195   5.99 2007-01-26 05:21:02.996577
+142      16608         565        1       458   6.99 2007-01-27 18:27:02.996577
+143      16610         566        2       234   5.99 2007-01-26 10:15:46.996577
+144      16617         570        2      1060   7.99 2007-01-31 06:50:09.996577
+145      16618         571        1       228   9.99 2007-01-26 09:22:54.996577
+146      16620         572        2       559   7.99 2007-01-28 07:07:28.996577
+147      16630         577        2       291   5.99 2007-01-26 18:49:13.996577
+148      16640         586        1       900   8.99 2007-01-30 08:07:07.996577
+149      16650         593        1       991   8.99 2007-01-30 21:57:48.996577
+150      16652         594        1       360   8.99 2007-01-27 05:19:40.996577
+151      16654         594        1      1045   6.99 2007-01-31 04:57:27.996577
+152      16655         595        1       613   6.99 2007-01-28 13:55:48.996577
+153      16661         596        1       974   6.99 2007-01-30 18:57:08.996577
+154      16663         597        2       514   8.99 2007-01-28 01:37:54.996577
+155      16673         207        2       826   6.99 2007-01-29 20:24:41.996577
+156      16683           5        1      1085   6.99 2007-01-31 09:44:09.996577
+157      16688           7        2        46   5.99 2007-01-25 04:32:34.996577
+158      16692           7        1      1063   5.99 2007-01-31 07:12:55.996577
+159      16693           8        2       866   6.99 2007-01-30 02:12:20.996577
+160      16698          11        1       987   6.99 2007-01-30 21:27:38.996577
+161      16702          14        1       346   9.99 2007-01-27 03:03:07.996577
+162      16703          14        1       525   5.99 2007-01-28 02:53:59.996577
+163      16717          19        2       110   9.99 2007-01-25 17:12:15.996577
+164      16718          19        1       179   6.99 2007-01-26 02:54:32.996577
+165      16723          20        2       497   6.99 2007-01-27 23:23:05.996577
+166      16730          22        2       820   8.99 2007-01-29 19:35:48.996577
+167      16731          23        1       129   8.99 2007-01-25 19:48:29.996577
+168      16734          24        2      1007   6.99 2007-01-30 23:30:54.996577
+169      16736          25        1        90   7.99 2007-01-25 12:59:51.996577
+170      16749          35        1       424   6.99 2007-01-27 14:02:27.996577
+171      16755          42        1       635   5.99 2007-01-28 16:15:23.996577
+172      16769          47        1       300   6.99 2007-01-26 19:25:26.996577
+173      16789          53        2       783   5.99 2007-01-29 13:09:44.996577
+174      16790          53        2       856   9.99 2007-01-30 00:29:47.996577
+175      16796          55        1      1027   9.99 2007-01-31 02:14:45.996577
+176      16799          56        1       341   5.99 2007-01-27 02:30:08.996577
+177      16801          56        1       569   6.99 2007-01-28 08:41:07.996577
+178      16802          57        2       152   9.99 2007-01-25 23:09:36.996577
+179      16805          58        2       276   7.99 2007-01-26 15:44:33.996577
+180      16809          59        1      1154   5.99 2007-01-31 20:10:35.996577
+181      16822          67        2       331   9.99 2007-01-27 00:50:52.996577
+182      16828          71        1       272   9.99 2007-01-26 14:55:37.996577
+183      16834          74        2      1121   6.99 2007-01-31 15:06:02.996577
+184      16843          77        1       760   5.99 2007-01-29 09:35:51.996577
+185      16849          82        2       288   8.99 2007-01-26 18:16:15.996577
+186      16854          84        1       739   6.99 2007-01-29 06:56:44.996577
+187      16856          85        1       690   9.99 2007-01-28 23:23:19.996577
+188      16864          89        1       740   5.99 2007-01-29 06:59:02.996577
+189      16865          91        2       216   5.99 2007-01-26 07:46:09.996577
+190      16866          92        1       271   5.99 2007-01-26 14:50:27.996577
+191      16869          93        2       420   6.99 2007-01-27 13:48:04.996577
+192      16877         101        1       468   9.99 2007-01-27 19:41:36.996577
+193      16881         103        1       240   7.99 2007-01-26 11:08:49.996577
+194      16882         103        1       658   9.99 2007-01-28 18:51:49.996577
+195      16883         104        1       163  10.99 2007-01-26 00:54:49.996577
+196      16885         105        1       327   8.99 2007-01-26 23:47:23.996577
+197      16886         105        2       473   7.99 2007-01-27 20:05:00.996577
+198      16888         105        1       779   6.99 2007-01-29 12:45:43.996577
+199      16891         107        1       170   5.99 2007-01-26 01:39:38.996577
+200      16892         107        1      1026   5.99 2007-01-31 02:13:52.996577
+201      16895         109        1       203   5.99 2007-01-26 05:56:23.996577
+202      16899         109        1      1061   7.99 2007-01-31 06:56:24.996577
+203      16902         110        1       515   7.99 2007-01-28 01:38:36.996577
+204      16918         118        2       351   5.99 2007-01-27 04:07:29.996577
+205      16920         119        1       235   5.99 2007-01-26 10:19:35.996577
+206      16921         119        2       540   6.99 2007-01-28 05:08:51.996577
+207      16922         120        2        68   7.99 2007-01-25 08:15:57.996577
+208      16931         124        2      1130   5.99 2007-01-31 16:42:23.996577
+209      16938         128        2       888   5.99 2007-01-30 05:41:40.996577
+210      16944         131        2       944   7.99 2007-01-30 13:54:50.996577
+211      16945         133        1       275   6.99 2007-01-26 15:38:19.996577
+212      16949         134        1       814   6.99 2007-01-29 18:44:38.996577
+213      16951         135        1        78   5.99 2007-01-25 10:03:44.996577
+214      16958         142        2        11   8.99 2007-01-24 22:37:28.996577
+215      16960         142        1       575   9.99 2007-01-28 09:24:35.996577
+216      16966         146        2       762   7.99 2007-01-29 09:44:17.996577
+217      16976         151        2       418   5.99 2007-01-27 13:41:43.996577
+218      16979         154        1       469   5.99 2007-01-27 19:42:52.996577
+219      16980         154        2       865   7.99 2007-01-30 02:08:10.996577
+220      16981         154        2       978   5.99 2007-01-30 19:59:18.996577
+221      16983         156        2       899   6.99 2007-01-30 07:57:56.996577
+222      16988         158        1       293   5.99 2007-01-26 18:55:28.996577
+223      16995         161        1       520   5.99 2007-01-28 01:56:03.996577
+224      17013         170        1       377   5.99 2007-01-27 07:32:31.996577
+225      17015         171        2       804   9.99 2007-01-29 16:38:50.996577
+226      17017         172        1       685   6.99 2007-01-28 22:46:17.996577
+227      17021         174        1        41   5.99 2007-01-25 03:40:55.996577
+228      17024         176        2       380   6.99 2007-01-27 08:03:05.996577
+229      17027         176        1      1062   7.99 2007-01-31 07:06:46.996577
+230      17029         179        1       759   6.99 2007-01-29 09:26:23.996577
+231      17032         181        2       579   6.99 2007-01-28 09:47:49.996577
+232      17044         187        1       252   7.99 2007-01-26 13:08:19.996577
+233      17045         189        2      1117   5.99 2007-01-31 14:43:57.996577
+234      17054         194        2       677   7.99 2007-01-28 21:28:34.996577
+235      17055         196        2       106  11.99 2007-01-25 16:46:45.996577
+236      17056         196        2       178   5.99 2007-01-26 02:50:12.996577
+237      17072         199        1       499   7.99 2007-01-27 23:33:33.996577
+238      17073         200        2       270   9.99 2007-01-26 14:49:22.996577
+239      17075         201        1       670   6.99 2007-01-28 20:32:29.996577
+240      17076         201        2       756   5.99 2007-01-29 08:57:11.996577
+241      17077         209        2       340   9.99 2007-01-27 02:23:51.996577
+242      17092         217        2      1141   8.99 2007-01-31 18:10:28.996577
+243      17097         222        1         5   6.99 2007-01-24 21:33:47.996577
+244      17111         231        1       329   5.99 2007-01-27 00:25:40.996577
+245      17112         231        1       479   6.99 2007-01-27 21:07:36.996577
+246      17113         231        1       512   8.99 2007-01-28 01:36:16.996577
+247      17129         239        1       636   6.99 2007-01-28 16:16:24.996577
+248      17130         239        1      1022   7.99 2007-01-31 01:45:11.996577
+249      17131         239        2      1082   5.99 2007-01-31 09:30:27.996577
+250      17135         241        1       627   7.99 2007-01-28 15:33:09.996577
+251      17145         245        1       519   7.99 2007-01-28 01:50:59.996577
+252      17148         245        2       948   8.99 2007-01-30 14:12:53.996577
+253      17149         246        1       124   6.99 2007-01-25 19:14:37.996577
+254      17150         246        2       421   8.99 2007-01-27 13:58:39.996577
+255      17151         246        2       434   5.99 2007-01-27 15:22:53.996577
+256      17156         247        1       450   6.99 2007-01-27 17:47:20.996577
+257      17157         248        2       330   7.99 2007-01-27 00:43:56.996577
+258      17161         249        1       438   6.99 2007-01-27 16:21:00.996577
+259      17163         250        1        61   5.99 2007-01-25 07:30:23.996577
+260      17176         253        1       566   6.99 2007-01-28 08:20:05.996577
+261      17180         254        1      1108   5.99 2007-01-31 13:33:38.996577
+262      17189         259        2       722   6.99 2007-01-29 03:58:57.996577
+263      17191         259        1      1147   5.99 2007-01-31 19:06:18.996577
+264      17192         260        1      1101   8.99 2007-01-31 12:42:25.996577
+265      17195         261        2       542   6.99 2007-01-28 05:10:39.996577
+266      17203         267        2        91   6.99 2007-01-25 13:25:48.996577
+
+
+
+
+

Exercise 4.2

+

Construct a query that retrieves all rows from the payment table where the amount is greater then 5 and less then 8.

+
+
payment58 <- dbGetQuery(con,
+    "SELECT *
+    FROM payment
+    WHERE amount > 5 AND amount < 8")
+
+print(payment58)
+
+
    payment_id customer_id staff_id rental_id amount               payment_date
+1        16052         269        2       678   6.99 2007-01-28 21:44:14.996577
+2        16060         272        1       405   6.99 2007-01-27 12:01:05.996577
+3        16061         272        1      1041   6.99 2007-01-31 04:14:49.996577
+4        16068         274        1       394   5.99 2007-01-27 09:54:37.996577
+5        16074         277        2       308   6.99 2007-01-26 20:30:05.996577
+6        16082         282        2       282   6.99 2007-01-26 17:24:52.996577
+7        16086         284        1      1145   6.99 2007-01-31 18:42:11.996577
+8        16087         286        2        81   6.99 2007-01-25 10:43:45.996577
+9        16092         288        2       427   6.99 2007-01-27 14:38:30.996577
+10       16094         288        2       565   5.99 2007-01-28 07:54:57.996577
+11       16106         296        1       511   5.99 2007-01-28 01:32:30.996577
+12       16112         299        1       332   5.99 2007-01-27 00:55:36.996577
+13       16118         301        2       227   5.99 2007-01-26 09:20:12.996577
+14       16121         302        2        92   5.99 2007-01-25 14:07:12.996577
+15       16130         306        2       672   6.99 2007-01-28 20:33:55.996577
+16       16134         307        1       970   6.99 2007-01-30 18:18:54.996577
+17       16136         309        2       218   6.99 2007-01-26 07:55:35.996577
+18       16139         311        2       274   5.99 2007-01-26 15:17:17.996577
+19       16140         311        2       544   6.99 2007-01-28 05:31:26.996577
+20       16143         311        2      1128   6.99 2007-01-31 16:17:52.996577
+21       16147         312        2      1079   6.99 2007-01-31 09:16:43.996577
+22       16152         314        1        80   5.99 2007-01-25 10:40:33.996577
+23       16159         317        1       107   6.99 2007-01-25 16:56:35.996577
+24       16165         321        1       620   5.99 2007-01-28 14:23:11.996577
+25       16171         323        1       878   5.99 2007-01-30 04:17:39.996577
+26       16173         325        1       131   5.99 2007-01-25 20:11:12.996577
+27       16174         326        1       875   6.99 2007-01-30 04:06:50.996577
+28       16177         327        1       653   6.99 2007-01-28 18:40:46.996577
+29       16180         330        2       967   7.99 2007-01-30 17:40:32.996577
+30       16184         332        1      1000   6.99 2007-01-30 22:54:22.996577
+31       16186         334        1        13   6.99 2007-01-24 22:51:21.996577
+32       16188         337        1       374   6.99 2007-01-27 06:54:56.996577
+33       16192         339        1       876   5.99 2007-01-30 04:09:48.996577
+34       16196         344        2       813   5.99 2007-01-29 18:43:00.996577
+35       16208         353        2      1103   6.99 2007-01-31 12:52:44.996577
+36       16223         361        1       368   5.99 2007-01-27 06:10:55.996577
+37       16227         364        1       462   5.99 2007-01-27 18:39:02.996577
+38       16228         365        2       120   5.99 2007-01-25 18:06:13.996577
+39       16230         366        2       911   6.99 2007-01-30 09:18:48.996577
+40       16233         368        1        64   5.99 2007-01-25 07:49:55.996577
+41       16234         368        1       125   5.99 2007-01-25 19:17:16.996577
+42       16240         369        2       913   7.99 2007-01-30 09:33:24.996577
+43       16242         371        2       286   6.99 2007-01-26 18:13:17.996577
+44       16244         371        1       384   5.99 2007-01-27 08:46:46.996577
+45       16275         386        1       583   7.99 2007-01-28 10:17:21.996577
+46       16277         387        1       697   7.99 2007-01-29 00:32:30.996577
+47       16287         391        1       317   5.99 2007-01-26 21:52:22.996577
+48       16289         391        1       891   7.99 2007-01-30 06:11:38.996577
+49       16294         396        2       641   5.99 2007-01-28 17:14:13.996577
+50       16297         399        2        10   5.99 2007-01-24 22:30:47.996577
+51       16298         399        2       694   6.99 2007-01-29 00:18:09.996577
+52       16301         400        2       171   6.99 2007-01-26 01:42:41.996577
+53       16303         400        2       894   5.99 2007-01-30 06:59:57.996577
+54       16310         404        2      1081   5.99 2007-01-31 09:24:58.996577
+55       16314         407        1       619   7.99 2007-01-28 14:20:52.996577
+56       16316         408        2        59   5.99 2007-01-25 07:25:08.996577
+57       16318         409        1       310   6.99 2007-01-26 21:09:33.996577
+58       16342         425        2      1098   5.99 2007-01-31 12:20:14.996577
+59       16344         427        2        82   6.99 2007-01-25 10:46:12.996577
+60       16346         429        2       150   5.99 2007-01-25 22:57:05.996577
+61       16348         429        2       601   7.99 2007-01-28 12:36:48.996577
+62       16354         432        2       326   7.99 2007-01-26 23:38:37.996577
+63       16355         432        1       550   5.99 2007-01-28 06:07:42.996577
+64       16359         434        2       508   5.99 2007-01-28 01:09:16.996577
+65       16360         435        1       757   7.99 2007-01-29 08:58:13.996577
+66       16362         436        1        45   7.99 2007-01-25 04:28:05.996577
+67       16364         436        1       848   5.99 2007-01-29 23:48:19.996577
+68       16367         437        1       666   5.99 2007-01-28 20:17:17.996577
+69       16370         438        1      1138   6.99 2007-01-31 17:58:53.996577
+70       16377         442        2       558   6.99 2007-01-28 07:07:09.996577
+71       16378         442        1       632   5.99 2007-01-28 16:06:16.996577
+72       16392         449        2       325   5.99 2007-01-26 23:38:21.996577
+73       16393         449        1       849   7.99 2007-01-29 23:51:33.996577
+74       16401         454        1       735   7.99 2007-01-29 06:36:39.996577
+75       16405         457        2      1024   7.99 2007-01-31 01:58:45.996577
+76       16411         461        1       684   6.99 2007-01-28 22:41:41.996577
+77       16420         468        2       101   6.99 2007-01-25 15:45:30.996577
+78       16422         468        2       296   6.99 2007-01-26 19:03:45.996577
+79       16426         469        2       506   7.99 2007-01-28 00:37:45.996577
+80       16439         474        1       816   7.99 2007-01-29 18:55:05.996577
+81       16446         479        1       709   7.99 2007-01-29 02:16:27.996577
+82       16448         480        1       720   6.99 2007-01-29 03:45:56.996577
+83       16450         481        2      1109   5.99 2007-01-31 13:40:41.996577
+84       16454         483        2       742   6.99 2007-01-29 07:04:56.996577
+85       16464         490        2       585   6.99 2007-01-28 10:19:11.996577
+86       16469         493        1       543   7.99 2007-01-28 05:12:00.996577
+87       16475         497        1      1100   7.99 2007-01-31 12:31:47.996577
+88       16490         503        1       353   5.99 2007-01-27 04:32:05.996577
+89       16493         504        2       136   5.99 2007-01-25 20:30:56.996577
+90       16512         510        1       372   5.99 2007-01-27 06:42:24.996577
+91       16519         515        2       292   6.99 2007-01-26 18:50:38.996577
+92       16525         520        1       962   6.99 2007-01-30 17:13:43.996577
+93       16526         522        2       426   5.99 2007-01-27 14:25:23.996577
+94       16531         525        1       437   5.99 2007-01-27 16:15:48.996577
+95       16537         528        1       533   5.99 2007-01-28 04:43:12.996577
+96       16539         528        2       793   5.99 2007-01-29 15:12:34.996577
+97       16547         533        1       615   5.99 2007-01-28 14:04:18.996577
+98       16548         534        2       304   5.99 2007-01-26 19:49:54.996577
+99       16555         536        1       929   6.99 2007-01-30 11:01:05.996577
+100      16561         541        1      1021   7.99 2007-01-31 01:44:41.996577
+101      16565         543        1       243   6.99 2007-01-26 11:34:31.996577
+102      16571         546        1       197   5.99 2007-01-26 05:27:47.996577
+103      16572         546        1       482   6.99 2007-01-27 21:21:28.996577
+104      16576         548        2       177   6.99 2007-01-26 02:42:55.996577
+105      16583         550        2       922   7.99 2007-01-30 10:24:21.996577
+106      16584         551        2       155   7.99 2007-01-25 23:43:31.996577
+107      16594         556        2       772   5.99 2007-01-29 11:36:32.996577
+108      16603         562        1      1139   5.99 2007-01-31 18:03:18.996577
+109      16605         563        2       773   5.99 2007-01-29 11:46:31.996577
+110      16606         564        2       195   5.99 2007-01-26 05:21:02.996577
+111      16608         565        1       458   6.99 2007-01-27 18:27:02.996577
+112      16610         566        2       234   5.99 2007-01-26 10:15:46.996577
+113      16617         570        2      1060   7.99 2007-01-31 06:50:09.996577
+114      16620         572        2       559   7.99 2007-01-28 07:07:28.996577
+115      16630         577        2       291   5.99 2007-01-26 18:49:13.996577
+116      16654         594        1      1045   6.99 2007-01-31 04:57:27.996577
+117      16655         595        1       613   6.99 2007-01-28 13:55:48.996577
+118      16661         596        1       974   6.99 2007-01-30 18:57:08.996577
+119      16673         207        2       826   6.99 2007-01-29 20:24:41.996577
+120      16683           5        1      1085   6.99 2007-01-31 09:44:09.996577
+121      16688           7        2        46   5.99 2007-01-25 04:32:34.996577
+122      16692           7        1      1063   5.99 2007-01-31 07:12:55.996577
+123      16693           8        2       866   6.99 2007-01-30 02:12:20.996577
+124      16698          11        1       987   6.99 2007-01-30 21:27:38.996577
+125      16703          14        1       525   5.99 2007-01-28 02:53:59.996577
+126      16718          19        1       179   6.99 2007-01-26 02:54:32.996577
+127      16723          20        2       497   6.99 2007-01-27 23:23:05.996577
+128      16734          24        2      1007   6.99 2007-01-30 23:30:54.996577
+129      16736          25        1        90   7.99 2007-01-25 12:59:51.996577
+130      16749          35        1       424   6.99 2007-01-27 14:02:27.996577
+131      16755          42        1       635   5.99 2007-01-28 16:15:23.996577
+132      16769          47        1       300   6.99 2007-01-26 19:25:26.996577
+133      16789          53        2       783   5.99 2007-01-29 13:09:44.996577
+134      16799          56        1       341   5.99 2007-01-27 02:30:08.996577
+135      16801          56        1       569   6.99 2007-01-28 08:41:07.996577
+136      16805          58        2       276   7.99 2007-01-26 15:44:33.996577
+137      16809          59        1      1154   5.99 2007-01-31 20:10:35.996577
+138      16834          74        2      1121   6.99 2007-01-31 15:06:02.996577
+139      16843          77        1       760   5.99 2007-01-29 09:35:51.996577
+140      16854          84        1       739   6.99 2007-01-29 06:56:44.996577
+141      16864          89        1       740   5.99 2007-01-29 06:59:02.996577
+142      16865          91        2       216   5.99 2007-01-26 07:46:09.996577
+143      16866          92        1       271   5.99 2007-01-26 14:50:27.996577
+144      16869          93        2       420   6.99 2007-01-27 13:48:04.996577
+145      16881         103        1       240   7.99 2007-01-26 11:08:49.996577
+146      16886         105        2       473   7.99 2007-01-27 20:05:00.996577
+147      16888         105        1       779   6.99 2007-01-29 12:45:43.996577
+148      16891         107        1       170   5.99 2007-01-26 01:39:38.996577
+149      16892         107        1      1026   5.99 2007-01-31 02:13:52.996577
+150      16895         109        1       203   5.99 2007-01-26 05:56:23.996577
+151      16899         109        1      1061   7.99 2007-01-31 06:56:24.996577
+152      16902         110        1       515   7.99 2007-01-28 01:38:36.996577
+153      16918         118        2       351   5.99 2007-01-27 04:07:29.996577
+154      16920         119        1       235   5.99 2007-01-26 10:19:35.996577
+155      16921         119        2       540   6.99 2007-01-28 05:08:51.996577
+156      16922         120        2        68   7.99 2007-01-25 08:15:57.996577
+157      16931         124        2      1130   5.99 2007-01-31 16:42:23.996577
+158      16938         128        2       888   5.99 2007-01-30 05:41:40.996577
+159      16944         131        2       944   7.99 2007-01-30 13:54:50.996577
+160      16945         133        1       275   6.99 2007-01-26 15:38:19.996577
+161      16949         134        1       814   6.99 2007-01-29 18:44:38.996577
+162      16951         135        1        78   5.99 2007-01-25 10:03:44.996577
+163      16966         146        2       762   7.99 2007-01-29 09:44:17.996577
+164      16976         151        2       418   5.99 2007-01-27 13:41:43.996577
+165      16979         154        1       469   5.99 2007-01-27 19:42:52.996577
+166      16980         154        2       865   7.99 2007-01-30 02:08:10.996577
+167      16981         154        2       978   5.99 2007-01-30 19:59:18.996577
+168      16983         156        2       899   6.99 2007-01-30 07:57:56.996577
+169      16988         158        1       293   5.99 2007-01-26 18:55:28.996577
+170      16995         161        1       520   5.99 2007-01-28 01:56:03.996577
+171      17013         170        1       377   5.99 2007-01-27 07:32:31.996577
+172      17017         172        1       685   6.99 2007-01-28 22:46:17.996577
+173      17021         174        1        41   5.99 2007-01-25 03:40:55.996577
+174      17024         176        2       380   6.99 2007-01-27 08:03:05.996577
+175      17027         176        1      1062   7.99 2007-01-31 07:06:46.996577
+176      17029         179        1       759   6.99 2007-01-29 09:26:23.996577
+177      17032         181        2       579   6.99 2007-01-28 09:47:49.996577
+178      17044         187        1       252   7.99 2007-01-26 13:08:19.996577
+179      17045         189        2      1117   5.99 2007-01-31 14:43:57.996577
+180      17054         194        2       677   7.99 2007-01-28 21:28:34.996577
+181      17056         196        2       178   5.99 2007-01-26 02:50:12.996577
+182      17072         199        1       499   7.99 2007-01-27 23:33:33.996577
+183      17075         201        1       670   6.99 2007-01-28 20:32:29.996577
+184      17076         201        2       756   5.99 2007-01-29 08:57:11.996577
+185      17097         222        1         5   6.99 2007-01-24 21:33:47.996577
+186      17111         231        1       329   5.99 2007-01-27 00:25:40.996577
+187      17112         231        1       479   6.99 2007-01-27 21:07:36.996577
+188      17129         239        1       636   6.99 2007-01-28 16:16:24.996577
+189      17130         239        1      1022   7.99 2007-01-31 01:45:11.996577
+190      17131         239        2      1082   5.99 2007-01-31 09:30:27.996577
+191      17135         241        1       627   7.99 2007-01-28 15:33:09.996577
+192      17145         245        1       519   7.99 2007-01-28 01:50:59.996577
+193      17149         246        1       124   6.99 2007-01-25 19:14:37.996577
+194      17151         246        2       434   5.99 2007-01-27 15:22:53.996577
+195      17156         247        1       450   6.99 2007-01-27 17:47:20.996577
+196      17157         248        2       330   7.99 2007-01-27 00:43:56.996577
+197      17161         249        1       438   6.99 2007-01-27 16:21:00.996577
+198      17163         250        1        61   5.99 2007-01-25 07:30:23.996577
+199      17176         253        1       566   6.99 2007-01-28 08:20:05.996577
+200      17180         254        1      1108   5.99 2007-01-31 13:33:38.996577
+201      17189         259        2       722   6.99 2007-01-29 03:58:57.996577
+202      17191         259        1      1147   5.99 2007-01-31 19:06:18.996577
+203      17195         261        2       542   6.99 2007-01-28 05:10:39.996577
+204      17203         267        2        91   6.99 2007-01-25 13:25:48.996577
+
+
+
+
+
+

Exercise 5

+

Retrieve all the payment IDs and their amounts from the customers whose last name is ‘DAVIS’.

+
+
paymentdavis <- dbGetQuery(con,
+    "SELECT payment_id, amount, last_name
+    FROM payment AS a
+    INNER JOIN customer AS b
+    ON a.customer_id = b.customer_id
+    WHERE last_name IN ('DAVIS')
+    ")
+
+print(paymentdavis)
+
+
  payment_id amount last_name
+1      16685   4.99     DAVIS
+2      16686   2.99     DAVIS
+3      16687   0.99     DAVIS
+
+
+
+
+

Exercise 6

+
+

Exercise 6.1

+

Use COUNT(*) to count the number of rows in rental.

+
+
rentalcount <- dbGetQuery(con,
+    "SELECT 
+    COUNT(*) AS count
+    FROM rental
+    ")
+
+print(rentalcount)
+
+
  count
+1 16044
+
+
+
+
+

Exercise 6.2

+

Use COUNT(*) and GROUP BY to count the number of rentals for each customer_id.

+
+
rentalcountcustomerid <- dbGetQuery(con,
+    "SELECT customer_id,
+    COUNT(*) AS count
+    FROM rental
+    GROUP BY customer_id
+    ")
+
+print(rentalcountcustomerid)
+
+
    customer_id count
+1             1    32
+2             2    27
+3             3    26
+4             4    22
+5             5    38
+6             6    28
+7             7    33
+8             8    24
+9             9    23
+10           10    25
+11           11    24
+12           12    28
+13           13    27
+14           14    28
+15           15    32
+16           16    28
+17           17    21
+18           18    22
+19           19    24
+20           20    30
+21           21    35
+22           22    22
+23           23    30
+24           24    25
+25           25    29
+26           26    34
+27           27    31
+28           28    32
+29           29    36
+30           30    34
+31           31    26
+32           32    29
+33           33    21
+34           34    24
+35           35    32
+36           36    27
+37           37    23
+38           38    34
+39           39    29
+40           40    27
+41           41    25
+42           42    30
+43           43    24
+44           44    26
+45           45    27
+46           46    34
+47           47    22
+48           48    17
+49           49    28
+50           50    35
+51           51    33
+52           52    29
+53           53    30
+54           54    29
+55           55    22
+56           56    30
+57           57    28
+58           58    27
+59           59    27
+60           60    25
+61           61    14
+62           62    23
+63           63    25
+64           64    33
+65           65    22
+66           66    34
+67           67    22
+68           68    22
+69           69    25
+70           70    18
+71           71    30
+72           72    30
+73           73    24
+74           74    27
+75           75    41
+76           76    23
+77           77    28
+78           78    31
+79           79    22
+80           80    30
+81           81    22
+82           82    26
+83           83    25
+84           84    33
+85           85    23
+86           86    33
+87           87    30
+88           88    21
+89           89    32
+90           90    28
+91           91    35
+92           92    28
+93           93    23
+94           94    21
+95           95    18
+96           96    27
+97           97    18
+98           98    25
+99           99    24
+100         100    24
+101         101    24
+102         102    33
+103         103    31
+104         104    24
+105         105    26
+106         106    23
+107         107    30
+108         108    30
+109         109    26
+110         110    14
+111         111    26
+112         112    30
+113         113    29
+114         114    33
+115         115    30
+116         116    27
+117         117    19
+118         118    21
+119         119    34
+120         120    32
+121         121    26
+122         122    32
+123         123    24
+124         124    18
+125         125    32
+126         126    28
+127         127    24
+128         128    32
+129         129    34
+130         130    24
+131         131    30
+132         132    28
+133         133    27
+134         134    23
+135         135    30
+136         136    15
+137         137    39
+138         138    34
+139         139    27
+140         140    18
+141         141    32
+142         142    26
+143         143    22
+144         144    42
+145         145    28
+146         146    29
+147         147    34
+148         148    46
+149         149    26
+150         150    25
+151         151    27
+152         152    21
+153         153    24
+154         154    30
+155         155    25
+156         156    25
+157         157    31
+158         158    32
+159         159    21
+160         160    27
+161         161    30
+162         162    20
+163         163    29
+164         164    16
+165         165    21
+166         166    28
+167         167    32
+168         168    34
+169         169    22
+170         170    26
+171         171    26
+172         172    33
+173         173    29
+174         174    21
+175         175    24
+176         176    37
+177         177    23
+178         178    39
+179         179    29
+180         180    23
+181         181    34
+182         182    26
+183         183    23
+184         184    23
+185         185    23
+186         186    31
+187         187    28
+188         188    25
+189         189    22
+190         190    27
+191         191    20
+192         192    23
+193         193    25
+194         194    18
+195         195    19
+196         196    35
+197         197    40
+198         198    37
+199         199    25
+200         200    27
+201         201    25
+202         202    26
+203         203    20
+204         204    35
+205         205    18
+206         206    27
+207         207    34
+208         208    26
+209         209    32
+210         210    31
+211         211    34
+212         212    20
+213         213    28
+214         214    31
+215         215    26
+216         216    23
+217         217    23
+218         218    18
+219         219    26
+220         220    27
+221         221    28
+222         222    21
+223         223    17
+224         224    22
+225         225    26
+226         226    21
+227         227    24
+228         228    27
+229         229    25
+230         230    33
+231         231    26
+232         232    25
+233         233    23
+234         234    26
+235         235    25
+236         236    42
+237         237    33
+238         238    21
+239         239    34
+240         240    26
+241         241    34
+242         242    32
+243         243    24
+244         244    32
+245         245    32
+246         246    23
+247         247    22
+248         248    15
+249         249    23
+250         250    20
+251         251    31
+252         252    22
+253         253    29
+254         254    32
+255         255    18
+256         256    30
+257         257    37
+258         258    24
+259         259    32
+260         260    31
+261         261    26
+262         262    28
+263         263    29
+264         264    25
+265         265    29
+266         266    28
+267         267    36
+268         268    29
+269         269    30
+270         270    25
+271         271    18
+272         272    20
+273         273    35
+274         274    35
+275         275    30
+276         276    28
+277         277    32
+278         278    26
+279         279    31
+280         280    27
+281         281    14
+282         282    27
+283         283    28
+284         284    28
+285         285    26
+286         286    27
+287         287    29
+288         288    24
+289         289    30
+290         290    28
+291         291    23
+292         292    27
+293         293    31
+294         294    26
+295         295    38
+296         296    30
+297         297    33
+298         298    26
+299         299    30
+300         300    31
+301         301    21
+302         302    29
+303         303    26
+304         304    26
+305         305    25
+306         306    32
+307         307    30
+308         308    25
+309         309    31
+310         310    20
+311         311    23
+312         312    26
+313         313    23
+314         314    33
+315         315    17
+316         316    29
+317         317    28
+318         318    12
+319         319    30
+320         320    20
+321         321    22
+322         322    28
+323         323    31
+324         324    25
+325         325    22
+326         326    25
+327         327    26
+328         328    23
+329         329    32
+330         330    25
+331         331    27
+332         332    28
+333         333    27
+334         334    30
+335         335    23
+336         336    30
+337         337    29
+338         338    28
+339         339    30
+340         340    31
+341         341    23
+342         342    32
+343         343    25
+344         344    18
+345         345    23
+346         346    32
+347         347    30
+348         348    36
+349         349    29
+350         350    23
+351         351    24
+352         352    23
+353         353    18
+354         354    36
+355         355    20
+356         356    30
+357         357    26
+358         358    21
+359         359    25
+360         360    34
+361         361    31
+362         362    32
+363         363    29
+364         364    23
+365         365    23
+366         366    37
+367         367    22
+368         368    35
+369         369    25
+370         370    19
+371         371    35
+372         372    34
+373         373    35
+374         374    28
+375         375    24
+376         376    28
+377         377    26
+378         378    19
+379         379    23
+380         380    36
+381         381    35
+382         382    24
+383         383    27
+384         384    25
+385         385    26
+386         386    29
+387         387    26
+388         388    30
+389         389    26
+390         390    33
+391         391    30
+392         392    23
+393         393    31
+394         394    22
+395         395    19
+396         396    27
+397         397    29
+398         398    16
+399         399    21
+400         400    28
+401         401    21
+402         402    20
+403         403    35
+404         404    30
+405         405    32
+406         406    32
+407         407    27
+408         408    30
+409         409    23
+410         410    38
+411         411    26
+412         412    21
+413         413    21
+414         414    25
+415         415    23
+416         416    31
+417         417    25
+418         418    30
+419         419    25
+420         420    21
+421         421    27
+422         422    26
+423         423    26
+424         424    30
+425         425    26
+426         426    27
+427         427    24
+428         428    20
+429         429    21
+430         430    22
+431         431    23
+432         432    23
+433         433    25
+434         434    27
+435         435    25
+436         436    30
+437         437    23
+438         438    33
+439         439    36
+440         440    22
+441         441    28
+442         442    32
+443         443    22
+444         444    30
+445         445    26
+446         446    31
+447         447    29
+448         448    29
+449         449    20
+450         450    26
+451         451    33
+452         452    32
+453         453    23
+454         454    33
+455         455    24
+456         456    25
+457         457    28
+458         458    19
+459         459    38
+460         460    25
+461         461    28
+462         462    33
+463         463    25
+464         464    16
+465         465    17
+466         466    23
+467         467    29
+468         468    39
+469         469    40
+470         470    32
+471         471    27
+472         472    30
+473         473    34
+474         474    26
+475         475    24
+476         476    22
+477         477    22
+478         478    19
+479         479    31
+480         480    22
+481         481    29
+482         482    29
+483         483    18
+484         484    30
+485         485    29
+486         486    26
+487         487    26
+488         488    22
+489         489    21
+490         490    24
+491         491    27
+492         492    16
+493         493    23
+494         494    31
+495         495    26
+496         496    21
+497         497    28
+498         498    27
+499         499    30
+500         500    28
+501         501    21
+502         502    34
+503         503    32
+504         504    28
+505         505    21
+506         506    35
+507         507    25
+508         508    25
+509         509    20
+510         510    26
+511         511    24
+512         512    26
+513         513    31
+514         514    22
+515         515    28
+516         516    26
+517         517    25
+518         518    26
+519         519    23
+520         520    32
+521         521    23
+522         522    33
+523         523    22
+524         524    19
+525         525    19
+526         526    45
+527         527    24
+528         528    25
+529         529    29
+530         530    23
+531         531    25
+532         532    32
+533         533    33
+534         534    24
+535         535    32
+536         536    25
+537         537    25
+538         538    29
+539         539    22
+540         540    24
+541         541    24
+542         542    18
+543         543    22
+544         544    22
+545         545    21
+546         546    25
+547         547    23
+548         548    19
+549         549    20
+550         550    32
+551         551    26
+552         552    21
+553         553    24
+554         554    22
+555         555    17
+556         556    21
+557         557    24
+558         558    28
+559         559    28
+560         560    30
+561         561    27
+562         562    25
+563         563    29
+564         564    24
+565         565    29
+566         566    34
+567         567    20
+568         568    21
+569         569    32
+570         570    26
+571         571    24
+572         572    25
+573         573    29
+574         574    28
+575         575    29
+576         576    34
+577         577    27
+578         578    22
+579         579    27
+580         580    27
+581         581    27
+582         582    25
+583         583    23
+584         584    30
+585         585    24
+586         586    19
+587         587    26
+588         588    29
+589         589    28
+590         590    25
+591         591    27
+592         592    29
+593         593    26
+594         594    27
+595         595    30
+596         596    28
+597         597    25
+598         598    22
+599         599    19
+
+
+
+
+

Exercise 6.3

+

Repeat the previous query and sort by the count in descending order.

+
+
rentalcountcustomerid <- dbGetQuery(con,
+    "SELECT customer_id,
+    COUNT(*) AS count
+    FROM rental
+    GROUP BY customer_id
+    ORDER BY count DESC
+    ")
+
+print(rentalcountcustomerid)
+
+
    customer_id count
+1           148    46
+2           526    45
+3           236    42
+4           144    42
+5            75    41
+6           469    40
+7           197    40
+8           468    39
+9           178    39
+10          137    39
+11          459    38
+12          410    38
+13          295    38
+14            5    38
+15          366    37
+16          257    37
+17          198    37
+18          176    37
+19          439    36
+20          380    36
+21          354    36
+22          348    36
+23          267    36
+24           29    36
+25          506    35
+26          403    35
+27          381    35
+28          373    35
+29          371    35
+30          368    35
+31          274    35
+32          273    35
+33          204    35
+34          196    35
+35           91    35
+36           50    35
+37           21    35
+38          576    34
+39          566    34
+40          502    34
+41          473    34
+42          372    34
+43          360    34
+44          241    34
+45          239    34
+46          211    34
+47          207    34
+48          181    34
+49          168    34
+50          147    34
+51          138    34
+52          129    34
+53          119    34
+54           66    34
+55           46    34
+56           38    34
+57           30    34
+58           26    34
+59          533    33
+60          522    33
+61          462    33
+62          454    33
+63          451    33
+64          438    33
+65          390    33
+66          314    33
+67          297    33
+68          237    33
+69          230    33
+70          172    33
+71          114    33
+72          102    33
+73           86    33
+74           84    33
+75           64    33
+76           51    33
+77            7    33
+78          569    32
+79          550    32
+80          535    32
+81          532    32
+82          520    32
+83          503    32
+84          470    32
+85          452    32
+86          442    32
+87          406    32
+88          405    32
+89          362    32
+90          346    32
+91          342    32
+92          329    32
+93          306    32
+94          277    32
+95          259    32
+96          254    32
+97          245    32
+98          244    32
+99          242    32
+100         209    32
+101         167    32
+102         158    32
+103         141    32
+104         128    32
+105         125    32
+106         122    32
+107         120    32
+108          89    32
+109          35    32
+110          28    32
+111          15    32
+112           1    32
+113         513    31
+114         494    31
+115         479    31
+116         446    31
+117         416    31
+118         393    31
+119         361    31
+120         340    31
+121         323    31
+122         309    31
+123         300    31
+124         293    31
+125         279    31
+126         260    31
+127         251    31
+128         214    31
+129         210    31
+130         186    31
+131         157    31
+132         103    31
+133          78    31
+134          27    31
+135         595    30
+136         584    30
+137         560    30
+138         499    30
+139         484    30
+140         472    30
+141         444    30
+142         436    30
+143         424    30
+144         418    30
+145         408    30
+146         404    30
+147         391    30
+148         388    30
+149         356    30
+150         347    30
+151         339    30
+152         336    30
+153         334    30
+154         319    30
+155         307    30
+156         299    30
+157         296    30
+158         289    30
+159         275    30
+160         269    30
+161         256    30
+162         161    30
+163         154    30
+164         135    30
+165         131    30
+166         115    30
+167         112    30
+168         108    30
+169         107    30
+170          87    30
+171          80    30
+172          72    30
+173          71    30
+174          56    30
+175          53    30
+176          42    30
+177          23    30
+178          20    30
+179         592    29
+180         588    29
+181         575    29
+182         573    29
+183         565    29
+184         563    29
+185         538    29
+186         529    29
+187         485    29
+188         482    29
+189         481    29
+190         467    29
+191         448    29
+192         447    29
+193         397    29
+194         386    29
+195         363    29
+196         349    29
+197         337    29
+198         316    29
+199         302    29
+200         287    29
+201         268    29
+202         265    29
+203         263    29
+204         253    29
+205         179    29
+206         173    29
+207         163    29
+208         146    29
+209         113    29
+210          54    29
+211          52    29
+212          39    29
+213          32    29
+214          25    29
+215         596    28
+216         589    28
+217         574    28
+218         559    28
+219         558    28
+220         515    28
+221         504    28
+222         500    28
+223         497    28
+224         461    28
+225         457    28
+226         441    28
+227         400    28
+228         376    28
+229         374    28
+230         338    28
+231         332    28
+232         322    28
+233         317    28
+234         290    28
+235         284    28
+236         283    28
+237         276    28
+238         266    28
+239         262    28
+240         221    28
+241         213    28
+242         187    28
+243         166    28
+244         145    28
+245         132    28
+246         126    28
+247          92    28
+248          90    28
+249          77    28
+250          57    28
+251          49    28
+252          16    28
+253          14    28
+254          12    28
+255           6    28
+256         594    27
+257         591    27
+258         581    27
+259         580    27
+260         579    27
+261         577    27
+262         561    27
+263         498    27
+264         491    27
+265         471    27
+266         434    27
+267         426    27
+268         421    27
+269         407    27
+270         396    27
+271         383    27
+272         333    27
+273         331    27
+274         292    27
+275         286    27
+276         282    27
+277         280    27
+278         228    27
+279         220    27
+280         206    27
+281         200    27
+282         190    27
+283         160    27
+284         151    27
+285         139    27
+286         133    27
+287         116    27
+288          96    27
+289          74    27
+290          59    27
+291          58    27
+292          45    27
+293          40    27
+294          36    27
+295          13    27
+296           2    27
+297         593    26
+298         587    26
+299         570    26
+300         551    26
+301         518    26
+302         516    26
+303         512    26
+304         510    26
+305         495    26
+306         487    26
+307         486    26
+308         474    26
+309         450    26
+310         445    26
+311         425    26
+312         423    26
+313         422    26
+314         411    26
+315         389    26
+316         387    26
+317         385    26
+318         377    26
+319         357    26
+320         327    26
+321         312    26
+322         304    26
+323         303    26
+324         298    26
+325         294    26
+326         285    26
+327         278    26
+328         261    26
+329         240    26
+330         234    26
+331         231    26
+332         225    26
+333         219    26
+334         215    26
+335         208    26
+336         202    26
+337         182    26
+338         171    26
+339         170    26
+340         149    26
+341         142    26
+342         121    26
+343         111    26
+344         109    26
+345         105    26
+346          82    26
+347          44    26
+348          31    26
+349           3    26
+350         597    25
+351         590    25
+352         582    25
+353         572    25
+354         562    25
+355         546    25
+356         537    25
+357         536    25
+358         531    25
+359         528    25
+360         517    25
+361         508    25
+362         507    25
+363         463    25
+364         460    25
+365         456    25
+366         435    25
+367         433    25
+368         419    25
+369         417    25
+370         414    25
+371         384    25
+372         369    25
+373         359    25
+374         343    25
+375         330    25
+376         326    25
+377         324    25
+378         308    25
+379         305    25
+380         270    25
+381         264    25
+382         235    25
+383         232    25
+384         229    25
+385         201    25
+386         199    25
+387         193    25
+388         188    25
+389         156    25
+390         155    25
+391         150    25
+392          98    25
+393          83    25
+394          69    25
+395          63    25
+396          60    25
+397          41    25
+398          24    25
+399          10    25
+400         585    24
+401         571    24
+402         564    24
+403         557    24
+404         553    24
+405         541    24
+406         540    24
+407         534    24
+408         527    24
+409         511    24
+410         490    24
+411         475    24
+412         455    24
+413         427    24
+414         382    24
+415         375    24
+416         351    24
+417         288    24
+418         258    24
+419         243    24
+420         227    24
+421         175    24
+422         153    24
+423         130    24
+424         127    24
+425         123    24
+426         104    24
+427         101    24
+428         100    24
+429          99    24
+430          73    24
+431          43    24
+432          34    24
+433          19    24
+434          11    24
+435           8    24
+436         583    23
+437         547    23
+438         530    23
+439         521    23
+440         519    23
+441         493    23
+442         466    23
+443         453    23
+444         437    23
+445         432    23
+446         431    23
+447         415    23
+448         409    23
+449         392    23
+450         379    23
+451         365    23
+452         364    23
+453         352    23
+454         350    23
+455         345    23
+456         341    23
+457         335    23
+458         328    23
+459         313    23
+460         311    23
+461         291    23
+462         249    23
+463         246    23
+464         233    23
+465         217    23
+466         216    23
+467         192    23
+468         185    23
+469         184    23
+470         183    23
+471         180    23
+472         177    23
+473         134    23
+474         106    23
+475          93    23
+476          85    23
+477          76    23
+478          62    23
+479          37    23
+480           9    23
+481         598    22
+482         578    22
+483         554    22
+484         544    22
+485         543    22
+486         539    22
+487         523    22
+488         514    22
+489         488    22
+490         480    22
+491         477    22
+492         476    22
+493         443    22
+494         440    22
+495         430    22
+496         394    22
+497         367    22
+498         325    22
+499         321    22
+500         252    22
+501         247    22
+502         224    22
+503         189    22
+504         169    22
+505         143    22
+506          81    22
+507          79    22
+508          68    22
+509          67    22
+510          65    22
+511          55    22
+512          47    22
+513          22    22
+514          18    22
+515           4    22
+516         568    21
+517         556    21
+518         552    21
+519         545    21
+520         505    21
+521         501    21
+522         496    21
+523         489    21
+524         429    21
+525         420    21
+526         413    21
+527         412    21
+528         401    21
+529         399    21
+530         358    21
+531         301    21
+532         238    21
+533         226    21
+534         222    21
+535         174    21
+536         165    21
+537         159    21
+538         152    21
+539         118    21
+540          94    21
+541          88    21
+542          33    21
+543          17    21
+544         567    20
+545         549    20
+546         509    20
+547         449    20
+548         428    20
+549         402    20
+550         355    20
+551         320    20
+552         310    20
+553         272    20
+554         250    20
+555         212    20
+556         203    20
+557         191    20
+558         162    20
+559         599    19
+560         586    19
+561         548    19
+562         525    19
+563         524    19
+564         478    19
+565         458    19
+566         395    19
+567         378    19
+568         370    19
+569         195    19
+570         117    19
+571         542    18
+572         483    18
+573         353    18
+574         344    18
+575         271    18
+576         255    18
+577         218    18
+578         205    18
+579         194    18
+580         140    18
+581         124    18
+582          97    18
+583          95    18
+584          70    18
+585         555    17
+586         465    17
+587         315    17
+588         223    17
+589          48    17
+590         492    16
+591         464    16
+592         398    16
+593         164    16
+594         248    15
+595         136    15
+596         281    14
+597         110    14
+598          61    14
+599         318    12
+
+
+
+
+

Exercise 6.4

+

Repeat the previous query but use HAVING to only keep the groups with 40 or more.

+
+
rentalcountcustomerid <- dbGetQuery(con,
+    "SELECT customer_id,
+    COUNT(*) AS count
+    FROM rental
+    GROUP BY customer_id
+    HAVING COUNT (*) > 40
+    ORDER BY count DESC
+    ")
+
+print(rentalcountcustomerid)
+
+
  customer_id count
+1         148    46
+2         526    45
+3         236    42
+4         144    42
+5          75    41
+
+
+
+
+
+

Exercise 7

+

Write a query that calculates a number of summary statistics for the payment table using MAX, MIN, AVG and SUM

+
+
paymentstats <- dbGetQuery(con,
+    "SELECT
+    MAX(amount) as max,
+    MIN(amount) as min,
+    AVG(amount) as avg,
+    SUM(amount) as sum
+    FROM payment
+    ")
+
+print(paymentstats)
+
+
    max  min      avg     sum
+1 11.99 0.99 4.169775 4824.43
+
+
+
+

Exercise 7.1

+

Modify the above query to do those calculations for each customer_id.

+
+
paymentcustomerstats <- dbGetQuery(con,
+    "SELECT customer_id,
+    MAX(amount) as max,
+    MIN(amount) as min,
+    AVG(amount) as avg,
+    SUM(amount) as sum
+    FROM payment
+    GROUP BY customer_id
+    ")
+
+print(paymentcustomerstats)
+
+
    customer_id   max  min      avg   sum
+1             1  2.99 0.99 1.990000  3.98
+2             2  4.99 4.99 4.990000  4.99
+3             3  2.99 1.99 2.490000  4.98
+4             5  6.99 0.99 3.323333  9.97
+5             6  4.99 0.99 2.990000  8.97
+6             7  5.99 0.99 4.190000 20.95
+7             8  6.99 6.99 6.990000  6.99
+8             9  4.99 0.99 3.656667 10.97
+9            10  4.99 4.99 4.990000  4.99
+10           11  6.99 6.99 6.990000  6.99
+11           12  4.99 4.99 4.990000  9.98
+12           14  9.99 0.99 4.190000 20.95
+13           16  3.99 0.99 2.740000 10.96
+14           17  4.99 2.99 3.656667 10.97
+15           18  4.99 2.99 4.323333 12.97
+16           19  9.99 0.99 4.490000 26.94
+17           20  6.99 1.99 3.990000 11.97
+18           21  3.99 0.99 2.990000  8.97
+19           22  8.99 4.99 6.323333 18.97
+20           23  8.99 0.99 4.323333 12.97
+21           24  6.99 2.99 4.990000  9.98
+22           25  7.99 2.99 5.490000 10.98
+23           26  2.99 2.99 2.990000  5.98
+24           27  2.99 2.99 2.990000  2.99
+25           28  2.99 2.99 2.990000  5.98
+26           29  1.99 1.99 1.990000  1.99
+27           32  4.99 4.99 4.990000 14.97
+28           33  2.99 2.99 2.990000  2.99
+29           35  6.99 3.99 5.490000 10.98
+30           36  0.99 0.99 0.990000  1.98
+31           37  2.99 0.99 1.990000  3.98
+32           40  4.99 4.99 4.990000  4.99
+33           42  5.99 5.99 5.990000  5.99
+34           43  4.99 4.99 4.990000  9.98
+35           44  4.99 0.99 2.590000 12.95
+36           45  2.99 2.99 2.990000  2.99
+37           46  4.99 2.99 3.656667 10.97
+38           47  6.99 3.99 5.323333 15.97
+39           48  4.99 0.99 2.990000  8.97
+40           49  4.99 1.99 3.490000 13.96
+41           50  4.99 4.99 4.990000 24.95
+42           51  4.99 4.99 4.990000 14.97
+43           52  0.99 0.99 0.990000  0.99
+44           53  9.99 0.99 4.490000 26.94
+45           54  4.99 3.99 4.656667 13.97
+46           55  9.99 0.99 5.323333 15.97
+47           56  6.99 2.99 4.990000 19.96
+48           57  9.99 4.99 7.490000 14.98
+49           58  7.99 0.99 3.323333  9.97
+50           59  5.99 2.99 4.656667 13.97
+51           60  4.99 1.99 3.323333  9.97
+52           61  0.99 0.99 0.990000  0.99
+53           62  4.99 0.99 2.990000  5.98
+54           64  4.99 0.99 2.990000  8.97
+55           65  4.99 0.99 2.990000  5.98
+56           66  4.99 4.99 4.990000  4.99
+57           67  9.99 2.99 6.490000 12.98
+58           69  4.99 1.99 3.490000  6.98
+59           70  4.99 4.99 4.990000  4.99
+60           71  9.99 2.99 6.490000 12.98
+61           72  4.99 0.99 3.656667 10.97
+62           73  4.99 2.99 3.990000  7.98
+63           74  6.99 6.99 6.990000  6.99
+64           75  4.99 0.99 2.990000  5.98
+65           76  1.99 0.99 1.490000  2.98
+66           77  5.99 0.99 2.990000 14.95
+67           79  4.99 2.99 3.656667 10.97
+68           81  0.99 0.99 0.990000  0.99
+69           82  8.99 2.99 5.990000 11.98
+70           83  2.99 0.99 1.656667  4.97
+71           84  6.99 0.99 4.323333 12.97
+72           85  9.99 4.99 7.490000 14.98
+73           86  1.99 1.99 1.990000  1.99
+74           87  4.99 2.99 3.990000  7.98
+75           88  2.99 2.99 2.990000  2.99
+76           89  5.99 0.99 3.323333  9.97
+77           91  5.99 5.99 5.990000  5.99
+78           92  5.99 4.99 5.490000 10.98
+79           93  6.99 2.99 4.990000 14.97
+80           94  4.99 2.99 3.990000  7.98
+81           95  4.99 4.99 4.990000  4.99
+82           98  3.99 3.99 3.990000  3.99
+83           99  0.99 0.99 0.990000  0.99
+84          100  0.99 0.99 0.990000  0.99
+85          101  9.99 9.99 9.990000  9.99
+86          102  4.99 0.99 2.656667  7.97
+87          103  9.99 7.99 8.990000 17.98
+88          104 10.99 3.99 7.490000 14.98
+89          105  8.99 2.99 6.740000 26.96
+90          106  3.99 0.99 2.490000  4.98
+91          107  5.99 5.99 5.990000 11.98
+92          108  4.99 0.99 2.990000  5.98
+93          109  7.99 0.99 3.990000 27.93
+94          110  7.99 1.99 4.990000  9.98
+95          111  2.99 2.99 2.990000  2.99
+96          112  2.99 0.99 1.990000  3.98
+97          113  0.99 0.99 0.990000  1.98
+98          114  4.99 2.99 4.323333 12.97
+99          115  2.99 0.99 1.656667  4.97
+100         116  4.99 4.99 4.990000  4.99
+101         117  0.99 0.99 0.990000  1.98
+102         118  5.99 5.99 5.990000  5.99
+103         119  6.99 0.99 4.656667 13.97
+104         120  7.99 0.99 4.490000  8.98
+105         121  4.99 4.99 4.990000  4.99
+106         122  4.99 0.99 2.990000  5.98
+107         123  2.99 2.99 2.990000  2.99
+108         124  5.99 0.99 3.990000 15.96
+109         125  3.99 3.99 3.990000  3.99
+110         126  4.99 4.99 4.990000 14.97
+111         127  0.99 0.99 0.990000  1.98
+112         128  5.99 2.99 4.490000  8.98
+113         130  2.99 2.99 2.990000  5.98
+114         131  7.99 2.99 5.323333 15.97
+115         133  6.99 2.99 4.990000  9.98
+116         134  6.99 0.99 4.240000 16.96
+117         135  5.99 3.99 4.990000  9.98
+118         136  2.99 2.99 2.990000  2.99
+119         137  2.99 2.99 2.990000  2.99
+120         138  2.99 0.99 1.990000  3.98
+121         141  2.99 2.99 2.990000  2.99
+122         142  9.99 0.99 6.656667 19.97
+123         143  2.99 2.99 2.990000  5.98
+124         144  2.99 2.99 2.990000  5.98
+125         145  0.99 0.99 0.990000  0.99
+126         146  7.99 4.99 6.490000 12.98
+127         147  0.99 0.99 0.990000  1.98
+128         148  4.99 4.99 4.990000  4.99
+129         149  4.99 4.99 4.990000  4.99
+130         150  3.99 2.99 3.656667 10.97
+131         151  5.99 4.99 5.490000 10.98
+132         152  4.99 4.99 4.990000  9.98
+133         154  7.99 5.99 6.656667 19.97
+134         155  2.99 2.99 2.990000  2.99
+135         156  6.99 4.99 5.990000 11.98
+136         157  4.99 0.99 2.990000  5.98
+137         158  5.99 4.99 5.490000 10.98
+138         159  3.99 0.99 2.490000  9.96
+139         161  5.99 0.99 2.990000 17.94
+140         162  4.99 1.99 3.990000 11.97
+141         164  1.99 1.99 1.990000  1.99
+142         165  4.99 4.99 4.990000  4.99
+143         166  1.99 1.99 1.990000  1.99
+144         167  4.99 2.99 3.656667 10.97
+145         168  4.99 0.99 2.990000  5.98
+146         169  4.99 3.99 4.490000  8.98
+147         170  5.99 0.99 3.323333  9.97
+148         171  9.99 9.99 9.990000  9.99
+149         172  6.99 0.99 3.990000 11.97
+150         173  4.99 2.99 3.990000  7.98
+151         174  5.99 4.99 5.490000 10.98
+152         176  7.99 0.99 4.390000 21.95
+153         179  6.99 0.99 4.323333 12.97
+154         180  2.99 2.99 2.990000  2.99
+155         181  6.99 6.99 6.990000  6.99
+156         182  3.99 0.99 2.490000  4.98
+157         183  0.99 0.99 0.990000  0.99
+158         184  4.99 1.99 3.323333  9.97
+159         185  2.99 0.99 1.656667  4.97
+160         186  1.99 0.99 1.490000  2.98
+161         187  7.99 7.99 7.990000  7.99
+162         189  5.99 5.99 5.990000  5.99
+163         190  4.99 2.99 3.990000  7.98
+164         191  4.99 2.99 3.990000  7.98
+165         192  1.99 1.99 1.990000  1.99
+166         193  2.99 0.99 1.990000  3.98
+167         194  7.99 4.99 6.490000 12.98
+168         196 11.99 1.99 5.740000 22.96
+169         197  3.99 0.99 2.615000 20.92
+170         198  4.99 0.99 3.390000 16.95
+171         199  7.99 7.99 7.990000  7.99
+172         200  9.99 9.99 9.990000  9.99
+173         201  6.99 3.99 5.656667 16.97
+174         203  0.99 0.99 0.990000  0.99
+175         204  4.99 0.99 3.240000 12.96
+176         207  6.99 0.99 2.990000 17.94
+177         208  4.99 4.99 4.990000  4.99
+178         209  9.99 0.99 4.656667 13.97
+179         210  2.99 2.99 2.990000  2.99
+180         211  4.99 4.99 4.990000  4.99
+181         213  0.99 0.99 0.990000  0.99
+182         214  3.99 0.99 2.590000 12.95
+183         215  4.99 4.99 4.990000  9.98
+184         216  4.99 4.99 4.990000  4.99
+185         217  8.99 2.99 5.990000 11.98
+186         219  0.99 0.99 0.990000  0.99
+187         220  3.99 0.99 2.490000  4.98
+188         221  4.99 4.99 4.990000  4.99
+189         222  6.99 0.99 3.990000 19.95
+190         223  2.99 2.99 2.990000  2.99
+191         225  4.99 3.99 4.490000  8.98
+192         227  4.99 3.99 4.490000  8.98
+193         228  4.99 0.99 2.990000  5.98
+194         230  4.99 0.99 2.990000  5.98
+195         231  8.99 5.99 7.323333 21.97
+196         232  4.99 3.99 4.490000  8.98
+197         234  4.99 4.99 4.990000  4.99
+198         235  2.99 0.99 1.990000  3.98
+199         236  2.99 2.99 2.990000  8.97
+200         237  4.99 0.99 2.990000  5.98
+201         238  4.99 2.99 3.990000  7.98
+202         239  7.99 2.99 5.656667 33.94
+203         240  4.99 2.99 3.656667 10.97
+204         241  7.99 3.99 5.990000 11.98
+205         242  4.99 2.99 3.990000 11.97
+206         243  4.99 4.99 4.990000  4.99
+207         244  4.99 1.99 3.490000  6.98
+208         245  8.99 0.99 4.823333 28.94
+209         246  8.99 3.99 6.190000 30.95
+210         247  6.99 3.99 5.323333 15.97
+211         248  7.99 4.99 6.490000 12.98
+212         249  6.99 2.99 4.740000 18.96
+213         250  5.99 0.99 3.790000 18.95
+214         251  4.99 1.99 3.323333 19.94
+215         252  4.99 0.99 2.990000  5.98
+216         253  6.99 0.99 3.656667 10.97
+217         254  5.99 2.99 4.490000  8.98
+218         256  4.99 0.99 2.990000 14.95
+219         257  2.99 2.99 2.990000  8.97
+220         259  6.99 2.99 5.323333 15.97
+221         260  8.99 8.99 8.990000  8.99
+222         261  6.99 0.99 4.240000 16.96
+223         262  4.99 4.99 4.990000  4.99
+224         263  4.99 0.99 2.990000  5.98
+225         265  0.99 0.99 0.990000  0.99
+226         266  2.99 1.99 2.490000  4.98
+227         267  6.99 4.99 5.656667 16.97
+228         269  6.99 0.99 3.156667 18.94
+229         270  4.99 1.99 3.490000  6.98
+230         271  8.99 8.99 8.990000  8.99
+231         272  6.99 0.99 3.990000 15.96
+232         273  3.99 0.99 2.490000  4.98
+233         274  5.99 2.99 4.156667 24.94
+234         275  2.99 2.99 2.990000  2.99
+235         276 10.99 3.99 7.490000 14.98
+236         277  6.99 6.99 6.990000  6.99
+237         278  4.99 4.99 4.990000  4.99
+238         279  2.99 0.99 1.990000  3.98
+239         280  4.99 4.99 4.990000  4.99
+240         281  2.99 2.99 2.990000  5.98
+241         282  6.99 0.99 3.323333  9.97
+242         284  6.99 0.99 2.990000  8.97
+243         286  6.99 6.99 6.990000  6.99
+244         287  2.99 0.99 2.323333  6.97
+245         288  6.99 3.99 5.490000 21.96
+246         290  2.99 2.99 2.990000  2.99
+247         291  4.99 2.99 4.323333 12.97
+248         292  0.99 0.99 0.990000  0.99
+249         293  8.99 0.99 4.990000 14.97
+250         294  1.99 1.99 1.990000  1.99
+251         295  3.99 3.99 3.990000  3.99
+252         296  5.99 2.99 4.740000 18.96
+253         297  3.99 0.99 2.490000  4.98
+254         298  3.99 3.99 3.990000  3.99
+255         299  8.99 5.99 7.490000 14.98
+256         300  4.99 0.99 3.323333  9.97
+257         301  5.99 0.99 3.990000 11.97
+258         302  5.99 4.99 5.490000 10.98
+259         303  4.99 0.99 2.990000  8.97
+260         304 10.99 0.99 4.990000 14.97
+261         305  2.99 2.99 2.990000  2.99
+262         306  6.99 3.99 5.490000 10.98
+263         307  6.99 1.99 4.740000 18.96
+264         308  3.99 3.99 3.990000  3.99
+265         309  6.99 0.99 3.990000  7.98
+266         310  0.99 0.99 0.990000  0.99
+267         311  6.99 2.99 5.390000 26.95
+268         312  6.99 0.99 4.490000 17.96
+269         313  4.99 0.99 2.490000  9.96
+270         314  5.99 4.99 5.490000 10.98
+271         315  8.99 4.99 6.990000 13.98
+272         316  8.99 1.99 5.323333 15.97
+273         317  6.99 6.99 6.990000  6.99
+274         318  9.99 9.99 9.990000  9.99
+275         319  9.99 2.99 5.656667 16.97
+276         321  5.99 4.99 5.323333 15.97
+277         322  4.99 0.99 2.990000  5.98
+278         323  5.99 2.99 4.656667 13.97
+279         324  3.99 3.99 3.990000  3.99
+280         325  5.99 5.99 5.990000  5.99
+281         326  6.99 3.99 5.323333 15.97
+282         327  6.99 6.99 6.990000  6.99
+283         328  2.99 2.99 2.990000  2.99
+284         330  7.99 3.99 5.990000 11.98
+285         331  2.99 0.99 1.990000  3.98
+286         332  6.99 3.99 5.490000 10.98
+287         333  4.99 4.99 4.990000  4.99
+288         334  8.99 6.99 7.990000 15.98
+289         337  8.99 4.99 6.990000 20.97
+290         338  0.99 0.99 0.990000  0.99
+291         339  5.99 5.99 5.990000  5.99
+292         343  3.99 3.99 3.990000  7.98
+293         344  5.99 2.99 4.490000  8.98
+294         345  0.99 0.99 0.990000  1.98
+295         346  4.99 4.99 4.990000  9.98
+296         348  0.99 0.99 0.990000  1.98
+297         349  4.99 4.99 4.990000  4.99
+298         350  4.99 4.99 4.990000  9.98
+299         351  1.99 1.99 1.990000  1.99
+300         352  2.99 2.99 2.990000  2.99
+301         353  6.99 6.99 6.990000  6.99
+302         354  1.99 0.99 1.323333  3.97
+303         355  3.99 3.99 3.990000  3.99
+304         356  4.99 4.99 4.990000  4.99
+305         357  4.99 0.99 2.990000  8.97
+306         358  4.99 4.99 4.990000  4.99
+307         359  8.99 2.99 5.323333 15.97
+308         360  4.99 0.99 2.990000  5.98
+309         361  5.99 4.99 5.490000 10.98
+310         362  4.99 4.99 4.990000  4.99
+311         363  3.99 3.99 3.990000  3.99
+312         364  5.99 5.99 5.990000  5.99
+313         365  5.99 4.99 5.490000 10.98
+314         366  6.99 6.99 6.990000  6.99
+315         367  2.99 0.99 1.990000  3.98
+316         368  5.99 2.99 4.490000 17.96
+317         369  7.99 0.99 4.740000 18.96
+318         371  6.99 0.99 4.323333 25.94
+319         372  2.99 2.99 2.990000  5.98
+320         373  4.99 4.99 4.990000  4.99
+321         374  2.99 0.99 1.656667  4.97
+322         375  8.99 2.99 5.490000 21.96
+323         376  0.99 0.99 0.990000  0.99
+324         378  0.99 0.99 0.990000  0.99
+325         379  4.99 4.99 4.990000  9.98
+326         380  3.99 3.99 3.990000  3.99
+327         381  2.99 0.99 2.323333  6.97
+328         382  2.99 2.99 2.990000  5.98
+329         383  8.99 0.99 4.990000  9.98
+330         384  4.99 0.99 2.990000 11.96
+331         385  4.99 2.99 3.990000  7.98
+332         386  7.99 7.99 7.990000  7.99
+333         387  7.99 3.99 5.490000 21.96
+334         388  4.99 4.99 4.990000  9.98
+335         389  4.99 4.99 4.990000  4.99
+336         390  4.99 4.99 4.990000  9.98
+337         391  7.99 2.99 4.990000 24.95
+338         393  4.99 0.99 2.990000  5.98
+339         394  3.99 2.99 3.490000  6.98
+340         396  5.99 5.99 5.990000  5.99
+341         397  0.99 0.99 0.990000  0.99
+342         398  4.99 4.99 4.990000  4.99
+343         399  6.99 4.99 5.990000 17.97
+344         400  6.99 1.99 4.740000 18.96
+345         401  4.99 1.99 3.990000 11.97
+346         402  1.99 1.99 1.990000  1.99
+347         403  2.99 0.99 1.990000  3.98
+348         404  5.99 5.99 5.990000  5.99
+349         405  4.99 2.99 3.990000  7.98
+350         406  0.99 0.99 0.990000  0.99
+351         407  7.99 7.99 7.990000  7.99
+352         408  5.99 2.99 4.323333 12.97
+353         409  6.99 6.99 6.990000  6.99
+354         411  4.99 1.99 3.490000  6.98
+355         412  4.99 0.99 2.740000 10.96
+356         413  4.99 4.99 4.990000  9.98
+357         414  4.99 3.99 4.490000  8.98
+358         415  4.99 4.99 4.990000  4.99
+359         416  3.99 0.99 2.656667  7.97
+360         417  8.99 4.99 6.323333 18.97
+361         419  2.99 2.99 2.990000  2.99
+362         420  4.99 4.99 4.990000  4.99
+363         421  0.99 0.99 0.990000  1.98
+364         422  0.99 0.99 0.990000  0.99
+365         424  0.99 0.99 0.990000  0.99
+366         425  5.99 5.99 5.990000  5.99
+367         426  0.99 0.99 0.990000  0.99
+368         427  6.99 6.99 6.990000  6.99
+369         428  4.99 4.99 4.990000  4.99
+370         429  7.99 2.99 5.390000 26.95
+371         430  4.99 2.99 3.990000  7.98
+372         431  2.99 2.99 2.990000  2.99
+373         432  8.99 5.99 7.656667 22.97
+374         433 10.99 8.99 9.990000 19.98
+375         434  5.99 5.99 5.990000  5.99
+376         435  7.99 4.99 6.490000 12.98
+377         436  7.99 3.99 5.990000 17.97
+378         437  5.99 2.99 4.656667 13.97
+379         438  6.99 0.99 4.323333 12.97
+380         439  9.99 0.99 4.656667 13.97
+381         440  4.99 4.99 4.990000  4.99
+382         441  4.99 4.99 4.990000  4.99
+383         442  6.99 0.99 4.656667 13.97
+384         443  4.99 4.99 4.990000  4.99
+385         444  8.99 0.99 4.990000  9.98
+386         445  2.99 2.99 2.990000  5.98
+387         446  4.99 0.99 2.323333  6.97
+388         447  2.99 0.99 1.990000  3.98
+389         448  4.99 2.99 3.990000  7.98
+390         449  7.99 4.99 6.323333 18.97
+391         450  3.99 3.99 3.990000  3.99
+392         451  2.99 0.99 2.323333  6.97
+393         452  2.99 1.99 2.656667  7.97
+394         454  7.99 7.99 7.990000  7.99
+395         455  0.99 0.99 0.990000  1.98
+396         456  4.99 4.99 4.990000  4.99
+397         457  7.99 7.99 7.990000  7.99
+398         459  2.99 2.99 2.990000  2.99
+399         460  4.99 0.99 2.990000 11.96
+400         461  6.99 6.99 6.990000  6.99
+401         462  3.99 2.99 3.490000  6.98
+402         463  1.99 1.99 1.990000  1.99
+403         464  3.99 1.99 2.990000  5.98
+404         465  0.99 0.99 0.990000  0.99
+405         466  2.99 2.99 2.990000  2.99
+406         467  4.99 4.99 4.990000  4.99
+407         468  6.99 0.99 4.190000 20.95
+408         469  7.99 0.99 3.790000 18.95
+409         470  2.99 2.99 2.990000  2.99
+410         471  2.99 2.99 2.990000  2.99
+411         472  4.99 0.99 2.490000  9.96
+412         473  4.99 2.99 3.990000 11.97
+413         474  7.99 7.99 7.990000  7.99
+414         475  4.99 0.99 2.990000  5.98
+415         476  4.99 2.99 3.990000  7.98
+416         477  2.99 2.99 2.990000  2.99
+417         479  7.99 3.99 5.990000 11.98
+418         480  9.99 0.99 5.990000 17.97
+419         481  5.99 5.99 5.990000  5.99
+420         482  8.99 0.99 4.323333 12.97
+421         483  6.99 6.99 6.990000  6.99
+422         484  4.99 2.99 3.656667 10.97
+423         485  2.99 2.99 2.990000  2.99
+424         486  8.99 0.99 4.323333 12.97
+425         489  4.99 2.99 3.990000  7.98
+426         490  6.99 4.99 5.990000 11.98
+427         491  2.99 0.99 1.990000  3.98
+428         492  2.99 2.99 2.990000  2.99
+429         493  7.99 7.99 7.990000  7.99
+430         494  4.99 4.99 4.990000  4.99
+431         495  4.99 4.99 4.990000  9.98
+432         496  4.99 0.99 2.990000  5.98
+433         497  7.99 7.99 7.990000  7.99
+434         498  8.99 2.99 4.990000 14.97
+435         499  2.99 2.99 2.990000  2.99
+436         500  8.99 0.99 6.323333 18.97
+437         501  1.99 0.99 1.490000  2.98
+438         502  2.99 0.99 1.990000  7.96
+439         503  5.99 1.99 3.990000 15.96
+440         504  5.99 4.99 5.323333 15.97
+441         505  2.99 2.99 2.990000  5.98
+442         506  8.99 0.99 4.132857 28.93
+443         507  4.99 0.99 2.990000  5.98
+444         508  2.99 2.99 2.990000  5.98
+445         509  8.99 4.99 6.990000 13.98
+446         510  8.99 4.99 6.656667 19.97
+447         511  3.99 2.99 3.490000  6.98
+448         513  4.99 4.99 4.990000  4.99
+449         514  4.99 4.99 4.990000  4.99
+450         515  8.99 6.99 7.990000 15.98
+451         516  3.99 1.99 2.990000  5.98
+452         517  4.99 4.99 4.990000  4.99
+453         518  2.99 2.99 2.990000  2.99
+454         519  3.99 3.99 3.990000  3.99
+455         520  6.99 6.99 6.990000  6.99
+456         522  5.99 5.99 5.990000  5.99
+457         523  4.99 0.99 2.990000  5.98
+458         524  4.99 0.99 2.990000  5.98
+459         525  5.99 5.99 5.990000  5.99
+460         526  4.99 2.99 4.323333 12.97
+461         528  5.99 0.99 3.590000 17.95
+462         529  2.99 2.99 2.990000  2.99
+463         530  0.99 0.99 0.990000  0.99
+464         531  4.99 2.99 3.990000  7.98
+465         532  2.99 2.99 2.990000  2.99
+466         533  5.99 0.99 2.990000  8.97
+467         534  5.99 0.99 3.490000  6.98
+468         535  4.99 0.99 3.240000 12.96
+469         536  6.99 0.99 3.990000  7.98
+470         537  4.99 4.99 4.990000  4.99
+471         538  4.99 2.99 3.990000  7.98
+472         539  4.99 0.99 2.990000  5.98
+473         541  7.99 4.99 6.490000 12.98
+474         542  4.99 4.99 4.990000  9.98
+475         543  6.99 1.99 4.490000  8.98
+476         544  2.99 2.99 2.990000  5.98
+477         545  3.99 0.99 2.490000  4.98
+478         546  6.99 5.99 6.490000 12.98
+479         547  8.99 0.99 3.990000 11.97
+480         548  6.99 3.99 5.323333 15.97
+481         549  4.99 0.99 3.740000 14.96
+482         550  7.99 7.99 7.990000  7.99
+483         551  7.99 0.99 4.190000 20.95
+484         552  0.99 0.99 0.990000  0.99
+485         553  4.99 4.99 4.990000  4.99
+486         554  2.99 2.99 2.990000  5.98
+487         556  5.99 0.99 3.656667 10.97
+488         557  4.99 4.99 4.990000  9.98
+489         560  2.99 2.99 2.990000  2.99
+490         561  4.99 4.99 4.990000  9.98
+491         562  5.99 2.99 3.990000 11.97
+492         563  5.99 4.99 5.490000 10.98
+493         564  5.99 2.99 4.490000  8.98
+494         565  6.99 0.99 3.990000  7.98
+495         566  5.99 4.99 5.490000 10.98
+496         569  4.99 1.99 4.190000 20.95
+497         570  7.99 7.99 7.990000  7.99
+498         571  9.99 3.99 6.990000 13.98
+499         572  7.99 7.99 7.990000  7.99
+500         573  2.99 2.99 2.990000  2.99
+501         574  0.99 0.99 0.990000  0.99
+502         575  4.99 0.99 3.390000 16.95
+503         576  2.99 0.99 1.990000  3.98
+504         577  5.99 0.99 3.490000  6.98
+505         578  0.99 0.99 0.990000  0.99
+506         580  0.99 0.99 0.990000  0.99
+507         581  4.99 4.99 4.990000  9.98
+508         582  0.99 0.99 0.990000  0.99
+509         584  4.99 4.99 4.990000 14.97
+510         586  8.99 4.99 6.990000 13.98
+511         587  4.99 0.99 2.990000  5.98
+512         588  4.99 2.99 3.990000  7.98
+513         589  4.99 0.99 3.656667 10.97
+514         590  3.99 3.99 3.990000  3.99
+515         593  8.99 2.99 5.990000 11.98
+516         594  8.99 0.99 5.490000 21.96
+517         595  6.99 6.99 6.990000  6.99
+518         596  6.99 0.99 3.823333 22.94
+519         597  8.99 2.99 5.990000 11.98
+520         599  4.99 4.99 4.990000  4.99
+
+
+
+
+

Exercise 7.2

+

Modify the above query to only keep the customer_ids that have more then 5 payments.

+
+
paymentcustomerstats5 <- dbGetQuery(con,
+    "SELECT customer_id,
+    MAX(amount) as max,
+    MIN(amount) as min,
+    AVG(amount) as avg,
+    SUM(amount) as sum
+    FROM payment
+    GROUP BY customer_id
+    HAVING COUNT(customer_id) > 5
+    ")
+
+print(paymentcustomerstats5)
+
+
   customer_id  max  min      avg   sum
+1           19 9.99 0.99 4.490000 26.94
+2           53 9.99 0.99 4.490000 26.94
+3          109 7.99 0.99 3.990000 27.93
+4          161 5.99 0.99 2.990000 17.94
+5          197 3.99 0.99 2.615000 20.92
+6          207 6.99 0.99 2.990000 17.94
+7          239 7.99 2.99 5.656667 33.94
+8          245 8.99 0.99 4.823333 28.94
+9          251 4.99 1.99 3.323333 19.94
+10         269 6.99 0.99 3.156667 18.94
+11         274 5.99 2.99 4.156667 24.94
+12         371 6.99 0.99 4.323333 25.94
+13         506 8.99 0.99 4.132857 28.93
+14         596 6.99 0.99 3.823333 22.94
+
+
+
+
+
+

Cleanup

+

Run the following chunk to disconnect from the connection.

+
+
#clean up
+dbDisconnect(con)
+
+
+ +
+ + +
+ + + + + \ No newline at end of file