KevsRobots Learning Platform
56% Percent Complete
By Kevin McAleer, 4 Minutes
Letβs run a compact analysis workflow you can adapt to your own data. Weβll ingest CSV, transform to tidy tables, and export Parquet.
New to terms like view, table, materialize, or Parquet? See the Beginner glossary.
Note: ensure an
exports/folder exists in your project (or change the path) before runningCOPYcommands below.
Why this matters:
Goal: get the source data into a table for stable, fast queries.
.open analytics.duckdb
CREATE OR REPLACE TABLE tips AS
SELECT * FROM read_csv_auto('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv');
Notes:
.open creates/opens a local database file next to your project.read_csv_auto infers schema automatically; great for quick starts.Quality check:
SELECT COUNT(*) AS rows, MIN(total_bill), MAX(total_bill) FROM tips;
SELECT * FROM tips LIMIT 5;
You try it
- Verify row count:
SELECT COUNT(*) FROM tips;- Quick peek:
SELECT * FROM tips LIMIT 5;
Goal: create helpful metrics without changing the raw table.
CREATE OR REPLACE VIEW v_tips_enriched AS
SELECT *, ROUND(tip / NULLIF(total_bill,0) * 100, 2) AS tip_pct
FROM tips;
Notes:
NULLIF(total_bill,0) prevents divide-by-zero.tip_pct) in views for clarity.Quality check:
SELECT * FROM v_tips_enriched LIMIT 5;
SELECT AVG(tip_pct) FROM v_tips_enriched;
You try it
- Round
tip_pctto 2 decimals astip_pct_2dp- Filter out rows where
tip_pctis NULL
Goal: answer questions by summarizing and comparing groups.
-- Day/time revenue and tip behavior
WITH day_time AS (
SELECT day, time, SUM(total_bill) AS revenue, AVG(tip_pct) AS avg_tip_pct
FROM v_tips_enriched
GROUP BY day, time
)
SELECT * FROM day_time ORDER BY revenue DESC;
Notes:
GROUP BY to make buckets (e.g., by day and time).SUM(total_bill) and AVG(tip_pct) describe each bucket.Try: filter to a specific day or time to focus the analysis.
Goal: save results youβll reuse or share.
CREATE OR REPLACE TABLE tip_summary AS
SELECT day, time, ROUND(SUM(total_bill),2) AS revenue,
ROUND(AVG(tip_pct),2) AS avg_tip_pct,
COUNT(*) AS orders
FROM v_tips_enriched
GROUP BY day, time;
Notes:
Quality check:
SELECT * FROM tip_summary ORDER BY revenue DESC LIMIT 5;
Goal: produce a fast, portable file for downstream tools.
COPY tip_summary TO 'exports/tip_summary.parquet' (FORMAT 'parquet');
Notes:
import duckdb, pandas as pd
con = duckdb.connect("analytics.duckdb")
df = con.execute("SELECT * FROM tip_summary ORDER BY revenue DESC").df()
ax = df.plot.bar(x='day', y='revenue', title='Revenue by Day')
fig = ax.get_figure()
fig.tight_layout()
fig.savefig('exports/revenue_by_day.png', dpi=150)
Tip:
You try it
- Change chart type to line or stacked bar
- Save the figure as
exports/tips_chart.png
weekend flag (via a tiny lookup table) and compare avg_tip_pct for weekend vs weekday.high_tip indicator (e.g., tip_pct >= 18) and count orders by day.
You can use the arrows β β on your keyboard to navigate between lessons.
Comments