How to select data from a table using SQL | by Kate Marie Lewis | Towards Data Science

Whilst self isolating at home, my friends and I have decided to work together as a group learning data science. We have had one lesson so far and I have been so pleased by how enthusiastic the group is. By asking questions, my friends helped me to be clearer in my explanations and assisted each other learning. Hopefully, we can maintain this momentum and keep developing new data science skills together.
If you would like to join us in learning data science during this global pandemic, please feel free to follow along. You may like to go back to the previous lesson so that you can start from the beginning. Otherwise, if you are interested specifically in learning how to select data from tables using SQL then you have come to the right place.
In the previous lesson we learned how to create a table and to put some data into it using SQL. This was done using the CREATE TABLE and INSERT INTO keywords. For a bit of fun, we used the children’s show Bluey in the example.
Now that we know how to create a table of data from our first lesson, let’s learn how to access it.
Using the SQL keyword SELECT you can access all the data in your table, specific columns or a specific number of records. We will go through each of these three scenarios in this lesson. In addition, we will practice how to select data without including duplicate values using the SELECT DISTINCT keyword.
In contrast to the children’s show used as an example in the previous lesson, this week we will use The Walking Dead. Certainly not one for the kids 😉
In the first season of The Walking Dead, there were some good guys and some bad guys and some others in between. But what we want to find out the top 5 characters who are the deadliest of them all.
To figure out who is the best at killing we need to figure out what criteria we want to use. The characters in The Walking Dead have dispatched many ‘walkers’, but quite a lot of them have also killed several humans.
We will define the deadliest characters as the one that has killed the most zombies. Then if there is a tie for zombie kills, the person who has killed the most humans will be hailed as the deadliest character out of the two.
We have a table that contains information about characters from The Walking Dead that we will use to solve our problem. The dataset is primarily from this article. I changed some of the numbers slightly to illustrate one of the points of the lesson.
So now we need to work out how to select the correct data from the table to find out who is the deadliest.
Like we saw in the first lesson, you can use an asterisk to indicate that you want to select all the data in the table. All of the available columns in the table will be returned when the query is run.
SELECT    *FROM     name_of_table;
This is one of the simplest queries that you can write.
Indentation does not do anything in SQL. I just do it to make my code easier to read. Generally speaking, you should conform to the indentation rules set forth in your organisation’s style guide. However, we don’t have a style guide so you can organise your code whichever way pleases you. It is important to know that in other coding languages like Python indentations are functional. If you do not use the correct indentations in Python your code will throw an error.
If you want to select only specific columns from a larger table you can list the names of the columns that you want to be returned from the query.
SELECT     name_column_one,     name_column_two,     name_column_fiveFROM    name_of_table;
This is very useful, particularly for very wide datasets where there are many more columns than can be easily visualised in the resulting table. For example if you were looking at census data but are only interested in a few of the many available columns, it is much easier to see the values you want if you only select those columns of interest.
Question 1: Can you tell how many columns are in the above table?
The keyword LIMIT can be used to select a specific number of records. When LIMIT is used, only the specified number of records are returned starting from the top of the table. This is particularly helpful if you have your data in a particular order.
If you would like to order your data by a column of interest you can use the ORDER BY keyword. ORDER BY is put after the FROM statement. To use ORDER BY, you need to specify which column(s) you want to use to dictate the order the data is returned in.
You may also decide if you want it to be ordered in an ascending or descending order by using ASC or DESC respectively.
SELECT     *FROM    name_of_table ORDER BY    name_column_one DESCLIMIT 10;
The code above will select all of the columns from the table and will return the top 10 records. This is after they have been put in descending ordered according to the values in column one.
The syntax to SELECT and SELECT DISTINCT is the same. The difference will be in the results it returns.
SELECT DISTINCT    name_column_oneFROM    name_of_table;
SELECT DISTINCT returns only the different values from the columns selected and so all duplicates are removed. That is easy to understand if you are only selecting one column. However you can also use SELECT DISTINCT to select unique combinations of columns.
SELECT DISTINCT    name_column_one,    name_column_fiveFROM    name_of_table;
So using the code above, you would get distinct combinations of the values in column one and column five. Even if there are duplicate values in column one, if there are different values for column five in those same rows, both rows would be returned.
go to http://sqlfiddle.com/ (or you can use https://www.db-fiddle.com/ as I have found SQL fiddle has had some downtime recently)In the left hand box put the CREATE TABLE and INSERT INTO queries below
go to http://sqlfiddle.com/ (or you can use https://www.db-fiddle.com/ as I have found SQL fiddle has had some downtime recently)
In the left hand box put the CREATE TABLE and INSERT INTO queries below
CREATE TABLE walking_dead(    first_name varchar(255),    last_name varchar(255),    hair_colour varchar(255),    walker_kills int(255),    human_kills int(255),    number_of_hands int(255));INSERT INTO walking_dead (first_name, last_name, hair_colour, walker_kills, human_kills, number_of_hands)VALUES    ('Maggie', 'Greene', 'brown', 64, 5, 2),    ('Carol', 'Peletier', 'grey', 57, 27, 2),    ('Merle', 'Dixon', 'dark blonde', 20, 6, 1),    ('Tyreese', 'Williams', 'black', 59, 0, 2),    ('Morgan', 'Jones', 'black', 40, 3, 2),    ('Glenn', 'Rhee', 'black', 185, 5, 2),    ('Michonne', '', 'black', 141, 10, 2),    ('Andrea', 'Harrison', 'blonde', 39, 0, 2),    ('Sasha', 'Williams', 'black', 73, 4, 2),    ('Abraham', 'Ford', 'red', 49, 4, 2),    ('Shane', 'Walsh', 'brown', 28, 3, 2),    ('Daryl', 'Dixon', 'brown', 185, 22, 2),    ('Rick', 'Grimes', 'brown', 216, 35, 2),    ('Philip “The Governor”', 'Blake', 'brown', 26, 30, 2);
3. Click the build schema button
4. In the right hand box put your queries
5. Run the query below and see if it returns what you would expect it to:
SELECT    hair_colour,     number_of_handsFROM    walking_deadORDER BY    hair_colour ASC, number_of_hands DESC;
Hint: Notice the order that the columns are arranged in and that there are duplicate values in the results
6. Run the query below and see if it returns what you would expect it to:
SELECT DISTINCT    hair_colour,     number_of_handsFROM    walking_deadORDER BY    hair_colour ASC, number_of_hands DESC;
Hint: Notice how adding the DISTINCT keyword changes the results that get returned
7. Run the query below and see if it returns what you would expect it to:
SELECT    first_name,     human_killsFROM    walking_deadORDER BY    human_kills DESCLIMIT 10;
Hint: Notice the order and number of records returned.
8. Run the query below and see if it returns what you would expect it to:
SELECT    first_name,     human_killsFROM    walking_deadORDER BY    first_name ASCLIMIT 10;
Hint: Notice the difference in results returned compared with the previous query.
Exercise 1: Write a query to select all the data from this table
Exercise 2: Write a query to create a list of all the different family names in the dataset
Exercise 3: Write a query to create a list of all the different family name, hair colour combinations in this dataset
Exercise 4: Select the full name, walker_kills and human_kills of the top 5 most deadly characters. This is based on the criteria that zombie kills are most important, followed by human kills.
There is at least one big fan of The Walking Dead in our study group. She was thrilled to get a zombie based lesson 😜
Some members of the group are running a little behind our initially planned timeline and are still working on this lesson. But what can you do when it is school holidays and you have three kids home to take care of? Or if you are working on getting your company and employees through this financial downturn. The women working with me to learn data science have plenty of other things going on in their lives so I totally understand that they can sometimes get in the way.
Having said that, everyone still seems enthusiastic to complete this lesson when they can. My mates who have been able to complete the lesson this week have done really well. They seem to be grasping the basics of selecting from tables really well. I can’t wait to see how they go when we move onto some of the slightly more complex SQL methods.
In the next lesson, we will focus on filtering data. There are lots of different ways to filter tables in SQL, but we will focus on learning to use the keywords WHERE, AND, OR, LIKE, IN, BETWEEN and NOT.
In addition to data, my other passion is painting. You can find my wildlife art at www.katemarielewis.com