liveBook · Manning

Select statement

As explained in Statistics Slam Dunk

A SELECT statement is an SQL query construct that specifies the fields or expressions to retrieve from a data source.

A SELECT statement retrieves chosen fields or expressions from a data source. In R, sqldf() makes it practical to ask focused NBA data questions, such as finding the player with the minimum or maximum win shares. section 2.5.2

Use a SELECT statement to:

  • Name the variables to return and identify their source with SELECT and FROM. section 2.5.2
  • Retrieve one record or a short list, such as salaries for a particular team and season. section 2.5.2 section 12.4
  • Filter results with WHERE and combine conditions with AND; dplyr provides a related approach through filter(), but uses different syntax. section 12.4
  • Inspect messy data before analysis, including blank teams and seasons with unusually few records. section 12.4

Find one team-season

sqldf("SELECT * FROM gini3 WHERE season_end = 2012 AND team = 'Denver Nuggets'") returns the Denver Nuggets’ 2012 salary records. Applying the same query to gini4 returns only the ranked record retained after preprocessing. section 12.4

sqldf("SELECT min(WS), Player, Tm, Pk, Year FROM draft")
##   min(WS)        Player  Tm Pk Year
## 1    -1.6 Mardy Collins NYK 29 2006
sqldf("SELECT max(WS), Player, Tm, Pk, Year FROM draft")
##   max(WS)       Player  Tm Pk Year
## 1   236.1 LeBron James CLE  1 2003
sqldf("SELECT min(G), Player, Tm, Pk, Year FROM draft")
##   min(G)          Player  Tm Pk Year
## 1      6 Pavel Podkolzin UTA 21 2004
sqldf("SELECT max(G), Player, Tm, Pk, Year FROM draft")
##   max(G)         Player  Tm Pk Year
## 1   1326 Jamal Crawford CLE  8 2000
sqldf("SELECT min(MP), Player, Tm, Pk, Year FROM draft")
##   min(MP)       Player  Tm Pk Year
## 1     4.3 Julius Hodge DEN 20 2005
sqldf("SELECT max(MP), Player, Tm, Pk, Year FROM draft")
##   max(MP)       Player  Tm Pk Year
## 1    38.4 LeBron James CLE  1 2003
sqldf("SELECT min(Age), Player, Tm, Pk, Year FROM draft")
##   min(Age)       Player  Tm Pk Year
## 1   17.249 Andrew Bynum LAL 10 2005
sqldf("SELECT max(Age), Player, Tm, Pk, Year FROM draft")
##   max(Age)          Player  Tm Pk Year
## 1   25.019 Mamadou N'Diaye DEN 26 2000

Compare SELECT queries that retrieve minimum and maximum values alongside player, team, draft pick, and year. section 2.5.2

How Statistics Slam Dunk covers it

How deep it goes
Explains SELECT, FROM, WHERE, and AND through executable queries, while deferring reusable analysis objects to dplyr.
Examples
Demonstrates win-share extremes, blank teams, team-season records, and comparisons between gini3 and ranked gini4 data.
What it assumes
Assumes an R data frame and sqldf() while teaching clause roles and contrasting dplyr filtering syntax.
What you can do after
Apply it via sqldf() to retrieve selected fields, filter records, and inspect short result sets.
How it connects
Connects SELECT statements with FROM, WHERE, AND, sqldf(), dplyr, filter(), and data-quality checks.

Also explained in

Compare how 9 books explain select statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.