All labs
beginnerSQL
Query a Database with SQL
You will learn to
- Create an in-memory SQLite database using Python
- Write SQL SELECT statements to retrieve specific columns
- Filter data using the WHERE clause and logical operators
- Aggregate data using GROUP BY, SUM, COUNT, and AVG
- Use the HAVING clause to filter aggregated results
Before you start
- No experience needed; basic spreadsheets help
- The in-browser Jupyter Sandbox with sqlite3 (embedded below)
Your workspace
The notebook runs entirely in your browser (JupyterLite). The first load downloads the Python runtime, give it a minute on slow connections, then it is cached. Use the upload arrow in the file panel to add datasets.
Step-by-step
1. Create a database in memory
Done when: A SQLite connection object is successfully established2. Load the sample data into SQL
Done when: A "sales" table is populated in the SQLite database3. Your first SELECT query
Done when: A table with exactly three columns and five rows is displayed4. Filter rows with WHERE
Done when: Only rows matching your filter conditions are returned5. Aggregate data with GROUP BY
Done when: A summary table showing total units per product, sorted descending6. Filter aggregated data with HAVING
Done when: The summary table updates to exclude low-volume products
Finished every step?
Reflect
- What is the order of execution between WHERE, GROUP BY, and HAVING?
- Why do we need the LIMIT clause in large databases?
- How does the AS keyword make SQL output more readable?

