100% found this document useful (1 vote)
23 views12 pages

SQL With R

Uploaded by

Ammar Jagadhita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
23 views12 pages

SQL With R

Uploaded by

Ammar Jagadhita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

SQL with R

Overview
• understanding how to connect to databases,
• execute SQL queries,
• manipulate and visualize data in R,
• and optimize data analysis workflows.
what is the advantage of using SQL with R?
• SQL is a programming language designed for managing and querying
data stored in relational database management systems (RDBMS).
• SQL provides a powerful and efficient means to extract valuable
insights from data.
• By integrating SQL with R, we can leverage the strengths of both
languages
• SQL helps us efficiently query and manipulate data stored in
databases, while R allows us to perform advanced statistical analysis,
visualization, and modeling on that data.
Introduction to SQL in R
• R provides several packages (e.g., DBI, RSQLite, RODBC, odbc) that
allow you to connect to different types of databases (e.g., SQLite,
MySQL, PostgreSQL, SQL Server).
• You can establish a connection using the dbConnect() function,
specifying the database type and connection details.

library(DBI)
con <- dbConnect(RSQLite::SQLite(), "mydatabase.db")
Libraries
• DBI: The DBI (Database Interface) library in R provides a common
interface for communicating with relational databases. It allows you
to connect to various database management systems (DBMS) such as
SQLite, MySQL, PostgreSQL, and SQL Server, among others, using a
consistent set of functions and methods
• Connecting to a Database:
• library(DBI)
• con <- dbConnect(RSQLite::SQLite(), "mydatabase.db")
Libraries cont.
• Executing SQL Queries:
• result <- dbGetQuery(con, "SELECT * FROM mytable")
• Fetching Data:
• data <- dbFetch(result)
Libraries cont.
• RSQLite: SQLite is a popular, lightweight, and self-contained SQL
database engine that requires minimal configuration and is suitable
for small to medium-sized databases.
• Connecting to a SQLite Database:
• library(RSQLite)
• con <- dbConnect(SQLite(), "mydatabase.db")
• Executing SQL Queries:
• result <- dbGetQuery(con, "SELECT * FROM mytable")
Libraries cont.
• Fetching Data:
• data <- dbFetch(result)
• Error Handling
tryCatch({
result <- dbGetQuery(con, "SELECT * FROM mytable")
}, error = function(e) {
print(paste("Error:", e$message))
})
• Parameterized Queries:
• dbExecute(con, "DELETE FROM mytable WHERE id = ?", params = list(id))
Why use a database management system
• Simple and effective software for storing, managing and retrieving
information
• Widely used to store and maintain lots of existing data
• Fast processing across large amounts of data
• Out-of-memory and remote processing for really large data
• Can handle spatially explicit queries (GIS)
• Improve quality control of data entry
• Set valid data type and value constraints.
• Use data entry forms in Excel, Access, Filemaker, etc.
• Use quality control scripts to test entered data.
• Concepts are core to understanding data manipulation in other languages
Relational databases
• We will be working with ‘Relational Databases’.
• Data is stored in tables.
• One table per type of data
• Tables can be linked together to combine information.
• Each row contains a single record.
• A single observation or data point
• Each column or field contains a single attribute.
• A single type of information
• The concepts of relational database management are core to
understanding how to do similar things using programming
languages such as R or Python.
SQLite
• We will primarily use SQLite.
• Simple to use
• Almost no work to set up
• Stored in single file
• But, there are lots of alternatives
• Access - commonly used, GUI
• PostgreSQL - fast/powerful, lots of users
Our data set for today
• https://www.kaggle.com/datasets/atanaskanev/sqlite-sakila-sample-
database/code

• Database Description
• The Sakila sample database is a fictitious database designed to
represent a DVD rental store. The tables of the database include film,
film_category, actor, customer, rental, payment and inventory among
others.

You might also like