dbplyr: SQL via dplyr Syntax
Write dplyr code that translates to SQL and runs on the database.
dbplyr: SQL via dplyr Syntax is a free R Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the R Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is dbplyr?
dbplyr is an R package that translates dplyr verbs into SQL automatically. Instead of writing raw SQL, you write familiar dplyr code and dbplyr converts it to the correct SQL dialect for your database backend. The query runs in the database — not in R memory.
# install.packages(c('dbplyr', 'DBI', 'RSQLite'))
library(DBI)
library(dbplyr)
library(dplyr)
# dbplyr sits between dplyr and your database:
# Your dplyr code -> dbplyr -> SQL -> Database -> result
# Supported backends: PostgreSQL, MySQL, SQLite,
# SQL Server, BigQuery, Snowflake, ...
cat('dbplyr translates dplyr to SQL')Connecting to a Database
dbplyr works on top of a DBI connection. You establish the connection with DBI::dbConnect() using the appropriate driver package, then pass that connection object to dbplyr functions.
library(DBI)
library(dplyr)
library(dbplyr)
# SQLite example (no server needed — great for demos)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
# Write a test table to the in-memory database
copy_to(con, nycflights13::flights, 'flights',
temporary = FALSE, overwrite = TRUE)
# PostgreSQL example (real server):
# con <- dbConnect(
# RPostgres::Postgres(),
# host = 'db.example.com',
# dbname = 'analytics',
# user = Sys.getenv('DB_USER'),
# password = Sys.getenv('DB_PASS')
# )
cat('Connected to database')tbl() — Reference a Database Table
tbl(con, 'table_name') creates a lazy reference to a database table. No data is fetched yet — you just get a pointer. You can then chain dplyr verbs onto it, and dbplyr will build up the SQL query incrementally.
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
mtcars_df <- mtcars
dbWriteTable(con, 'cars', mtcars_df)
# Create a lazy table reference
cars_tbl <- tbl(con, 'cars')
# Printing shows top rows and indicates it is a database source
print(cars_tbl)
# Source: table<cars> [?? x 11]
# Database: sqlite 3.x [:memory:]
cat('tbl() = lazy reference, no data fetched yet')Applying filter() on a Database Table
You can chain filter() onto a tbl() reference just like you would on a local data frame. dbplyr translates it to a SQL WHERE clause. The filtering happens in the database — only matching rows will be sent to R.
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
dbWriteTable(con, 'cars', mtcars)
cars_tbl <- tbl(con, 'cars')
# Filter in the database (generates WHERE clause)
high_mpg <- cars_tbl |>
filter(mpg > 25, cyl == 4)
# Still lazy! No data fetched yet.
cat('Class:', class(high_mpg)[1], '\n')
# Collect to pull data into R:
result <- collect(high_mpg)
cat('Rows matching filter:', nrow(result))Applying select() and mutate()
select() maps to SQL SELECT and mutate() maps to computed columns in the SELECT clause. dbplyr handles the translation, including many common R expressions that have SQL equivalents.
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
dbWriteTable(con, 'cars', mtcars)
cars_tbl <- tbl(con, 'cars')
# Select specific columns + add a computed column
result <- cars_tbl |>
filter(am == 1) |> # manual transmission
select(mpg, cyl, hp, wt) |> # pick columns
mutate(wt_kg = wt * 453.592) # add computed column
# Pull into R
df <- collect(result)
cat('Columns:', names(df), '\n')
cat('Rows:', nrow(df))group_by() and summarise() — Aggregation
group_by() and summarise() translate to SQL GROUP BY with aggregate functions. This lets you compute summaries in the database without pulling all rows into R first — critical for large tables.
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
dbWriteTable(con, 'cars', mtcars)
cars_tbl <- tbl(con, 'cars')
# Aggregate in the database
summary_tbl <- cars_tbl |>
group_by(cyl) |>
summarise(
avg_mpg = mean(mpg, na.rm = TRUE),
max_hp = max(hp),
n_models = n()
)
result <- collect(summary_tbl)
print(result)show_query() — Inspect Generated SQL
show_query() prints the SQL that dbplyr will send to the database. This is invaluable for debugging, performance tuning, and learning SQL by seeing how your dplyr code translates.
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
dbWriteTable(con, 'cars', mtcars)
cars_tbl <- tbl(con, 'cars')
# Build a query
q <- cars_tbl |>
filter(mpg > 20) |>
group_by(cyl) |>
summarise(avg_mpg = mean(mpg, na.rm = TRUE))
# See the SQL dbplyr generated:
show_query(q)
# <SQL>
# SELECT cyl, AVG(mpg) AS avg_mpg
# FROM cars
# WHERE mpg > 20.0
# GROUP BY cylcollect() — Pull Data into R
collect() executes the lazy query and retrieves the results into a local R data frame (tibble). Until you call collect(), no data moves from the database to R — all operations are translated to SQL and run server-side.
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
dbWriteTable(con, 'cars', mtcars)
cars_tbl <- tbl(con, 'cars')
# Build up a lazy query chain
lazy_q <- cars_tbl |>
filter(hp > 100) |>
select(mpg, hp, cyl) |>
arrange(desc(hp))
# Nothing fetched yet
cat('Is lazy?', inherits(lazy_q, 'tbl_sql'), '\n')
# NOW pull data into R
local_df <- collect(lazy_q)
cat('Class after collect:', class(local_df)[1], '\n')
cat('Rows:', nrow(local_df))copy_to() — Push a Local Data Frame to DB
copy_to() writes a local R data frame into the database as a (usually temporary) table. This is useful for joining local lookup tables with large remote tables, or for testing without a pre-existing database.
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
# Push a local data frame into the database
local_df <- data.frame(
cyl = c(4, 6, 8),
category = c('efficient', 'balanced', 'powerful')
)
# copy_to creates a temporary table in the DB
copy_to(con, local_df, name = 'cyl_labels', temporary = TRUE)
# Now reference it with tbl()
labels_tbl <- tbl(con, 'cyl_labels')
cat('Rows in DB table:', collect(labels_tbl) |> nrow())Joining Database Tables
You can join two tbl() references using the same left_join(), inner_join(), etc. functions from dplyr. dbplyr translates them into SQL JOIN clauses — the join runs in the database.
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
dbWriteTable(con, 'cars', mtcars)
copy_to(con, data.frame(cyl = c(4,6,8),
label = c('four','six','eight')),
'cyl_ref', temporary = TRUE)
cars_tbl <- tbl(con, 'cars')
labels_tbl <- tbl(con, 'cyl_ref')
# Join in the database
joined <- left_join(cars_tbl, labels_tbl, by = 'cyl') |>
select(mpg, cyl, label, hp)
show_query(joined) # See the SQL JOIN
result <- collect(joined)
cat('Joined rows:', nrow(result))When NOT to Use dbplyr
dbplyr cannot translate every R expression to SQL. Complex custom functions, base R date manipulation, or R-specific statistical functions may not have SQL equivalents. Use collect() first to pull data into R, then apply R-only operations locally.
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
dbWriteTable(con, 'cars', mtcars)
cars_tbl <- tbl(con, 'cars')
# Do as much as possible in the database:
prepped <- cars_tbl |>
filter(mpg > 15) |>
select(mpg, hp, cyl) |>
collect() # <- pull only what you need
# Now apply R-only operations locally:
cor_result <- cor(prepped$mpg, prepped$hp)
cat('Correlation mpg~hp:', round(cor_result, 3))
# Rule: filter and aggregate in DB, model in RQuick Check
After building a chain of dplyr verbs on a tbl() database reference, which function executes the query and returns the results as a local R data frame?
dbplyr — Key Takeaways
dbplyr lets you query databases with dplyr syntax — no SQL required:
tbl(con, 'table')— lazy reference to a DB tablefilter(),select(),mutate()— translated to SQL clausesgroup_by() |> summarise()— becomes SQLGROUP BYshow_query()— inspect the generated SQL (great for learning)collect()— execute the query and pull data into Rcopy_to()— push a local data frame to the database- Joins work too:
left_join(),inner_join(), etc. - Filter and aggregate in the DB; use R only for what SQL cannot do
# Complete dbplyr workflow example:
library(DBI)
library(dplyr)
library(dbplyr)
con <- dbConnect(RSQLite::SQLite(), ':memory:')
dbWriteTable(con, 'sales', data.frame(
region = c('North','South','North','East','South'),
revenue = c(100, 200, 150, 300, 250),
year = c(2023, 2023, 2024, 2024, 2024)
))
tbl(con, 'sales') |>
filter(year == 2024) |>
group_by(region) |>
summarise(total = sum(revenue, na.rm = TRUE)) |>
arrange(desc(total)) |>
collect() |>
print()Frequently asked questions
Is the “dbplyr: SQL via dplyr Syntax” lesson free?
Yes — the full text of “dbplyr: SQL via dplyr Syntax” is free to read here on the web, and the R Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the R Academy course, upgrade to CoddyKit PRO.
What will I learn in “dbplyr: SQL via dplyr Syntax”?
Write dplyr code that translates to SQL and runs on the database. You practise R Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start R Academy?
No prior experience is required. R Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “dbplyr: SQL via dplyr Syntax” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this R Academy lesson?
Yes. Every R Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- DBI and RSQLite Basics
- Connecting to PostgreSQL and MySQL
- dbplyr: SQL via dplyr Syntax
- Parameterized Queries and Transactions