Skip to content

Commit 537158a

Browse files
committed
update live lesson on website for sqlite
1 parent fb5e246 commit 537158a

File tree

2 files changed

+196
-1
lines changed

2 files changed

+196
-1
lines changed

lessons/misc/sql-intro/lesson.Rmd

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

0 commit comments

Comments
 (0)