SQL and R - Introduction to Database Queries

SQL and R - Introduction to Database Queries

taught by Ben Baumer


Close Popup

Aim of Course:

Most data are stored in relational database management systems, which are organized as a number of related tables.  To perform common statistical analyses, the data must typically be merged into a single dataframe. 

The SQL (structured query language) programming language is often used to pull data from the various tables in a database and to assemble the data in a format amenable to statistical analysis or review.  SQL can also be used for basic calculations, but it’s not meant for heavy-duty statistical programming.

The purpose of this online course, "SQL and R Introduction to Database Queries" is to teach you how to extract data from a relational database using SQL, and then merge the data into a single file in R, so that you can perform statistical operations.   The focus is on structuring queries to extract structured data (not on building databases or methods of handling big data).

An example you might face at work is: you’d like to see the effect of different variations of an online ad on click-through rates (CTR's).  All the various ad names are stored in one table, while daily CTR's for each ad are stored another table.  You'll need to total CTR's by ad and merge that data with the ads table.   your final goal may be to correlate ad characteristics with total CTR's.

This is an introductory course that will help you get started on dealing with problems like the above. You will learn how to think “like” a relational database, so that you can manipulate matrixes and vectors of data using SQL queries.   Then you will learn how to bring data from your database and organize it into a flatfile in R. This course will teach you how to prepare data so that you can perform anything from basic statistical calculations (e.g. averages, tabulations, linear regressions, test of two means) to machine learning algorithms on your data.  By the end of the 4 weeks, you should be able to visualize how you need to manipulate a dataset in order to perform a desired calculation or answer a particular question.

This course may be taken individually (one-off) or as part of a certificate program.
Course Program:

WEEK 1: Thinking Ahead, How to Organize your Data?

  • Discussion and visuals of data models how data are stored in Postgres(SQL), i.e. entity relationship schemes, vs. R, flat files or data frames.
  • Unique identifiers, and primary vs. foreign keys
  • Questions we’ll ask: Will I need all the data (panel), or should I select certain segments of the data?
  • Restoring a database in Postgres

WEEK 2:  Basic SQL Procedures and Functions

  • Using commands: Select and Where, Like, Order By, And, Or
  • Using functions: Count, Avg, Sum
  • Using: group-by and having
  • Subqueries: In
  • Copying tables to csv from SQL

WEEK 3:  Bringing SQL into R

  • Joins (Inner, Outer, and multiple joins) in SQL
  • Using the ODBC driver to run SQL queries in R.
  • Reading in csv files
WEEK 4:  Working in R

Plyr function in R for grouping data

Performing a few basic statistical calculations in R

  • Joins vs merges?
  • Subsetting data and merging it back in
  • Data summary statistics
  • Basic Graph with Ggplot

And if there’s time, we’ll compare some basic calculations (counts, sums, averages) in SQL and R, so you can check your work in R on the raw data in Postgres.


Homework in this course consists of short answer questions to test concepts and guided data wrangling projects using SQL and R.

In addition to assigned readings, this course also has practice exercises, and supplemental readings available online.

SQL and R - Introduction to Database Queries

Who Should Take This Course:

This course is meant for beginners in data science who need to learn how to extract data from their relational databases.  It can also be a good kickstarter course for individuals who have fluency in SPSS, SAS, or STATA. Such data scientists might not have had to work with relational databases directly before, or they may not be familiar with the syntax and logic of a more object oriented language like R.

Introductory / Intermediate

Familiarity with Excel; basic familiarity with R

Basic knowledge of vectors and matrices

Note:  Statistics.com's introductory courses for R do not need to be taken prior to this course, but if you are planning to take them anyway, you may gain more from this course by taking the Introduction to R courses first.
Organization of the Course:

This course takes place online at the Institute for 4 weeks. During each course week, you participate at times of your own choosing - there are no set times when you must be online. Course participants will be given access to a private discussion board. In class discussions led by the instructor, you can post questions, seek clarification, and interact with your fellow students and the instructor.

At the beginning of each week, you receive the relevant material, in addition to answers to exercises from the previous session. During the week, you are expected to go over the course materials, work through exercises, and submit answers. Discussion among participants is encouraged. The instructor will provide answers and comments, and at the end of the week, you will receive individual feedback on your homework answers.

Time Requirement:
About 15 hours per week, at times of  your choosing.

Options for Credit and Recognition:
Students come to the Institute for a variety of reasons. As you begin the course, you will be asked to specify your category:
  1. No credit - You may be interested only in learning the material presented, and not be concerned with grades or a record of completion.
  2. Certificate - You may be enrolled in PASS (Programs in Analytics and Statistical Studies) that requires demonstration of proficiency in the subject, in which case your work will be assessed for a grade.
  3. CEUs and/or proof of completion - You may require a "Record of Course Completion," along with professional development credit in the form of Continuing Education Units (CEU's).  For those successfully completing the course,  CEU's and a record of course completion will be issued by The Institute, upon request.
  4. Other options - Statistics.com Specializations, INFORMS CAP recognition, and academic (college) credit are available for some Statistics.com courses
College credit:
SQL and R - Introduction to Database Queries has been evaluated by the American Council on Education (ACE) and is recommended for the upper-division baccalaureate degree category, 3 semester hours in computer science or programming. Note: The decision to accept specific credit recommendations is up to each institution. More info here.

This course is also recognized by the Institute for Operations Research and the Management Sciences (INFORMS) as helpful preparation for the Certified Analytics Professional (CAP®) exam, and can help CAP® analysts accrue Professional Development Units to maintain their certification .
Course Text:

Step by step SQL and R instructions will be supplied in the course.  The required text for this course is  SQL in a Nutshell . If you’d like to have a reference book for R, take a look at  R in a Nutshell .

If you already have some statistical programming experience and are looking for a quick entry into R based on that experience, you may want to try R for STATA users or R for SAS and SPSS users.


We will use

1.  Postgres SQL:  http://www.postgresql.org/download/

2.  R:

Mac (tar.gz file): http://cran.r-project.org/bin/macosx/

Windows:  http://cran.r-project.org/bin/windows/base/

3.  R Studio (an editing and development environment for R): http://www.rstudio.com/ide/download/

Please start the process of getting these software components in place at least one week before the class start date.



To be scheduled.

SQL and R - Introduction to Database Queries


To be scheduled.

Course Fee: $549

Do you meet course prerequisites? What about book & software? (Click here to learn more)

Group rates: Click here to get information on group rates. 

First time student or academic? Click here for an introductory offer on select courses. Academic affiliation?  You may be eligible for a discount at checkout.

Register Now

Add $50 service fee if you require a prior invoice, or if you need to submit a purchase order or voucher, pay by wire transfer or EFT, or refund and reprocess a prior payment. Please use this printed registration form, for these and other special orders.

Courses may fill up at any time and registrations are processed in the order in which they are received. Your registration will be confirmed for the first available course date, unless you specify otherwise.

The Institute for Statistics Education is certified to operate by the State Council of Higher Education in Virginia (SCHEV).

Contact Us
Have a question about a course before you register? Call us. We're here for you. (571) 281-8817or

Want to be notified of future courses?

Student comments