SQL Tutorial


If you work in a business that involves data analysis, SQL is an extremely valuable tool. In this tutorial, we'll cover the basics of SQL: What it is, how it works and when you should use it.

Section 1: Introduction

SQL is a language that allows you to interact with database systems. It's used for managing data, and it's arguably the most popular database language in use today. SQL can be used for simple tasks like retrieving information from databases or creating new records, but it also has powerful features that allow users to do more advanced things like insert large amounts of data at once (aka "batch") or build complex queries based on complex conditions (known as "conditional statements").

For example: You might want to find all the customers who have purchased more than $500 worth of products within the last six months by month (January through June). You could do this with SQL by writing something like this:


Which would return all customer IDs associated with those who have purchased more than $500 worth of goods during each month over the past six months—or equivalently if you wanted only those customers whose names matched one specific pattern!

Section 2: The SELECT Statement

It can be used to retrieve one or more columns from a table, or it can be used to retrieve all columns from a table.

The SELECT statement has three parts:

Section 3: The WHERE Clause

The WHERE clause is used to filter data. It can be used in conjunction with the SELECT statement, or it can stand alone as an independent clause that returns no rows.

The WHERE clause has two parts: a condition and an operator (a comparison operator). For example, if you wanted to find all orders that were placed in February and had a total value over $1000 USD, this query would look like this:

SELECT * FROM Orders WHERE Year(OrderDate) = '2016' AND TotalPrice > 1000

Section 4: Sorting Results

You can also specify a column by which you want to sort the results, even if it isn't included in the SELECT list.

You can use multiple columns as part of an ORDER BY clause; however, they must be listed within parentheses (e.g., "Name, Type"). In addition, there are some restrictions on how many levels deep you can go with nested subqueries: only one level is allowed (i.e., no nesting).

Section 5: Filtering with the HAVING clause

In the previous example, we used the GROUP BY clause to group our data by column. We then used another HAVING clause to filter out any records that did not have a value in the column 'col1' and displayed them on their own line.

In this section, I'll go over how you can use a HAVING clause to filter your results based on conditions such as: