SQL Select Statements Using NBA Data

Analyzing NBA Cleveland Cavaliers 2017–2018 Data

Image for post
Image for post

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 !

Image for post
Image for post
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.

Select all columns from that table

SELECT * FROM cavs;

This query returns the original table.

Image for post
Image for post
Image for post
Image for post

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.

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.

Image for post
Image for post

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!

Image for post
Image for post

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.

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.

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.

Image for post
Image for post
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.

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