|
| 1 | +### Set-Up ### |
| 2 | + |
| 3 | +# install.packages("RSQLite") |
| 4 | + |
| 5 | +library(RSQLite) |
| 6 | + |
| 7 | +# these are notes |
| 8 | +# SQL == Structured Query Language |
| 9 | +# SQL is the most used language worldwide for relational databases |
| 10 | + |
| 11 | + |
| 12 | +# data tables we are using are: mtcars; iris |
| 13 | + |
| 14 | +mtcars$car_names <- rownames(mtcars) # string operator to adds a new column |
| 15 | +iris |
| 16 | + |
| 17 | +### Create a database ### |
| 18 | + |
| 19 | +db <- dbConnect(RSQLite::SQLite(), "") |
| 20 | + |
| 21 | +summary(db) # base R - what is "db"? |
| 22 | + |
| 23 | +# add data tables in |
| 24 | +dbWriteTable(db, "mtcars", mtcars) # database connection, name of the table |
| 25 | +# and the actual dataframe mtcars from R |
| 26 | +dbWriteTable(db, "iris", iris) |
| 27 | + |
| 28 | +dbListTables(db) # two tables in my database |
| 29 | + |
| 30 | +### Write SQL Queries ### |
| 31 | + |
| 32 | +# star * wildcard == select all |
| 33 | +dbGetQuery(db, "SELECT * FROM mtcars") |
| 34 | + |
| 35 | +# select certain columns |
| 36 | +dbGetQuery(db, "SELECT car_names, mpg, qsec FROM mtcars") |
| 37 | + |
| 38 | + |
| 39 | +# filter certain rows |
| 40 | +# mpg is between 10-15 |
| 41 | +dbGetQuery(db, "SELECT car_names, mpg, qsec FROM mtcars |
| 42 | + WHERE mpg BETWEEN 10 AND 15") |
| 43 | + |
| 44 | +dbGetQuery(db, "SELECT * from mtcars WHERE qsec > 15") |
| 45 | + |
| 46 | +### Create a new data table ### |
| 47 | + |
| 48 | +# make a new table in R (not SQL sorry) |
| 49 | +car <- c("Camaro", "Mustang", "Explorer", "California") # column 1 |
| 50 | +make <- c("Chevrolet", "Ferrari", "Ford", "Dodges") # column 2 |
| 51 | +df1 <- tibble::tibble(car, make) |
| 52 | + |
| 53 | +# write a table by appending the data tables inside our db |
| 54 | +dbWriteTable(db, "car_makes", df1) |
| 55 | + |
| 56 | +dbListTables(db) |
| 57 | + |
| 58 | + |
| 59 | +### Make more queries! ### |
| 60 | + |
| 61 | +dbGetQuery(db, "SELECT * FROM car_makes") |
| 62 | + |
| 63 | +dbGetQuery(db, "SELECT * FROM mtcars LIMIT 5") |
| 64 | +# this gives top rows to a limit |
| 65 | +# there is no way to get bottom |
| 66 | +# but we can use GROUP BY and ORDER BY |
| 67 | + |
| 68 | +# Now, let's get into some logic |
| 69 | +# mtcars: get car names and horsepower |
| 70 | +# but car names start with M and 6 or 8 cylinders |
| 71 | +dbGetQuery(db, "SELECT car_names, hp, cyl FROM mtcars |
| 72 | + WHERE car_names LIKE 'M%' |
| 73 | + AND cyl IN (6, 8)") |
| 74 | + |
| 75 | +# mtcars: select all the columns but not MPG between 10 and 15 |
| 76 | +dbGetQuery(db, "SELECT * FROM mtcars WHERE mpg NOT BETWEEN 10 AND 15") |
| 77 | + |
| 78 | +# what if you want car names starting with M or disp bigger than 170? |
| 79 | +# less strict: car names that don't start with M as long as disp is |
| 80 | +# still bigger than 170, or vice versa |
| 81 | +dbGetQuery(db, "SELECT car_names, disp FROM mtcars WHERE |
| 82 | + disp > 170 |
| 83 | + OR car_names LIKE 'M%' ") |
| 84 | + |
| 85 | +# make sure to group correctly! use brackets to group conditions |
| 86 | +# select all the columns and where mgp is not between 10 and 15 |
| 87 | +# and (car name start with M or disp is bigger than 170) |
| 88 | +dbGetQuery(db, "SELECT * FROM mtcars |
| 89 | + WHERE mpg NOT BETWEEN 10 AND 15 |
| 90 | + AND (car_names LIKE 'M%' OR disp > 170) |
| 91 | + ") |
| 92 | + |
| 93 | +# select car_names, hp, cyl where car names do not start with T |
| 94 | +# and mpg is not between 11 and 16 |
| 95 | +dbGetQuery(db, "SELECT car_names, hp, cyl FROM mtcars WHERE |
| 96 | + car_names NOT LIKE 'T%' AND |
| 97 | + mpg NOT BETWEEN 11 AND 16") |
| 98 | + |
| 99 | + |
| 100 | +### Appending our data table ### |
| 101 | + |
| 102 | +car <- c("Corolla", "Lancer", "Sportage", "XE") |
| 103 | +make <- c("Toyota Corolla", "Mitsubishi", "Kia", "Jaguar") |
| 104 | +df2 <- tibble::tibble(car, make) |
| 105 | + |
| 106 | +# add |
| 107 | +dbWriteTable(db, "car_makes", df2, append = TRUE) |
| 108 | +dbGetQuery(db, "SELECT * FROM car_makes") |
| 109 | + |
| 110 | +# or overwrite |
| 111 | +dbWriteTable(db, "car_makes", df2, overwrite = TRUE) |
| 112 | +dbGetQuery(db, "SELECT * FROM car_makes") |
| 113 | + |
| 114 | + |
| 115 | +### Calculations ### |
| 116 | + |
| 117 | +# sort in asc or desc order |
| 118 | +dbGetQuery(db, "SELECT cyl, hp FROM mtcars |
| 119 | + ORDER BY hp") |
| 120 | + |
| 121 | +dbGetQuery(db, "SELECT cyl, hp FROM mtcars |
| 122 | + ORDER BY cyl DESC") |
| 123 | + |
| 124 | +dbGetQuery(db, "SELECT cyl, hp, mpg FROM mtcars |
| 125 | + ORDER BY mpg, hp") |
| 126 | + |
| 127 | +# get the average |
| 128 | +dbGetQuery(db, "SELECT cyl, AVG(hp) AS 'mean_horse' FROM mtcars |
| 129 | + GROUP BY cyl |
| 130 | + ORDER BY mean_horse") |
| 131 | + |
| 132 | +# select cyl, hp, mpg |
| 133 | +# average hp and average mpg |
| 134 | +# group by cylinder |
| 135 | +# order descending hp |
| 136 | +avg_hpcyl <- dbGetQuery(db, "SELECT cyl, AVG(hp) AS 'mean_horse', AVG(mpg) |
| 137 | +AS 'mean_gas' |
| 138 | +FROM mtcars |
| 139 | +GROUP BY cyl |
| 140 | +ORDER BY mean_horse DESC |
| 141 | + ") |
| 142 | + |
| 143 | +# saved as a dataframe now |
| 144 | +avg_hpcyl |
| 145 | +class(avg_hpcyl) |
| 146 | + |
| 147 | + |
| 148 | +### Joining data tables ### |
| 149 | +dbGetQuery(db, "SELECT * FROM mtcars WHERE car_names = 'Toyota Corolla' ") |
| 150 | +dbGetQuery(db, "SELECT * FROM car_makes") |
| 151 | + |
| 152 | +dbGetQuery(db, " |
| 153 | + SELECT * FROM mtcars AS t1 |
| 154 | + INNER JOIN car_makes AS t2 |
| 155 | + ON car_names = make |
| 156 | + ") |
| 157 | + |
| 158 | +### select certain columns in join |
| 159 | +dbGetQuery(db, " |
| 160 | + SELECT t1.hp, t1.cyl, t1.car_names, t2.car |
| 161 | + FROM mtcars t1 |
| 162 | + JOIN car_makes t2 |
| 163 | + ON t1.car_names = make") |
| 164 | + |
| 165 | + |
| 166 | +### Close connections ### |
| 167 | + |
| 168 | +dbDisconnect(db) |
| 169 | + |
| 170 | +dbGetQuery(db, "SELECT * FROM mtcars") |
0 commit comments