SQL for Data Science · Lesson 1 of 1
SQL for Data Science
Model relational data and use SQL to filter, aggregate, join, and safely change records.
Lesson
1. Introduction
Relational databases store related information in tables with defined columns and keys. SQL supports both analysis and controlled data changes, making it essential when datasets are larger than a local file or shared across applications.
2. Core concepts
A primary key identifies a row; a foreign key expresses a relationship. SELECT constructs an answer from rows and columns, while GROUP BY changes the output grain. JOIN combines records based on a relationship, and constraints help preserve valid data.
3. Topics covered in this module
- Database and table creation, data types, constraints, and foreign keys
- SELECT, filtering, sorting, date/time functions, and CASE
- INSERT, UPDATE, DELETE, transactions, and views
- Joins, UNION, grouping, functions, indexes, subqueries, and stored procedures
4. Syntax and how to read it
SELECT category, COUNT(*) AS item_count
FROM products
WHERE active = TRUE
GROUP BY category
HAVING COUNT(*) >= 5
ORDER BY item_count DESC;The query filters active products, groups the remaining records by category, and retains groups with at least five rows.
5. Practical workflow
Inspect schema and key constraints, state the output grain, and build the query incrementally. Use transactions and a restrictive predicate for changes; verify the number of affected rows before committing.
6. Common mistakes
- Changing data without a narrow WHERE clause and reviewing affected rows.
- Using GROUP BY incorrectly or joining tables at incompatible grains.
7. Best practices
- Use explicit columns and parameterized queries from code.
- Use transactions for related changes and inspect query plans for performance work.
8. Practice task
A table has multiple line items per order. How would you count orders by customer?
9. One possible solution
Group by customer and count distinct order_id values, or aggregate line items to one row per order before counting.10. Summary
Model relational data and use SQL to filter, aggregate, join, and safely change records. Use explicit columns and parameterized queries from code. Use transactions for related changes and inspect query plans for performance work.
11. What comes next
The next module, Probability, builds on this foundation and applies it to a new part of the analytics workflow.
Visual guide
A reliable SQL for Data Science workflow
-
Step 01
Frame
Define the question, input scope, and intended output.
-
Step 02
Work
Apply the method and validate assumptions as you go.
-
Step 03
Check
Compare results with known cases and communicate limitations.
Code examples
SELECT c.name, SUM(o.total) AS revenue
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name;
One revenue total per customer.
START TRANSACTION;
UPDATE orders SET status = 'review'
WHERE order_id = 101;
SELECT ROW_COUNT();
ROLLBACK;
Demonstrates a reviewable change that is rolled back; use COMMIT only after verification.
Key points
- Model relational data and use SQL to filter, aggregate, join, and safely change records.
- Use explicit columns and parameterized queries from code.
- Use transactions for related changes and inspect query plans for performance work.
Check your understanding
Practice
Your learning progress