Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Database Created.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 60 additions & 6 deletions sql-project.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "sql-workshop"
author: "Charles Lang"
author: "Stanley Zhao"
output: html_document
---

Expand All @@ -13,10 +13,10 @@ Before you follow the directions below, please take a screenshot of your AWS con
library(DBI)
library(RMySQL)

db_user <- 'admin'
db_password <- 'testsql!'
db_user <- 'ssz2119_hudk4051'
db_password <- 'ssz2119_hudk4051'
db_name <- 'oudb'
db_host <- 'PASTE YOUR ENDPOINT HERE'
db_host <- 'database-1.cbidvpq9uehs.us-east-2.rds.amazonaws.com'
db_port <- 3306

mydb <- dbConnect(MySQL(), user = db_user, password = db_password, dbname = db_name, host = db_host, port = db_port)
Expand Down Expand Up @@ -55,6 +55,14 @@ dbReadTable(mydb, 'studentInfo')
#EXERCISE 1
#Make two toy data sets with at least three variables and at least 30 rows each in them. Have a mix of numeric and character variables. Transfer these dataframes to your SQL database using the DBI commands. Name the tables whatever you like.

library(dplyr)
D1 <- select(studentInfo,"id_student","gender","region","imd_band")
D2 <- select(studentInfo,"id_student","highest_education","age_band","disability")
dbWriteTable(mydb, "Table1", D1, overwrite = TRUE)
dbWriteTable(mydb,"Table2", D2, overwrite = TRUE)
dbReadTable(mydb, 'Table1')


```

## Getting into SQL - READING
Expand Down Expand Up @@ -83,8 +91,11 @@ dbGetQuery(mydb, "SELECT COUNT(*) FROM studentAssessment WHERE score > 50 AND id
#EXERCISE 2
#Read one of your toy data tables, make sure the output is ordered in descending order, you rename one of the variables and the output is limited to the first 20 rows.

dbGetQuery(mydb, "SELECT id_student AS 'Student ID', quantity FROM Table1 ORDER BY imd_band DESC LIMIT 20;")

#Read the other table according to a condition of one of the variables.

dbGetQuery(mydb, "SELECT * FROM Table2 WHERE disability='Y';")
```

## Getting into SQL - UPDATING
Expand Down Expand Up @@ -123,6 +134,11 @@ dbGetQuery(mydb, "SELECT * FROM studentAssessment ORDER BY id_student LIMIT 10;"
#EXERCISE 3
#Insert a new row in one of your toy data tables leaving one variable empty. Change one value in your other table. Display your new tables. Delete the row you edited and the row you inserted.

dbGetQuery(mydb, "INSERT INTO Table1 (id_student, gender, region, imd_band) VALUES ('12345', 'F', 'North Pole','0');")
#dbReadTable(mydb, 'Table1')
dbGetQuery(mydb, "UPDATE Table2 SET disability = 'N' WHERE id_student = 30268;")
#dbReadTable(mydb, 'Table2')
dbGetQuery(mydb, "DELETE FROM Table1 WHERE region = 'North Pole';")

```

Expand Down Expand Up @@ -159,17 +175,27 @@ dbGetQuery(mydb, "DROP TABLE IF EXISTS test;") #No error since it is only if it
#EXERCISE 4
#Create a table that is exactly the same as your first toy data table but this time use SQL commands. Display your new table. Then delete the original table.

dbGetQuery(mydb,"CREATE TABLE Stable1 (
id_student INTEGER,
gender TEXT,
region TEXT,
imd_band TEXT
);")

dbGetQuery(mydb,"INSERT INTO Stable1 (id_student, gender, region, imd_band) SELECT id_student, gender,region, imd_band FROM Table1;")
dbReadTable(mydb, 'Stable1')
dbGetQuery(mydb, "DROP TABLE Table1;")
```

# NULL Value
```{r}
#NULL is a state (similar to R), represents the lack of a value. But is not compatible with R backend so this code doesn't work as part of dbGetQuery()

#This doesn't work because NULL is not a value
SELECT * FROM test WHERE score = NULL;
#SELECT * FROM test WHERE score = NULL;

#Instead use
SELECT * FROM test WHERE score is NULL;
#SELECT * FROM test WHERE score is NULL;

```

Expand Down Expand Up @@ -212,6 +238,15 @@ dbGetQuery(mydb,"INSERT INTO test2 (score, student) VALUES (NULL, 'A');")
#EXERCISE 5
#Recreate one of your toy data tables with the constraint that for one of the integer variablesthe default value will be zero. Test your table by inserting some empty values. Display your new tables. Then delete your table.

dbGetQuery(mydb,"CREATE TABLE Stable2 (
id_student INTEGER,
highest_education TEXT,
age_band INTEGER,
disability TEXT
);")

dbGetQuery(mydb,"INSERT INTO Stable2 (id_student, highest_education, age_band, disability) SELECT id_student, highest_education,NULL, disability FROM Table2;")
dbReadTable(mydb, 'Table2')
```


Expand All @@ -227,6 +262,10 @@ dbGetQuery(mydb, "ALTER TABLE studentAssessment DROP COLUMN email;")

#EXERCISE 6
#Add a column to one of your toy data tables with a default value of 3. Display your new table. Delete this column.

dbGetQuery(mydb, "ALTER TABLE Table2 ADD email INTEGER DEFAULT 3 ")
dbReadTable(mydb, 'Table2')
dbGetQuery(mydb, "ALTER TABLE Table2 DROP COLUMN email;")
```


Expand All @@ -248,6 +287,16 @@ dbGetQuery(mydb, "DROP TABLE IF EXISTS test3;")
#EXERCISE 7
#Create a new table with four variables and a primary key that is a sequential id value.

dbGetQuery(mydb,"CREATE TABLE Table3 (
id INTEGER AUTO_INCREMENT PRIMARY KEY, #Not standard syntax
id_student INTEGER,
gender TEXT,
region TEXT,
imd_band TEXT
);")

dbGetQuery(mydb,"INSERT INTO Table3 (id_student, gender, region, imd_band) SELECT id_student, gender,region, imd_band FROM Stable1;")
dbReadTable(mydb, 'Table3')
```

## Filtering (WHERE)
Expand Down Expand Up @@ -278,6 +327,7 @@ dbGetQuery(mydb, "SELECT id_student, gender, region FROM studentInfo WHERE regio
#EXERCISE 8
#Query one of your original toy data tables, for two different conditions.

dbGetQuery(mydb, "SELECT highest_education, disability FROM Table2 WHERE highest_education = 'No Formal quals' AND disability = 'Y';")
```

## Removing Duplicates
Expand All @@ -289,6 +339,7 @@ dbGetQuery(mydb, "SELECT DISTINCT region, gender FROM studentInfo;")
#EXERCISE 9
#Insert a duplicate row into one of your toy data tables. Then query the table without including duplicates.

dbGetQuery(mydb, "INSERT INTO Table2 (id_student, highest_education, age_band, disability) VALUES ('2716795', 'A Level or Equivalent', '0-35', 'N');")
```

## Conditional Expressions (non-standard)
Expand Down Expand Up @@ -360,6 +411,9 @@ dbGetQuery(mydb, "SELECT * FROM left_table
#EXERCISE 10
# Create a common id variable in your two toy data tables. Then join those tables so that your query returns all the values from one table and only those that match from the other.

dbGetQuery(mydb, "ALTER TABLE Table2 DROP COLUMN row_names;")
dbReadTable(mydb, 'Stable1')
dbReadTable(mydb, 'Table2')
```
```{r}
#Now disconnect from your database
Expand Down