SQL Select Statements Using NBA Data

Analyzing NBA Cleveland Cavaliers 2017–2018 Data

In this article, I want to put together a few SELECT statements to do some queries against some 2017 NBA data, specifically the Cleveland Cavaliers. I got the data from stats.nba.com . If you want the exact data that I used then you can find it on my Github here. SQL stands for Structured Query Language and is used in relational databases. SQL is a great way to manipulate data to get the answers that you want or to see some insight in data that you didn’t expect. Let’s get started !

Cavaliers Team Roster Table “cavs

For these examples we are going to use the NBA Cavaliers table above , called “cavs”. The data from the above table is below without the columns height and birthdate, we will be manipulating this table.

         PLAYER     NO. POS  WEIGHT AGE EXP
1 Kevin Love #0 F-C 251 lbs 29 9
2 Derrick Rose #1 G 190 lbs 29 8
3 Isaiah Thomas #3 G 185 lbs 28 6
4 Iman Shumpert #4 G 220 lbs 27 6
5 JR Smith #5 G-F 225 lbs 32 13
6 Channing Frye #8 F 255 lbs 34 11
7 Dwyane Wade #9 G 220 lbs 35 14
8 John Holland #10 G-F 205 lbs 29 1
9 Tristan Thompson #13 C-F 238 lbs 26 6
10 London Perrantes #15 G 190 lbs 23 R
11 Cedi Osman #16 F 215 lbs 22 R
12 LeBron James #23 F-G 250 lbs 32 14
13 Kyle Korver #26 G 212 lbs 36 14
14 Jeff Green #32 F 235 lbs 31 9
15 Ante Zizic #41 F-C 250 lbs 20 R
16 Jose Calderon #81 G 200 lbs 36 12
17 Jae Crowder #99 F 235 lbs 27 5

Select all columns from that table

SELECT * FROM cavs;

This query returns the original table.

          PLAYER     NO. POS  WEIGHT AGE EXP
1 Kevin Love #0 F-C 251 lbs 29 9
2 Derrick Rose #1 G 190 lbs 29 8
3 Isaiah Thomas #3 G 185 lbs 28 6
4 Iman Shumpert #4 G 220 lbs 27 6
5 JR Smith #5 G-F 225 lbs 32 13
6 Channing Frye #8 F 255 lbs 34 11
7 Dwyane Wade #9 G 220 lbs 35 14
8 John Holland #10 G-F 205 lbs 29 1
9 Tristan Thompson #13 C-F 238 lbs 26 6
10 London Perrantes #15 G 190 lbs 23 R
11 Cedi Osman #16 F 215 lbs 22 R
12 LeBron James #23 F-G 250 lbs 32 14
13 Kyle Korver #26 G 212 lbs 36 14
14 Jeff Green #32 F 235 lbs 31 9
15 Ante Zizic #41 F-C 250 lbs 20 R
16 Jose Calderon #81 G 200 lbs 36 12
17 Jae Crowder #99 F 235 lbs 27 5

Select specific columns

SELECT player, school FROM cavs;

This returns the Players and the school they went to. Wow I didn’t know Jeff Green went to Georgia Tech.

           PLAYER                           SCHOOL
1 Kevin Love UCLA
2 Derrick Rose Memphis
3 Isaiah Thomas Washington
4 Iman Shumpert Georgia Tech
5 JR Smith St. Benedict's Prep (NJ)
6 Channing Frye Arizona
7 Dwyane Wade Marquette
8 John Holland Boston U.
9 Tristan Thompson Texas
10 London Perrantes Virginia
11 Cedi Osman Anadolu Efes
12 LeBron James St. Vincent-St. Mary HS (OH)
13 Kyle Korver Creighton
14 Jeff Green Georgetown
15 Ante Zizic <NA>
16 Jose Calderon Villanueva de la Serena, Spain
17 Jae Crowder Marquette

Select all players whose name start with ‘I’

SELECT player FROM cavs WHERE player like ‘I%’;

This query returns all players whose name start with ‘I’. Looks like Isaiah and Iman are the only two players from this table whose name starts with ‘I’. NOTE: ‘I%’ means return return ‘I’ and any other symbols behind it.

  PLAYER
1 Isaiah Thomas
2 Iman Shumpert

Select LeBron James , his age and weight

SELECT player, age, weight FROM cavs WHERE player = ‘LeBron James’;

This query returns the row that has LeBron James as well as his age and his weight. Wow he is 32 and 250 pounds!

  PLAYER        AGE  WEIGHT
1 LeBron James 32 250 lbs

Count the number of ages on the team roster

SELECT age, count(age) FROM cavs GROUP BY age;

This returns the age and the number of players that is that age on the Cavs. Looks like we have ages from 20 to 36 and 3 players share the age 29.

    AGE     count(age)
1 20 1
2 22 1
3 23 1
4 26 1
5 27 2
6 28 1
7 29 3
8 31 1
9 32 2
10 34 1
11 35 1
12 36 2

Count the number of ages on the team roster that have a count greater than or equal to 2.

SELECT age, count(age) FROM cavs GROUP BY age HAVING COUNT(age) ≥ 2;

This returns the age of all of the Cavs players in the table that share the same age. Looks like two players are 27, two are age 36 , two are age 32 , and three are 29.

   AGE      count(age)
1 27 2
2 29 3
3 32 2
4 36 2

Select the players and their age from the Cavs roster by age from oldest to youngest

SELECT player, age FROM cavs ORDER BY age DESC;

This returns a list of players from oldest to youngest. NOTE: DESC = descending or greatest to least. Looks like Kyle Korver and Jose Calderon are the oldest players on the team. Wow, 36 in basketball years is like 77 in human years. NOTE: Kyle’s birthday is actually before Jose.

           PLAYER   AGE
1 Kyle Korver 36
2 Jose Calderon 36
3 Dwyane Wade 35
4 Channing Frye 34
5 JR Smith 32
6 LeBron James 32
7 Jeff Green 31
8 Kevin Love 29
9 Derrick Rose 29
10 John Holland 29
11 Isaiah Thomas 28
12 Iman Shumpert 27
13 Jae Crowder 27
14 Tristan Thompson 26
15 London Perrantes 23
16 Cedi Osman 22
17 Ante Zizic 20
Left:Oldest Player, Kyle Korver age 36/ Right: Youngest Player, Ante Zizic age 20

Select all players with position guard A.K.A ‘G’ from the cavs table who is older than 28.

SELECT player, pos, age FROM cavs WHERE pos =’G’ and age > 28;

The query returns all of the players that play the guard position that are older than 28. It looks like we have four guards that are older than 28.

      PLAYER     POS AGE
1 Derrick Rose G 29
2 Dwyane Wade G 35
3 Kyle Korver G 36
4 Jose Calderon G 36

Thanks for reading and if you enjoyed this article please leave some claps, subscribe and share ! If you want the actual code I used, you can find it on my GitHub account at GitHub.com/randerson112358. Keep up the learning and thanks for reading.

Check Out the following for content / videos on Algorithm Analysis and Programming:

YouTube Channel:
randerson112358: https://www.youtube.com/channel/UCaV_0qp2NZd319K4_K8Z5SQ

compsci112358:
https://www.youtube.com/channel/UCbmb5IoBtHZTpYZCDBOC1CA

Website:
http://everythingcomputerscience.com/

Video Tutorials on Recurrence Relation:
https://www.udemy.com/recurrence-relation-made-easy/

Video Tutorial on Algorithm Analysis:
https://www.udemy.com/algorithm-analysis/

Twitter:
https://twitter.com/CsEverything

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store