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 withAND,OR,NOT, using brackets when mixing them. branch IN ('Airport', 'Campus')is a neat OR.total BETWEEN 5 AND 10includes both ends.item LIKE 'C%'matches anything starting with C (%is any text,_one character).SELECT DISTINCT branchlists each value once.IS NULLfinds missing values;= NULLnever 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).
SELECT these columns (none ticked = *)
Your SQL
SELECT order_id, branch, item, total FROM orders LIMIT 10;
Result
| # | order_id | branch | item | total |
|---|---|---|---|---|
| 0 | 1001 | Riverside | Croissant | 3.5 |
| 1 | 1002 | Downtown | Muffin | 12 |
| 2 | 1003 | Riverside | Bagel | 4.25 |
| 3 | 1004 | Airport | Muffin | 3 |
| 4 | 1005 | Airport | Chai | 4 |
| 5 | 1006 | Downtown | Croissant | 7 |
| 6 | 1007 | Campus | Espresso | 6 |
| 7 | 1008 | Riverside | Latte | 9 |
| 8 | 1009 | Airport | Bagel | 8.5 |
| 9 | 1010 | Downtown | Croissant | 14 |
10 rows × 4 columns
Real SQL on 4,420 orders
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
Quiz score: 0 of 4
Exercises solved: 0 of 2
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.