Skip to main content

Course Resources

Editorial

Introduction

This page gathers what you will reach for during the course:

  • documentation for SQLite and for other databases
  • the course's dataset: its tables and how they connect
  • how to study, and common mistakes
  • what to do when a query fails
  • a cheat sheet of clauses, functions and operators

Official Database Documentation

SQLite

The queries in this course run on SQLite. Its documentation is the reference for whatever a lesson leaves out:

Other Databases

What you learn here carries over to other databases, and their documentation helps once you work with them:

DatabaseDocumentation
PostgreSQLThe SQL Language
MySQLMySQL Documentation
Microsoft SQL ServerTransact-SQL Reference
Oracle DatabaseDatabase Documentation

One Standard, Many Dialects

SQL is an international standard, ISO/IEC 9075, revised from time to time; its latest version is SQL:2023. Each database follows much of it, adds features of its own and leaves some out. PostgreSQL's documentation notes that "at the time of writing, no current version of any database management system claims full conformance to Core SQL:2023". So SELECT with WHERE, GROUP BY and ORDER BY reads the same everywhere, and where SQLite does something in its own way, the lessons say so.

Practice Database: Brazilian E-Commerce Dataset

About the Dataset

The Brazilian E-Commerce Public Dataset holds real orders placed through Olist, a Brazilian company that sells small shops' products on online marketplaces. On CodePeet it has 99,441 orders, placed from 4 September 2016 to 17 October 2018, in 9 tables. It has the gaps real data has: values that are missing, the same city written two ways, and 2 product categories with no English name.

Available Tables

TableRowsOne row is
olist_customers_dataset99,441the customer of one order: city, state, zip code prefix
olist_orders_dataset99,441an order: its status, its times, its customer
olist_order_items_dataset112,650one item of an order: product, seller, price, freight
olist_order_payments_dataset103,886one payment of an order: type, installments, amount
olist_order_reviews_dataset100,000a review of an order: score, comment
olist_products_dataset32,951a product: category, size, weight
olist_sellers_dataset3,095a seller: city, state, zip code prefix
olist_geolocation_dataset1,000,163a location for a zip code prefix: latitude, longitude, city
product_category_name_translation71a category's name in Portuguese and in English

Why Real Data?

  1. It has gaps. Some values are missing and some are written two ways; you learn to find both.
  2. Its tables are connected. Many questions need two or more of them.
  3. It has a business behind it. Each query answers something a shop would ask.
  4. It has size. About a hundred thousand orders, and a million rows of locations.

Table Relationships Overview

Key Relationships

Each link is a column two tables share, and a join matches rows on it:

TablesJoined onWhat matches
orders and customerscustomer_ideach order has its own customer row, 99,441 of each
order_items and ordersorder_id112,650 items in 98,666 orders; 775 orders have none
order_payments and ordersorder_id103,886 payments; 1 order has none
order_reviews and ordersorder_idevery order has a review, and 555 have more than one
order_items and productsproduct_idevery item names one product
order_items and sellersseller_idevery item names one seller
products and the translationproduct_category_name71 of the 73 named categories have an English name
customers or sellers and geolocationthe zip code prefixnot a key: 52.6 location rows a prefix on average, so the join repeats rows

The SQL Joins section of this course shows how to follow these links. In the diagram, each line is one of them:

Nine tables, joined by their keys: the entity relationship diagram of the Brazilian E-Commerce dataset. Each table is a card with its full name and its main columns; a gold key marks a key column, blue links a column that points at another table. Each line is a join, with two bars on the side where one row matches and a fork on the side where many rows do, and a circle where there may be none. olist_orders_dataset (order_id key; customer_id, order_status, order_purchase_timestamp) sits in the middle. Its customer_id matches exactly one row of olist_customers_dataset (customer_id key; customer_unique_id, customer_zip_code_prefix, customer_city, customer_state), and each customer row belongs to exactly one order. Many rows of olist_order_reviews_dataset (review_id, order_id, review_score), of olist_order_payments_dataset (key order_id and payment_sequential; payment_type, payment_value) and of olist_order_items_dataset (key order_id and order_item_id; product_id, seller_id, price) point at one order. Each item points at one row of olist_products_dataset (product_id key; product_category_name, product_weight_g) and one of olist_sellers_dataset (seller_id key; seller_zip_code_prefix, seller_city, seller_state). A product's category name finds zero or one row of product_category_name_translation (product_category_name key; product_category_name_english). Dashed lines labelled zip code prefix tie the customers and the sellers to olist_geolocation_dataset (geolocation_zip_code_prefix, geolocation_lat, geolocation_lng, geolocation_city): shared values, but no key.

Tips for Effective Learning

Active Practice

Do thisNot this
Run every exampleOnly read the queries
Change a query and run it againCopy it without knowing what each part does
Try each exercise before you run its worked queryRun the worked query first
Build a long query one part at a timeTake in the whole thing at once

The Order a Query Runs In

You write SELECT first, but the database works in another order. SQLite's documentation and PostgreSQL's describe the same steps:

  1. FROM takes the rows of the tables.
  2. WHERE keeps the rows that pass its test.
  3. GROUP BY folds the rows into groups and works out the aggregates.
  4. HAVING keeps the groups that pass its test.
  5. SELECT works out the output columns.
  6. ORDER BY sorts the rows. Without it, in SQLite's words, "the order in which the rows are returned is undefined".
  7. LIMIT keeps the first ones.

Coding Order vs Execution Order goes through this step by step.

Common Beginner Mistakes

MistakeWhat happensWhat to write
A plain column beside COUNT, with no GROUP BYSQLite returns one row, and the plain column's value comes from "one of the input rows that form the aggregate": you cannot tell which. PostgreSQL refuses the query.Group by the plain column
A condition on COUNT in WHEREWHERE runs before the groups exist, so the query failsPut it in HAVING
Text without quotescustomer_state = SP looks for a column named SP: no such column: SPcustomer_state = 'SP'
== for equalsSQLite accepts it; PostgreSQL answers operator does not exist: text == unknownOne =

Getting Help

When a Query Fails

1. Read the error. The page shows the first line of SQLite's message, and it usually names the problem. Three you will meet often, as the page shows them:

  • no such column: customer_name: the table has no column of that name. Check its spelling, and which table you are reading.
  • near "FROM": syntax error: SQLite stopped at the word in quotes. Look just before it: here, a comma before FROM.
  • ambiguous column name: customer_id: two of the joined tables have that column. Say which one: o.customer_id.

2. Build it up. Start with SELECT and FROM and run it; add WHERE and run it again; then GROUP BY, and so on. The step that breaks the query is the one to look at.

3. Look at the data. A table's first rows:

SELECT * FROM olist_orders_dataset LIMIT 5;

And the values a column holds:

SELECT DISTINCT order_status FROM olist_orders_dataset ORDER BY order_status;

This one returns the 8 statuses an order has in this data: approved, canceled, created, delivered, invoiced, processing, shipped and unavailable.

4. Compare with a working example. Find the example closest to what you want in a lesson, and change it one part at a time.

Quick Reference Cheat Sheet

Basic Query Structure

SELECT column1, column2
FROM table_name
WHERE condition
GROUP BY column1
HAVING aggregate_condition
ORDER BY column1 DESC
LIMIT 10;

Common Aggregate Functions

FunctionWhat it givesExample
COUNT(*)the number of rowsCOUNT(*)
COUNT(column)the number of rows where the column is not NULLCOUNT(customer_id)
SUM(column)the totalSUM(price)
AVG(column)the averageAVG(review_score)
MIN(column)the smallest valueMIN(shipping_limit_date)
MAX(column)the largest valueMAX(shipping_limit_date)

Common Operators

OperatorMeaningExample
=equal toWHERE state = 'SP'
!= or <>not equal toWHERE status != 'canceled'
>, <greater than, less thanWHERE amount > 100
>=, <=greater or equal, less or equalWHERE score >= 4
ANDboth conditions trueWHERE a = 1 AND b = 2
OReither condition trueWHERE a = 1 OR a = 2
INequal to one in a listWHERE state IN ('SP', 'RJ')
BETWEENin a range, both ends includedWHERE amount BETWEEN 10 AND 100
LIKEmatches a patternWHERE city LIKE 'sao%'
IS NULLhas no valueWHERE email IS NULL

Summary

What You Learned

  • SQLite's documentation answers what a lesson leaves out; the other databases' documentation helps when you move to them.
  • The course's database is real: 99,441 orders in 9 tables, with real gaps.
  • Tables connect through shared columns. The diagram shows which, and one of them, the zip code prefix, is not a key.
  • Run, change and build up queries one part at a time, and read the first line of an error when one fails.

Next Up

Continue to Course Roadmap & Structure, or go straight to What is SQL and What are Databases?.