Module 3 · SQL: Getting Data Out of Databases · Lesson 8 of 51

Databases, tables and your first queries

What a database is, tables, keys and schemas, then SELECT, WHERE, ORDER BY, LIMIT, DISTINCT, IN, BETWEEN and LIKE on real café orders.

In this lesson · about 40 minutes

  • Explain tables, primary keys and a schema
  • Write SELECT … FROM … WHERE … ORDER BY … LIMIT queries
  • Filter with AND, OR, IN, BETWEEN and LIKE

Tables, keys and schemas

A relational database stores data in tables (like the ones you met in Module 1). Each table usually has a primary key: a column whose value is unique for every row, such as order_id. Other tables refer to it with a foreign key (an app_orders row stores the member_id of who ordered). The schema is the list of tables, their columns and their types. Popular databases: PostgreSQL, MySQL, SQL Server, SQLite, and cloud warehouses such as BigQuery and Snowflake. They all speak nearly the same SQL.

The shape of a query

SELECT branch, item, total          -- which columns
FROM orders                          -- which table
WHERE branch = 'Airport'             -- which rows
  AND total >= 8
ORDER BY total DESC                  -- sort, biggest first
LIMIT 10;                            -- only the first 10
  • SELECT * means all columns. You can compute columns and rename them: SELECT qty * unit_price AS line_total.
  • Text goes in single quotes; numbers don't. -- starts a comment.
  • Comparisons: =, <> or !=, <, >=. Combine with AND, OR, NOT, using brackets when mixing them.
  • branch IN ('Airport', 'Campus') is a neat OR. total BETWEEN 5 AND 10 includes both ends. item LIKE 'C%' matches anything starting with C (% is any text, _ one character).
  • SELECT DISTINCT branch lists each value once.
  • IS NULL finds missing values; = NULL never matches anything.

SQL keywords aren't case-sensitive, but writing them in capitals makes queries easier to read.

Try it · data lab

SQL query builder

Build queries by picking options and watch the SQL write itself. Work through the five challenges: each one is a real question a café manager might ask.

Challenge 1 of 5: List every order from the Airport branch.

Hint: Rows mode, WHERE branch = Airport, and make sure LIMIT doesn't cut rows off (0 means no limit).

I want

SELECT these columns (none ticked = *)

Your SQL

SELECT order_id, branch, item, total
FROM orders
LIMIT 10;

Result

#order_idbranchitemtotal
01001RiversideCroissant3.5
11002DowntownMuffin12
21003RiversideBagel4.25
31004AirportMuffin3
41005AirportChai4
51006DowntownCroissant7
61007CampusEspresso6
71008RiversideLatte9
81009AirportBagel8.5
91010DowntownCroissant14

10 rows × 4 columns

Real SQL on 4,420 orders

This cell loads the café orders into a SQLite database. q(...) runs a query and shows the result as a table. Change the query and run it again.

Exercise

Big early-morning orders

Write a query in sql that returns order_id, branch and total for orders placed at hour 7 with a total of more than 15, sorted by total from largest to smallest, then by order_id.

Code that runs before yours
import sqlite3
import pandas as pd
con = sqlite3.connect(":memory:")
pd.read_csv("kc_orders.csv").to_sql("orders", con, index=False)
q = lambda sql: pd.read_sql_query(sql, con)

Exercise

Coffee drinks at two branches

Return the distinct item names sold at 'Campus' or 'Riverside' whose name contains the letter pattern 'te' anywhere (use LIKE), sorted alphabetically. Use IN for the branches.

Code that runs before yours
import sqlite3
import pandas as pd
con = sqlite3.connect(":memory:")
pd.read_csv("kc_orders.csv").to_sql("orders", con, index=False)
q = lambda sql: pd.read_sql_query(sql, con)

Check your understanding0 of 4 correct

Question 1

1.Which clause picks the rows?

Question 2

2.How do you find rows where customer is missing?

Question 3

3.item LIKE 'C%' matches…

Question 4

4.What makes order_id a good primary key?

Quiz score: 0 of 4

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.