An Easy Introduction to SQL for Data Scientists | by George Seif | Towards Data Science

Want to be inspired? Come join my Super Quotes newsletter. 😎
SQL (Structured Query Language) is a standardised programming language designed for data storage and management. It allows one to create, parse, and manipulate data fast and easy.
With the AI-hype of recent years, technology companies serving all kinds of industries have been forced to become more data driven. When a company that serves thousands of customers is data driven, they’ll need a way to store and frequently access data on the order of millions or even billions of data points.
That’s where SQL comes in.
SQL is popular because it’s both fast and easy to understand. It’s designed to be read and written in a similar way to the English language. When an SQL query is used to retrieve data, that data is not copied anywhere, but instead accessed directly where it’s stored making the process much faster than other approaches.
This tutorial will teach you the basics of SQL including:
Creating database tables
Populating the database tables with real data
Retrieving your data for usage in a Data Science or Machine Learning task
Let’s jump right into it!
The first thing we’ll do is actually install our SQL server! That’ll give us a workbench to start playing around with databases and SQL queries.
To install a MySQL server, you can run the following command from your terminal:
sudo apt-get install mysql-server
Now we’ll start our MySQL server. This is similar to how we start Python in the terminal by just typing out “python”. The only difference here is that it’s convenient to give our server root privileges so we’ll have flexible access to everything.
sudo mysql -u root -p
Great! Now our mysql server is running and we can start issuing MySQL commands.
A couple of things to keep in mind before we move forward:
All MySQL commands end with a semicolon. If the command you type does not end with a semicolon then it won’t work!
MySQL commands are typically written in uppercase while any user defined text or numbers are in lowercase to make them easier to distinguish. It’s not strictly required (you won’t get any errors if you don’t follow this), but it’s generally recommended for readability.
Now we’re ready to create our first SQL database.
A database is an organised collection of data. In practical terms, you can think of a database as the highest level in the hierarchy.
A table stores data in rows (tuples) and columns (attributes). A database can and usually does consist of multiple tables
For the rest of our tutorial, we’re going to be using Soccer (or Football if you prefer) players’ stats for the data in our database.
The first thing we’ll do is create a database called “soccer_data” with the “CREATE” command. We can then “activate” our new database with the “USE” command.
CREATE DATABASE soccer_statsUSE soccer_stats
Just like when we create any table in Excel or Google Sheets, we’re going to define the names of each column, along with what type of data will be in that column.
Our “stats” table will track the goals, assists, points, and number of shots for each soccer player:
CREATE TABLE stats (id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, name TEXT, goals INTEGER, assists INTEGER, points INTEGER, shots INTEGER);
The above command did a few things for us:
It created a table called “stats” within the “soccer_stats” databaseWe set up 6 columns in the table — id, name, goals, assists, points, and shotsFor each column, we’ve defined the type of data we want that column to store.The “id” column has a special type (INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT) that automatically numbers each row. It’s good practice to give each row a unique id when building SQL tables in case two rows have the exact same data.
It created a table called “stats” within the “soccer_stats” database
We set up 6 columns in the table — id, name, goals, assists, points, and shots
For each column, we’ve defined the type of data we want that column to store.
The “id” column has a special type (INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT) that automatically numbers each row. It’s good practice to give each row a unique id when building SQL tables in case two rows have the exact same data.
We can check what tables our database has like so:
SHOW TABLES;+------------------------+| Tables_in_soccer_stats |+------------------------+| stats                  |+------------------------+
Going a step further, we can see some information about our newly created table:
DESCRIBE stats;+---------+---------+------+-----+---------+----------------+| Field   | Type    | Null | Key | Default | Extra          |+---------+---------+------+-----+---------+----------------+| id      | int(11) | NO   | PRI | NULL    | auto_increment || name    | text    | YES  |     | NULL    |                || goals   | int(11) | YES  |     | NULL    |                || assists | int(11) | YES  |     | NULL    |                || points  | int(11) | YES  |     | NULL    |                || shots   | int(11) | YES  |     | NULL    |                |+---------+---------+------+-----+---------+----------------+
Now we’ll starting populating our table with soccer stats data!
Remember how I mentioned that SQL is very human-readable? Inserting data into our table can be done using a command called INSERT followed by the table name and a simple tuple of the data we would like to insert.
INSERT INTO stats VALUES (NULL, "John", 26, 22, 26+22, 104);INSERT INTO stats VALUES (NULL, "Jessica", 126, 82, 126+82, 312);INSERT INTO stats VALUES (NULL, "Nick", 8, 25, 8+25, 51);INSERT INTO stats VALUES (NULL, "Mike", 52, 12, 52+12, 174);INSERT INTO stats VALUES (NULL, "Katie", 26, 92, 26+92, 188);
A couple of things to keep in mind when inserting data:
We can set the id as NULL since it will auto increment (as explained in the previous section)
Be sure that your data types of the data you are inserting match those of the table you have defined
To display all of the data in your table, simply run:
SELECT * FROM stats;+----+---------+-------+---------+--------+-------+| id | name    | goals | assists | points | shots |+----+---------+-------+---------+--------+-------+|  1 | John    |    26 |      22 |     48 |   104 ||  2 | Jessica |   126 |      82 |    208 |   312 ||  3 | Nick    |     8 |      25 |     33 |    31 ||  4 | Mike    |    52 |      12 |     64 |   174 ||  5 | Katie   |    26 |      92 |    118 |    88 |+----+---------+-------+---------+--------+-------+
There’s all our soccer stats.
Notice how “SELECT *” grabs everything from our table. If you only wanted the name and number of goals, you can instead specify which columns you wish to retrieve:
SELECT name,goals FROM stats;+---------+-------+| name    | goals |+---------+-------+| John    |    26 || Jessica |   126 || Nick    |     8 || Mike    |    52 || Katie   |    26 |+---------+-------+
To update a row in our table we can use..... you guessed it, the UPDATE command!
UPDATE stats SET goals=28 WHERE name="John";SELECT * FROM stats;+----+---------+-------+---------+--------+-------+| id | name    | goals | assists | points | shots |+----+---------+-------+---------+--------+-------+|  1 | John    |    28 |      22 |     48 |   104 ||  2 | Jessica |   126 |      82 |    208 |   312 ||  3 | Nick    |     8 |      25 |     33 |    31 ||  4 | Mike    |    52 |      12 |     64 |   174 ||  5 | Katie   |    26 |      92 |    118 |    88 |+----+---------+-------+---------+--------+-------+
In the above query:
UPDATE allows us to select the table we would like to update the data for
SET performs the actual updating
WHERE defines which data cell will receive that particular update
Now that we’ve populated our table with data, we’ll learn how to actually retrieve it. This will be where the bulk of SQL work will be for those working in Data Science or Machine Learning — retrieving data to prepare it for exploration and training.
You’ve already done a query in the last section using the SELECT command. This time we’ll run a few more complicated queries.
We can limit the number of rows we retrieve:
SELECT * FROM stats LIMIT 3;+----+---------+-------+---------+--------+-------+| id | name    | goals | assists | points | shots |+----+---------+-------+---------+--------+-------+|  1 | John    |    28 |      22 |     48 |   104 ||  2 | Jessica |   126 |      82 |    208 |   312 ||  3 | Nick    |     8 |      25 |     33 |    31 |+----+---------+-------+---------+--------+-------+
Or order our output by the number of goals a player scored:
SELECT * FROM stats ORDER BY goals DESC;+----+---------+-------+---------+--------+-------+| id | name    | goals | assists | points | shots |+----+---------+-------+---------+--------+-------+|  2 | Jessica |   126 |      82 |    208 |   312 ||  4 | Mike    |    52 |      12 |     64 |   174 ||  1 | John    |    28 |      22 |     48 |   104 ||  5 | Katie   |    26 |      92 |    118 |    88 ||  3 | Nick    |     8 |      25 |     33 |    31 |+----+---------+-------+---------+--------+-------+
We can combine our SELECT statement with the WHERE command to create a type of if-else query for retrieving data:
SELECT * FROM stats WHERE goals > 20 and assists > 50;+----+---------+-------+---------+--------+-------+| id | name    | goals | assists | points | shots |+----+---------+-------+---------+--------+-------+|  2 | Jessica |   126 |      82 |    208 |   312 ||  5 | Katie   |    26 |      92 |    118 |    88 |+----+---------+-------+---------+--------+-------+
MySQL even provides us with the ability to perform calculations. Check out how we can compute the shooting percentage of each player and sort the table by those numbers:
SELECT *, points/shots FROM stats ORDER BY points / shots DESC;+----+---------+-------+---------+--------+-------+--------------+| id | name    | goals | assists | points | shots | points/shots |+----+---------+-------+---------+--------+-------+--------------+|  2 | Jessica |   126 |      82 |    208 |   312 |       0.6667 ||  3 | Nick    |     8 |      25 |     33 |    51 |       0.6471 ||  5 | Katie   |    26 |      92 |    118 |   188 |       0.6277 ||  1 | John    |    28 |      22 |     48 |   104 |       0.4615 ||  4 | Mike    |    52 |      12 |     64 |   174 |       0.3678 |+----+---------+-------+---------+--------+-------+--------------+
That concludes our easy introduction to SQL!
If you’d like to practice using SQL queries, I’d highly recommend playing around with the HackerRank challenges which already provide pre-built databases and tables for query practice.
Follow me on twitter where I post all about the latest and greatest AI, Technology, and Science! Connect with me on LinkedIn too!