Learn How to Write SQL From R

[This article was first published on business-science.io, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This article is part of a R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks.

SQL queries getting you down 😰? Let R write SQL queries for you!

Here are the links to get set up. 👇

(Click image to play tutorial)

Writing SQL from R

We’re going to generate SQL from R using 3 key packages:

  • DBI – For connecting to & working with databases
  • RSQLite – For working with the SQLite-variety of database
  • tidyverse – Gives us dbplyr – the database-backened to dplyr – FOR FREE!! OMG!

Step 1: Connect to database
Use DBI to make a connection

DBI has the dbConnect() function. Let’s use this to establish a connection with our SQL database.

Examine the connection. You should see a connection object has been created and the SQL database has one table named “MPG”.

Step 2: Write Dplyr, Generate SQL
Use the dbplyr backend to auto-magically create SQL for us.

DBI has the show_query() function we can use to generate SQL…

…and here’s the result!

Friendly Reminders:
1. Full code in the Github Repository.
2. Watch the YouTube Video for detailed instructions.
3. Share with your friends. (Don’t be a hog).

The look on your co-workers face says everything.

But if you really want to improve your productivity…

Here’s how to master R. 👇 What happens after you learn R for Business.

When your CEO gets word of your Shiny Apps saving the company tons of $$$ (cha-ching!). 👇

This is career acceleration.


  1. Sign Up to Get the R-Tips Weekly (You’ll get email notifications of NEW R-Tips as they are released)

  2. Set Up the GitHub Repo

  3. Check out the setup video. Or, Hit Pull in the Git Menu to get the R-Tips Code

Once you take these actions, you’ll be set up to receive R-Tips with Code every week. =)

To leave a comment for the author, please follow the link and comment on their blog: business-science.io.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)