Learn Beginner SQL in 5 steps in 5 minutes! | by Terence Shin | Towards Data Science

So you want to learn SQL? Great, you should!
In this article, I’m going to explain to you how to query with SQL in the easiest way possible. But first, let me define a couple of terms...
If this is the kind of stuff that you like, be one of the FIRST to subscribe to my new YouTube channel here! While there aren’t any videos yet, I’ll be sharing lots of amazing content like this but in video form. Thanks for your support :)
A row, also called a record, is a collection of attributes (variables) that represent a single entity. For example, one row may represent one hospital patient and may have attributes/variables like age, weight, height, etc...
A table is a collection of rows with the same attributes (with the same variables). What helps me the most is to think of a table as an Excel table.
A query is a request for data from a database table or combination of tables. Using the table above, I would write a query if I wanted to find all patients that were older than 23 years old.
Since this is a tutorial for beginners, I’m going to show you how to write a query if you wanted to extract data from one table.
There are five components to a basic query:
SELECT (mandatory)FROM (mandatory)WHERE (optional)GROUP BY (optional)ORDER BY (optional)
SELECT (mandatory)
FROM (mandatory)
WHERE (optional)
GROUP BY (optional)
ORDER BY (optional)
The structure is as follows:
SELECT   [column_name_1],   [column_name_2],   [column_name_n]FROM   [table_name]WHERE   [condition 1]GROUP BY   [column_name] ORDER BY   [column_name]
Let’s bring back my example as a reference:
SELECT determines which columns you want to pull from a given table. For example, if I wanted to pull Name then my code would look like:
SELECT Name
A neat trick is if you want to pull all columns, you can use an asterisk — see below:
SELECT *
FROM determines which table you want to pull the information from. For example, if you wanted to pull the Name of the patient, you would want to pull the data FROM the table called patient_info (see above). The code would look something like this:
SELECT   NameFROM   patient_info
And there’s your first functional query! Let's go through the 3 additional optional steps.
What if you wanted to select the Names of patients who are older than 23? This is when WHERE comes in. WHERE is a statement used to filter your table, the same way you would use the filter tool in Excel!
The code to get the Names of patients who are older than 23 is to the left. A visual representation is shown to the right:
If you want the Names of patients that satisfy two clauses, you can use AND. Eg. Find the Names of patients who are older than 23 and weigh more than 130 lbs.
SELECT   NameFROM   patient_infoWHERE   Age > 23   AND   Weight_lbs > 130
If you want the Names of patients that satisfy one of two clauses, you can use OR. Eg. Find the Names of patients who are younger than 22 or older than 23.
SELECT   NameFROM   patient_infoWHERE   Age < 22   OR   Age > 23
GROUP BY does what it says — it groups rows that have the same values into summary rows. It is typically used with aggregate functions like COUNT, MIN, MAX, SUM, AVG.
Let's use the example below:
If we wanted to get the number of hospital visits for each patient, we could use the code below and get the following result:
ORDER BY allows you to sort your results based on a particular attribute or a number of attributes in ascending or descending order. Let’s show an example.
SELECT   *FROM   patient_infoORDER BY   Age asc
‘ORDER BY Age asc’ means that your result set will order the rows by age in ascending order (see the left table in the image above). If you want to order it in descending order (right table in the image above), you would replace asc with desc.
And that’s how you construct a query! You’ve just learned one of the most in-demand skills in the tech world. I’ll provide some links below where you can practice your SQL skills. Enjoy!
If you like my work and want to support me...
Be one of the FIRST to subscribe to my new YouTube channel here! While there aren’t any videos yet, I’ll be sharing lots of amazing content like this but in video form.
Also be one of the first to follow me on Twitter here.
Follow me on LinkedIn here.
Sign up on my email list here.
Check out my website, terenceshin.com.