SQL Tutorial Part 2: Keywords
In this post we will cover the four main keywords that you need to know in order to write basic queries (they are listed below). By the end of the article, you will able to write your own simple queries!
Before we start, we should re-state the main reason we use SQL in the first place. Most of the time, it is to reduce the amount of data we see. For example: if our candle sales table from the previous post contains 50 columns and 20 million rows, it contains more data than we can use directly. All of that data is useful because it can be used to answer a variety of questions, but most of the time we only need to answer one very specific question, so we only need a small portion of the data.
We use SQL to limit the data we see to exactly what we need to answer that question. For example, if the sales table referenced above contains sales data for the past twenty years but we only need to know about sales made on June 5, 2017, we probably only need a handful of those 20 million rows. We can use SQL to limit the rows we output to only those that have an entry of June 5, 2017 in the date column to create a smaller, more manageable data-set that can answer our question.
The keywords referenced above are exactly how we do that. They are:
- SELECT
- FROM
- WHERE
- GROUP BY
Now, how do they work?
Note: I have explained the keywords in the order in which they must be listed for your query to run, starting with SELECT. However, most of the time you will choose the table with the FROM keyword before you choose the columns with the SELECT keyword. This is because you need to know which table you’re using before you can decide which columns you want to use from that table (databases and tables are covered in more detail below). Even if you start by writing out the FROM keyword first, as I normally do, the keywords must still appear in the normal order: SELECT, FROM, WHERE, GROUP BY (you will start from the middle with FROM, then write SELECT above it and WHERE and GROUP BY below it).
The Keywords:
SELECT
What it does: Chooses which columns you see (and do not see).
How to use It: List the names of the columns you want to see after the keyword, separated by commas. Leave out the names of the columns you do not need to see. For example: SELECT date, size will return only the date and size columns of the Sales table (table selection is explained below in the FROM section) and ignore the price column.

FROM
What it does: Chooses the table the data will come from.
How to use It: Write the name of the table after the keyword. For example: FROM Sales will specify that you want to use data from the Sales table.
Note: Most databases (databases are data repositories that you communicate with using SQL) store data in tables, and those databases often contain multiple tables. The FROM keyword specifies which of those tables you want to use data from.

WHERE
What it Does: Chooses which rows you see (and do not see).
How to use It: List the conditions used to choose rows after the keyword. Include the word AND before any condition after the first one. These conditions will reference one or more columns, and will use the data in those columns to identify rows to return. For example: WHERE size = ‘medium’ will return only rows where there is a ‘medium’ entry in the size column. Any rows that do not match the criteria will be ignored.

GROUP BY
What it Does: Determines whether or not to show duplicate rows. GROUP BY is a bit more complicated than the other keywords, so I will elaborate on it in more detail later. For now, you can think of GROUP BY as a way to check the output for any rows that are exact matches and ignore the duplicates (technically, collapse the duplicates – but you can think of it as ignoring them for now) so each unique row only appears once.
How to use It: This is a bit of an oversimplification, but for now plan to use GROUP BY in one of two ways: (1) List all of the column names you put in the SELECT statement in the GROUP BY statement if you want to ignore duplicate rows. For example: you can use GROUP BY date, size as shown in the example below to ignore row 4, which is a duplicate of 3; or (2) leave out the GROUP BY statement entirely if you do not want to ignore duplicate rows.

And now, after all of these transformations, the final output of our query!

With these four keywords under your belt, you now understand the most important and fundamental concepts of SQL. Congratulations!
We will continue to elaborate on these concepts in more detail in later posts.