SQL Bootcamp 2021 & Advanced Queries
Description
Databases are systems that allow users to store and organize data and are useful when dealing with large amounts of data. On the other hand, spreadsheets are suitable for one-time analysis, quick charts, reasonable datasets, and allowing untrained people to work with data.
Databases are suitable for data integrity, handling large amounts of data, combining different datasets quickly, automating reuse steps, and supporting data for websites and applications.
A database is a collection of tables. Tables contain rows and columns, where the rows are known as records and the columns are known as fields. A column is a set of data values of a particular type, one value for each row of the database. A row represents a single data item in a table, and every row in the table has the same structure.
Table of Contents
1. Overview
Overview of the course curriculum and challenges.
1.1. Course Curriculum
The course is divided in the following sections...
- Section 1
- Databses and Table Basics
- SQL Statement Fundamentals
- GROUP BY Clause
- Assessment Test 1
- Section 2
- JOINS
- Advanced SQL
- Commands
- Assessment Test 2
- Section 3
- Create Database and Tables
- Assessment Test 3
- Views
- PostgreSQL with Python
Typical database users...
- Analyst
- Technical
- Data Scientist
- Software Engineers
- Web Developers
Database Platform Options:
- PostgreSQL (focus of the course)
- Free (Open Source)
- Widely used on internet
- Multi platform
- MySQL & MariaSQL
- Free (Open Source)
- Widely used on internet
- Multi platform
- MS SQL Server Express
- Free, but with some limitations
- Compatible with SQL Server
- Windows only (-)
- Microsoft Access
- Cost (-)
- Not easy to use just SQL (-)
- SQLite
- Free (Open Source)
- Mainly command line (-)
SQL is the programming language used to communicate with our database. Example:
select customer_id, first_name, last_name
from sales
order by first_name;
-- the ; at the end of the query is optional is pgSQL
1.2. Challenges
Challenges are based on the scenario that we've just been hired as a SQL consultant for a DVD Rental Store. Challenges increase in difficult over the course.
Challenge structure...
- Business Situation
- Challenge Question
- Expected Answer
- Hints
- Solution
2. Setup
We install and use the following applications.
- PostgreSQL: SQL Engine that stores data and reads queries and returns information
- PgAdmin: graphical User Interface for connecting with PostgreSQL
- Restore the provided database using the
.tar file in the repo here.
- VS Code: source-code editor. We use VS Code as a scratch pad and for general note taking.
Go to Servers > PostgreSQL 12 > Databases > dvdrental > Schemas > Tables to see the tables in the restored database.
3. SQL Statement Fundamentals
We retrieve information from tables with queries. We construct queries with statements.
3.1. SELECT Statement
The select statement is used to select data from a database. The data returned is stored in a result table, called the result-set.
The syntax we learn can be applied to any major type of SQL database. select is the most common statement used, and it allows us to retrieve information from a table. Later on we will learn how to combine select with other statements to perform more complex queries.
-- select columns c1,...,cn from table t1
select c1,...,cn from t1
-- select all columns from table t1, i.e. select the whole table
select * from t1
-- note that it is bad practice to use * if we don't need all columns (increased traffic between database server and app -> slows down retrieval of results)
Challenge: we want to send out a promotional email to our existing customers. Grab the first and last names of every customer and their email address.
3.2. DISTINCT Keyword
The distinct / distinct() keyword can be used to return only the distinct values in a column.
-- the DISTINCT keyword operates on a column
select distinct c from t1
-- we can add parenthesis for clarity. When adding more calls together, the parenthesis will be necessary
select distinct(c) from t1
Challenge: retrieve the distinct rating types out films could have in our database.
3.3. COUNT Function
The count() function returns the number of input rows that match a specific condition of a query. We can apply count() on a specific column or just pass count(*), this should return the same result. It simply returns the number of rows in the table, regardless of the column we call.
select count(c1) from t1
-- it's useful to use a column name to contextualize the query (what question were we trying to answer?)
count() is more useful when combined with other commands, e.g. distinct(). We can answer questions like, how many unique names are there in the table?
select count(distinct(c1)) from t1
-- we're calling count on the result of distinct name
3.4. WHERE Clause
The where clause is used to filter records. It is used to extract only those records that fulfill a specified condition.
select and where are the most fundamental SQL statements. The where statement allows us to specify conditions on columns for the rows to be returned. Basic syntax:
select c1,...,cn from t1
where conditions
The where clause appears immediately after the from clause of the select statement. The conditions are used to filter the rows returned from the select statement. There are a variety of standard operators to construct the conditions.
- Comparison operators: compare a column value to something.
- Logical operators: allow us to combine multiple comparison operators.
Example:
select count(title) from film
where rental_rate >= 4 and replacement_cost >= 19.99 and rating = 'R'
-- condition on rental_rate, replacement_cost and rating
-- note that the COUNT function does not need a specific column, as it just counts the number of records expected to be returned by the SELECT statement
Challenge: from now on we will focus more on directly asking the business related questions, to more realistically model a typical task. Find the email for the customer with the name Nancy Thomas. Solution:
-- task: find the email for the customer with the name Nancy Thomas
-- return the columns of the customer table
-- select * from customer where 1=0
select email, first_name, last_name from customer
where first_name = 'Nancy' and last_name = 'Thomas'
Challenge: what is the movie Outlaw Hanky about? Solution:
-- select * from film where 1=0
select description from film
where title = 'Outlaw Hanky'
Challenge: get the phone number for the customer who lives at 259 Ipoh Drive.
-- select * from address where 1=0
select phone from "address"
where address='259 Ipoh Drive'
3.5. ORDER BY Clause
We can use order by to sort rows based on a column value, in either ascending or descending order. Basic syntax:
select c1, c2 from t1
order by c1 asc / desc c2 asc / desc
-- ORDER BY uses ASC by default
Notice order by