SQL for Business

Introduction to SQL

Overview

Teaching: 20 min
Exercises: 5 min
Questions
  • What is a relational database and why should I use it?

  • What is SQL?

Objectives
  • Limitations of excel

  • What is SQL

  • What is relational database and Database Management Systems (DBMS)

  • Describe why relational databases are useful

Setup

Note: this should have been done by participants before the start of the workshop.

See Setup for instructions on how to download the data, and also how to install jupyter notebook

What is SQL?

SQL stands for Structured Query Language. SQL allows us to interact with relational databases through queries. These queries can allow you to perform a number of actions such as: insert, update and delete information in a database.

Questions

Think about this situation

Imagine if you are a owner of a convenience store, and you are trying to record your soda purchases.
In each invoice, it contains the following information:
Invoice id, Date, Category, Soda name, Volume, Cost, Retail Price, Vendor, Vendor phone number, Number of bottle purchased
How would you store the data?

Traditional File Approach

If you store all these invoice information in one Excel file, What problem could raise from this approach?

Goals

To sum up, these are frequent used data operations:

In addition, we don’t want to do this manually! Instead of searching for the right pieces of data ourselves, or clicking between spreadsheets, or manually sorting columns, we want to make the computer do the work.

In particular, we want to use a tool where it’s easy to repeat our analysis in case our data changes. We also want to do all this searching without actually modifying our source data.

Putting our data into a relational database and using SQL will help us achieve these goals.

Databases

Definition: Relational Database

A relational database stores data in relations made up of records with fields. The relations are usually represented as tables; each record is usually shown as a row, and the fields as columns. In most cases, each record will have a unique identifier, called a key, which is stored as one of its fields. Records may also contain keys that refer to records in other tables, which enables us to combine information from two or more sources.
db

Why use relational databases

Using a relational database serves several purposes.

Database Management Systems (DBMS)

There are a number of different database management systems for working with relational data. We’re going to use SQLite today, but basically everything we teach you will apply to the other database systems as well (e.g. MySQL, PostgreSQL, MS Access, MS SQL Server, Oracle Database and Filemaker Pro). The only things that will differ are the details of exactly how to import and export data and the data types.

Look at the popularity of database
alt text
Top 4 are all Relational Database Management Systems (RDBMS). More and more companies choose to use relational database.

Database Design

To summarize:

Key Points