Depending on your company’s size and the maturity of your product, you might have a wealth of data and data analysts you can depend on. Even with that, one of the best ways (I know) to understand a product is to know how data flows. This is where a little SQL is valuable. It’s not a skill you’ll find yourself using day-in day-out. But when you need it, and you have it, you’ll be thankful that you do.

Instructions: This will be a very handson post. We will be using this online SQL editor which has some data in it.

Basic SQL Syntax

  1. Selecting columns from a table

The “SELECT” keyword tells the database which columns you want to select “FROM” a database table.

I want to select all the items from the orders table

The astrick sign ("*") indicates that you want to select all the columns.

Query: Selecting all items Output: Selecting all items

I want to select all the items and their prices from the orders table

Instead of selecting all columns by using the ("") sign, you can speficy which columns you want to select. You do that by replacing the “” sign with each column name, separated by a comma sign (",").

Query: Selecting specific items Output: Selecting specific items
  1. Applying addtional conditions

When you have a million rows of records, you will need to use conditional statements to filter for what you want to select. You will need to use logical operators for filtering. Every logical operator begings with the “WHERE” clause, followed by the operator.

Some of the common operators are:

Operator Description
= Equals to. Example: WHERE amount = 400
!= Not equals to. Example: WHERE country != USA
> Greater than. Example: WHERE amount > 200
< Less than. Example: WHERE amount < 300
>= Greater than or equals to. Example: WHERE age >= 22
<= Less than or equals to. Example: WHERE age <= 25
BETWEEN Between two values. Example: WHERE age BETWEEN 22 AND 25
LIKE To find pattern. The % sign is a wildcard Example: WHERE item LIKE 'Mo%'
IN To specifiy multiple possible values. Example: `WHERE country IN (‘USA’, ‘UK’) |

TBD!!

  1. Retrieving values in specific order

  2. Renameing results using aliases

  3. Joining multiple tables

  4. Writing functions for code re-usability