KevsRobots Learning Platform
35% Percent Complete
By Kevin McAleer, 4 Minutes
Letβs practice core SQL with a small public dataset. Weβll use the Seaborn tips dataset via HTTP.
New to terms like GROUP BY, JOIN, or predicate pushdown? See the Beginner glossary.
Why this is useful: these five ideas cover 80% of everyday analytics.
In the CLI:
CREATE OR REPLACE TABLE tips AS
SELECT * FROM read_csv_auto('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv');
-- Peek at the data
SELECT * FROM tips LIMIT 5;
Whatβs happening:
read_csv_auto downloads the CSV and infers column types for you.tips so queries are fast and reproducible.LIMIT 5 is a safe way to preview without dumping everything to the screen.Purpose: pick the columns (features) and rows (cases) you care about.
-- Select specific columns
SELECT day, time, total_bill, tip FROM tips LIMIT 10;
-- Filter rows
SELECT * FROM tips WHERE day = 'Sun' AND total_bill > 20 ORDER BY total_bill DESC LIMIT 10;
-- Derived columns (create a new metric on the fly)
SELECT total_bill, tip, ROUND(tip / total_bill * 100, 2) AS tip_pct FROM tips LIMIT 10;
You try it (3β5 min)
- Show top 5
total_billrows for Saturdays only, withday,time,total_bill, andtip_pct- Add a filter
tip_pct >= 18and see how results change
Notes:
NULLIF(total_bill,0) to avoid errors.Purpose: turn many rows into a concise summary for trends and comparisons.
-- Group and aggregate (dimension: day; measures: count and average)
SELECT day, COUNT(*) AS orders, ROUND(AVG(total_bill), 2) AS avg_bill
FROM tips
GROUP BY day
ORDER BY orders DESC;
-- Multiple dimensions (day + time)
SELECT day, time, ROUND(SUM(total_bill), 2) AS revenue
FROM tips
GROUP BY day, time
ORDER BY revenue DESC;
You try it (3β5 min)
- Which
day, timepair has the highestavg_bill?- Add
COUNT(*) AS ordersand sort byordersinstead
Notes:
day, time) define βbucketsβ. Measures (SUM/COUNT/AVG) summarize each bucket.COUNT(*) counts rows; AVG and SUM ignore NULLs by default.Purpose: attach helpful context from another table using a shared key.
-- Tiny dimension table adds a weekend flag by day
CREATE OR REPLACE TABLE day_names(day TEXT, weekend BOOLEAN);
INSERT INTO day_names VALUES
('Thur', FALSE), ('Fri', FALSE), ('Sat', TRUE), ('Sun', TRUE);
SELECT t.day, dn.weekend, COUNT(*) AS orders
FROM tips t
JOIN day_names dn ON dn.day = t.day
GROUP BY t.day, dn.weekend
ORDER BY orders DESC;
Notes:
JOIN matches rows where the key columns are equal.JOIN (inner) for intersecting values; later explore LEFT JOIN when you need to keep all rows from the left side.COALESCE(x, 0) replaces NULLs with a default (handy for sums/ratios).CAST(x AS INTEGER) or shorthand x::INTEGER when needed.SELECT ROUND(tip / NULLIF(total_bill,0) * 100, 2) AS tip_pct
FROM tips
LIMIT 5;
total_bill, showing day, time, and tip_pct.day and time has the highest average tip_pct?party_size_band table (e.g., size 1β2 = small, 3β4 = medium, 5+ = large) and join to compare revenue by band.
You can use the arrows β β on your keyboard to navigate between lessons.
Comments