Appendix B — SQL primer

This brief appendix aims to serve two groups of users. The first group comprises those who have followed the material above and would like a quick introduction to SQL. The second group comprises those who know SQL (say, SAS’s PROC SQL) would like a quick introduction to the dplyr-based approach to R that we use in this book. SQL is a specialized language for manipulating and retrieving tabular data used by almost all modern database systems.

B.1 What is dplyr?

Throughout this course, we make use of the R package dplyr, which is a core part of the Tidyverse. From the Tidyverse website:

dplyr is a grammar of data manipulation, providing a consistent set of verbs that help you solve the most common data manipulation challenges:

  • mutate() adds new variables that are functions of existing variables
  • select() picks variables based on their names
  • filter() picks cases based on their values
  • summarize() reduces multiple values down to a single summary
  • arrange() changes the ordering of the rows

Prior to the advent of the dplyr in 2014, most users of R would have used base R functions and operators, such as subset(), $, and [. However, dplyr provides a much more consistent framework for manipulating data that is easier to learn, especially for users familiar with SQL. This last point makes sense, as SQL provides something like a “grammar of data manipulation” of its own. In fact, each dplyr verb has an SQL equivalent keyword or concept, as we see below.

B.2 A translation table

Table B.1: SQL translations of key dplyr verbs
dplyr verb (R) SQL equivalent
|> FROM
select() SELECT
filter() WHERE
group_by() GROUP BY
arrange() ORDER BY
mutate() used-defined columns
summarize() used-defined aggregate columns

As in earlier chapters, let’s set up a database connection that we can use within R.

library(DBI)
library(dplyr, warn.conflicts =  FALSE)
library(farr)
db <- dbConnect(RPostgres::Postgres())

crsp.dsf <- tbl(db, Id(table = "dsf", schema = "crsp"))
create_table <- function(conn, table, schema = "", 
                         data_dir = Sys.getenv("DATA_DIR"))  {
  dbExecute(conn, paste0("CREATE SCHEMA IF NOT EXISTS ", schema))
  file_path <- file.path(data_dir, schema, paste0(table, ".parquet"))
  df_sql <- paste0("CREATE VIEW ", schema, ".", table, " AS ",
                   "SELECT * FROM read_parquet('", file_path, 
                   "')")
  DBI::dbExecute(conn, dplyr::sql(df_sql))
}

db <- dbConnect(duckdb::duckdb())

crsp.dsi <- create_table(db, table = "dsi", schema = "crsp") 
crsp.dsf <- create_table(db, table = "dsf", schema = "crsp")

B.3 SQL terms SELECT and FROM

Let’s begin with a basic SQL query.

SELECT date, vwretd, ewretd
FROM crsp.dsi
Displaying records 1 - 10
date vwretd ewretd
1925-12-31 NA NA
1926-01-02 0.0056892 0.0095159
1926-01-04 0.0007065 0.0057804
1926-01-05 -0.0048213 -0.0019270
1926-01-06 -0.0004233 0.0011816
1926-01-07 0.0049883 0.0084531
1926-01-08 -0.0032378 -0.0016890
1926-01-09 0.0022093 0.0033121
1926-01-11 -0.0085396 -0.0099432
1926-01-12 -0.0009290 -0.0036231

This query

  • extracts the data in three columns (date, vwretd, ewretd) (the first line)
  • from the table named crsp.dsi (the second line)

While the syntax is a bit different, SQL’s SELECT operates very much like select() from dplyr.

Translating this into dplyr code using the pipe operator (|>), it’s easy to see that the order of presentation is one of the big differences between SQL and the dplyr approach.

While we first needed to set up the table on which to operate, once we’ve done so we can see that the FROM is implicit in the |> operator.

crsp.dsi |>
  select(date, vwretd, ewretd)  
date vwretd ewretd
1925-12-31 NA NA
1926-01-02 0.0056892 0.0095159
1926-01-04 0.0007065 0.0057804
1926-01-05 -0.0048213 -0.0019270
1926-01-06 -0.0004233 0.0011816

B.4 SQL WHERE

The filter() verb from dplyr corresponds to WHERE in SQL. Note that WHERE goes after the FROM clause in SQL, though in practice the query optimizer will option execute the filter implied by the WHERE clause before executing other elements of a query (it would be wasteful to perform calculations on data that are going to be filtered out later on).

SELECT date, vwretd, ewretd
FROM crsp.dsi
WHERE date = '2015-01-02'
1 records
date vwretd ewretd
2015-01-02 -0.0002423 0.0014701

In dplyr, this query could be written as follows:

crsp.dsi |>
  select(date, vwretd, ewretd) |>
  filter(date == '2015-01-02')
date vwretd ewretd
2015-01-02 -0.0002423 0.0014701

But it could also be written with the filter coming first.

crsp.dsi |>
  filter(date == '2015-01-02') |>
  select(date, vwretd, ewretd) 
date vwretd ewretd
2015-01-02 -0.0002423 0.0014701

Note that each of these dplyr queries is implemented in the same way by the PostgreSQL query optimizer, as can be seen by examining the output from explain().

crsp.dsi |>
  select(date, vwretd, ewretd) |>
  filter(date == '2015-01-02') |>
  explain()
<SQL>
SELECT "date", "vwretd", "ewretd"
FROM "crsp"."dsi"
WHERE ("date" = '2015-01-02')

<PLAN>
                                            QUERY PLAN
1 Seq Scan on dsi  (cost=0.00..681.49 rows=1 width=20)
2                  Filter: (date = '2015-01-02'::date)
crsp.dsi |>
  filter(date == '2015-01-02') |>
  select(date, vwretd, ewretd) |>
  explain()
<SQL>
SELECT "date", "vwretd", "ewretd"
FROM "crsp"."dsi"
WHERE ("date" = '2015-01-02')

<PLAN>
                                            QUERY PLAN
1 Seq Scan on dsi  (cost=0.00..681.49 rows=1 width=20)
2                  Filter: (date = '2015-01-02'::date)

We can see that these are in turn viewed by the query optimizer as equivalents of the SQL above.

EXPLAIN
SELECT date, vwretd, ewretd
FROM crsp.dsi
WHERE date = '2015-01-02'
2 records
QUERY PLAN
Seq Scan on dsi (cost=0.00..681.49 rows=1 width=20)
Filter: (date = ‘2015-01-02’::date)

This illustrates a nice feature of SQL engines, namely that you can leave some of the details of the query to the software and focus on the higher-level requirements of your query (i.e., which observations to keep, etc.).

B.5 SQL ORDER BY

A R data frame can be considered as a list of vectors, where each vector has a well-defined order. In contrast, SQL tables should be not considered to have any particular order absent an ORDER BY clause.1

An ORDER BY clause is placed at the end of an SQL query (reflecting in this case the order of operations) followed by the columns that the query is to be sorted on.

SELECT date, vwretd, ewretd
FROM crsp.dsi
ORDER BY vwretd
Displaying records 1 - 10
date vwretd ewretd
1987-10-19 -0.1713457 -0.1038971
1929-10-29 -0.1195386 -0.1348561
2020-03-16 -0.1181675 -0.1076310
1929-10-28 -0.1130215 -0.0952016
1929-11-06 -0.0970203 -0.0838187
2020-03-12 -0.0968405 -0.1005971
1933-07-21 -0.0932724 -0.1354675
2008-10-15 -0.0898179 -0.0707809
2008-12-01 -0.0894055 -0.0782403
1933-07-20 -0.0849734 -0.0931411

Thus ORDER BY works very much like arrange().

crsp.dsi |>
  select(date, vwretd, ewretd) |>
  arrange(vwretd)
date vwretd ewretd
1987-10-19 -0.1713457 -0.1038971
1929-10-29 -0.1195386 -0.1348561
2020-03-16 -0.1181675 -0.1076310
1929-10-28 -0.1130215 -0.0952016
1929-11-06 -0.0970203 -0.0838187

To reverse the order, use the DESC keyword after the relevant variable.

SELECT date, vwretd, ewretd
FROM crsp.dsi
ORDER BY vwretd DESC, date
Displaying records 1 - 10
date vwretd ewretd
1925-12-31 NA NA
1933-03-15 0.1568376 0.2030201
1929-10-30 0.1221548 0.1257142
2008-10-13 0.1149183 0.1074221
1931-10-06 0.1112778 0.0887420
1932-09-21 0.1098460 0.1130083
2008-10-28 0.0953434 0.0503864
2020-03-24 0.0915562 0.0821751
1939-09-05 0.0891618 0.1474485
1987-10-21 0.0866138 0.0692920

Thus DESC is very much equivalent to desc() in dplyr apart from the minor syntactical difference that desc() is written as a function of the relevant variable.

crsp.dsi |>
  select(date, vwretd, ewretd) |>
  arrange(desc(vwretd), date)
date vwretd ewretd
1925-12-31 NA NA
1933-03-15 0.1568376 0.2030201
1929-10-30 0.1221548 0.1257142
2008-10-13 0.1149183 0.1074221
1931-10-06 0.1112778 0.0887420

It is important to note that some differences in the implementation details between data frames in R and tables in SQL are seen with arrange(). While a data frame in R is fundamentally a list of ordered vectors, SQL tables are best thought of a sets of rows without a natural order. As such, one needs to pay attention to when arrange() is implemented in a query that depends on it.

To illustrate this issue, suppose we get get stock returns for Apple and order them from largest to smallest returns.2

apple <-
  crsp.dsf |>
  filter(permno == 14593L) |>
  select(permno, date, ret) 

apple |>
  arrange(desc(ret)) 
permno date ret
14593 1980-12-12 NA
14593 1997-08-06 0.3322785
14593 1998-01-02 0.2380952
14593 1996-07-18 0.2370370
14593 1998-01-06 0.1929134

The following code illustrates that the order created above is lost when we merged with crsp.dsi. Fortunately, dplyr provides a warning when we use arrange() in such situations.

apple |>
  inner_join(crsp.dsi, by = "date") |>
  select(permno, date, ret, vwretd)
permno date ret vwretd
14593 1980-12-12 NA 0.0148595
14593 1980-12-15 -0.0520607 0.0016051
14593 1980-12-16 -0.0732265 0.0074853
14593 1980-12-17 0.0246914 0.0161683
14593 1980-12-18 0.0289157 0.0041344

B.6 SQL approach to mutate()

Creating new variables in SQL is quite straightforward. In addition to basic mathematical operators, such as + and *, PostgreSQL and DuckDB have an extensive array of functions available for use in queries.3

The names of calculated variables can be specified using AS, as can be seen in the following query.

SELECT date, 
  vwretd - ewretd AS ret_diff,
  ln(1 + vwretd) AS log_vwretd,
  date_part('year', date) AS year
FROM crsp.dsi
Displaying records 1 - 10
date ret_diff log_vwretd year
1925-12-31 NA NA 1925
1926-01-02 -0.0038267 0.0056731 1926
1926-01-04 -0.0050740 0.0007062 1926
1926-01-05 -0.0028943 -0.0048330 1926
1926-01-06 -0.0016049 -0.0004234 1926
1926-01-07 -0.0034649 0.0049759 1926
1926-01-08 -0.0015489 -0.0032431 1926
1926-01-09 -0.0011027 0.0022069 1926
1926-01-11 0.0014036 -0.0085763 1926
1926-01-12 0.0026942 -0.0009294 1926

The translation of this query into dplyr requires the use of the mutate() verb, but this translation is straightforward, as can be seen below.

crsp.dsi |>
  mutate(ret_diff = vwretd - ewretd, 
         log_vwretd = ln(1 + vwretd),
         year = date_part('year', date)) |>
  select(date, ret_diff, log_vwretd, year) 
date ret_diff log_vwretd year
1925-12-31 NA NA 1925
1926-01-02 -0.0038267 0.0056731 1926
1926-01-04 -0.0050740 0.0007062 1926
1926-01-05 -0.0028943 -0.0048330 1926
1926-01-06 -0.0016049 -0.0004234 1926

Note that in the code above, we are using the function ln() to get the natural logarithm, rather than the R function log(). We will discuss this further below, but for now we just note that log() in PostgreSQL returns the base-10 logarithm and—because the calculations are being done by PostgreSQL, not R—we need to use the appropriate PostgreSQL function when using dplyr in this way.4 Similarly, we are using the date_part() function from PostgreSQL, but in this case we could have used year(), which is translated by dbplyr to SQL that gives the nearest equivalent to year() from the lubridate package.

Note that the dplyr code is slightly more verbose than the SQL because the SQL equivalent of mutate() is an implicit part of the SELECT portion of the statement.

B.7 SQL GROUP BY and aggregates

One use for group_by() in R is to create summary statistics and the like for each of the groups implied by the group_by() variables. In SQL, GROUP BY is used with aggregate functions, which are equivalent to functions used with summarize().

Suppose we want to get the maximum and minimum returns, as well as a measure of the volatility of the returns, for each year. In SQL, we could do the following:

SELECT date_part('year', date) AS year,
  max(vwretd) AS max_ret,
  min(vwretd) AS min_ret,
  stddev(ln(1 + vwretd)) AS sd_ret
FROM crsp.dsi
GROUP BY year
ORDER BY sd_ret DESC
Displaying records 1 - 10
year max_ret min_ret sd_ret
1925 NA NA NA
1932 0.1098460 -0.0731177 0.0274218
1933 0.1568376 -0.0932724 0.0256663
2008 0.1149183 -0.0898179 0.0253972
2020 0.0915562 -0.1181675 0.0213797
1929 0.1221548 -0.1195386 0.0207289
1931 0.1112778 -0.0680485 0.0205423
1987 0.0866138 -0.1713457 0.0182097
2009 0.0691054 -0.0553249 0.0176678
1938 0.0573017 -0.0516716 0.0164642

And the dplyr equivalent would look like this:

crsp.dsi |>
  mutate(year = year(date)) |>
  group_by(year) |>
  summarize(max_ret = max(vwretd, na.rm = TRUE),
            min_ret = min(vwretd, na.rm = TRUE),
            sd_ret = stddev(ln(1 + vwretd))) |>
  arrange(desc(sd_ret))
year max_ret min_ret sd_ret
1925 NA NA NA
1932 0.1098460 -0.0731177 0.0274218
1933 0.1568376 -0.0932724 0.0256663
2008 0.1149183 -0.0898179 0.0253972
2020 0.0915562 -0.1181675 0.0213797

Note that the na.rm = TRUE arguments could be omitted, as SQL always omits these values. The inclusion of this argument serves only as a reminder to the user of the code that these values are omitted.5


  1. See https://stackoverflow.com/questions/20050341 for discussion.↩︎

  2. Note that the NA value from Apple’s first day of trading is viewed as being the largest value due to the way that SQL collates NULL values.↩︎

  3. See https://www.postgresql.org/docs/current/functions.html.↩︎

  4. See the PostgreSQL documentation for mathematical functions.↩︎

  5. Setting na.rm = TRUE also suppresses warnings from dplyr regarding the handing of missing values in SQL.↩︎