DAY 2: SQL - SELECT & Aggregates
Learning Objectives: - Understand database structure (tables, columns, rows) - Master SELECT statement basics - Learn SQL aggregate functions (SUM, AVG, COUNT, MAX, MIN) - Use DISTINCT and ORDER BY
Topics Covered (60 min):
1. Database Fundamentals - Tables: Collections of data (like Excel sheets) - Columns: Fields/attributes (like Excel column headers) - Rows: Individual records - Example: Orders table with OrderID, CustomerID, OrderDate, Amount
2. SELECT Statement - All columns:
SELECT * FROM Orders; - Specific columns:
SELECT OrderID, Sales FROM Orders; - Unique values:
SELECT DISTINCT Region FROM Sales;
3. Aggregate Functions (Mirror Excel Day 1) -
SELECT SUM(Sales) FROM Orders; - Total sales -
SELECT AVG(Discount) FROM Orders; - Average discount -
SELECT COUNT(*) FROM Orders; - Count rows -
SELECT MAX(Quantity), MIN(Quantity) FROM Orders;
4. ORDER BY - Ascending:
ORDER BY Sales ASC; - Descending:
ORDER BY Sales DESC; - Multiple:
ORDER BY Region ASC, Sales DESC;
Video Resources:
-
Master SQL SELECT in ONE Video! Complete Tutorial 2025 (14:20)
https://www.youtube.com/watch?v=2nxKlUCyRS4 -
Using SELECT, FROM & WHERE in BigQuery (4:05)
https://www.youtube.com/watch?v=jKYkHqEwlHg -
SQL Full Course for Beginners (34:01)
https://www.youtube.com/watch?v=SSKVgrwhzus
AI Learning Prompts (Concept Understanding Only):
Understanding Databases & Tables:
I'm new to SQL and databases. Please explain:
1. What are databases, tables, columns, and rows? Use an analogy like an Excel spreadsheet or filing cabinet
2. What's the difference between a database and a table?
3. Show me a simple example of a Students table structure with 5 columns (ID, Name, Age, Grade, City)
4. How is SQL different from Excel?
5. What does relational database mean?
Use beginner-friendly language and real-world analogies.
Understanding SELECT Statement:
Explain the SQL SELECT statement:
1. What is the basic syntax of SELECT?
2. What's the difference between SELECT * and SELECT specific columns?
3. When and why should I use DISTINCT?
4. How does ORDER BY work? What's the difference between ASC and DESC?
5. Can I select columns in any order?
6. Show me examples using a Books table (Title, Author, Price, Year)
Provide clear syntax examples for each concept.
Understanding SQL Aggregates (Mirror Excel Day 1):
Yesterday I learned SUM, AVG, COUNT, MAX, MIN in Excel. Help me understand SQL equivalents:
1. Show me the SQL syntax for each aggregate function
2. How do SQL aggregates differ from Excel functions?
3. Give me side-by-side comparison: Excel formula vs SQL query
4. Provide examples using an Inventory table (Product, Quantity, Price)
5. What happens when I use aggregate functions with NULL values?
Include syntax examples that help me understand the parallel.
Understanding ORDER BY:
Explain SQL sorting with ORDER BY:
1. What is the syntax for ORDER BY?
2. How do I sort by multiple columns?
3. What's the default sort order (ASC or DESC)?
4. Can I sort by a column I didn't SELECT?
5. Show examples using an Employees table (Name, Department, Salary, HireDate)
Provide clear examples for each scenario.
Guided Practice (60 min):
📝 How to approach Guided Practice:
- Follow step-by-step: Work through each exercise in order
- Use provided resources: Reference the video tutorials and AI prompts if you get stuck
- Verify your work: Check your results match expected outcomes
- Take notes: Document key learnings and shortcuts you discover
- Ask for help: Use AI assistants to clarify concepts you don't understand
Using Superstore Sales dataset - SQL Practice
🎯 Interactive SQL Playground
💻 Learn by Doing - SQL Practice Right Here!
Use this interactive SQL editor to practice SELECT and aggregate queries on sample Superstore data. Start
with SELECT * FROM Orders and modify it based on the exercises below!
💡 Understanding SQL vs Excel: If you completed Day 1 (Excel), you'll notice SQL does similar things but with different syntax. Instead of clicking cells, you write queries!
💡 Try these example queries (click to load):
SELECT * FROM Orders LIMIT 10- View first 10 ordersSELECT SUM(Sales) FROM Orders- Calculate total sales (like Excel SUM)SELECT AVG(Discount) FROM Orders- Find average discountSELECT COUNT(*) FROM Orders- Count total ordersSELECT MAX(Sales), MIN(Sales) FROM Orders- Find highest and lowest salesSELECT DISTINCT Region FROM Orders- List unique regionsSELECT * FROM Orders ORDER BY Sales DESC LIMIT 5- Top 5 sales orders
Tip: Click any example query above to load it into the editor! Press Ctrl+Enter to run queries quickly.
Exercise 1: Your First SELECT Query - View All Data
Step-by-Step Instructions:
- In the SQL query editor, type exactly:
SELECT * FROM Orders; - Click the "Execute" button (▶️) or press F5
- You should see ALL your data displayed
- Now try:
SELECT * FROM Orders LIMIT 20;to see only 20 rows
🔍 What does this mean?
SELECT= "Show me..."*= "...all columns"FROM Orders= "...from the Orders table"LIMIT 20= "...but only 20 rows"
💡 Tips:
- Always end SQL statements with a semicolon
; - SQL keywords (SELECT, FROM) are case-insensitive, but UPPERCASE is conventional
- Use LIMIT when exploring large datasets to avoid overwhelming output
✅ Expected Result: A table showing all columns and rows (or 20 rows if you used LIMIT)
Exercise 2: SELECT Specific Columns
Step-by-Step Instructions:
- Type this query:
SELECT OrderID, CustomerName, Sales, Profit FROM Orders LIMIT 20; - Click Execute (▶️)
- Notice how you only see the 4 columns you requested!
Now try selecting different columns:
SELECT ProductName, Quantity, Discount
FROM Orders
LIMIT 15;
💡 Tips:
- List column names separated by commas
- Order of columns in SELECT determines display order
- Use multi-line formatting for readability (as shown above)
✅ Expected Result: Only the columns you specified appear in the results
Exercise 3: Using DISTINCT to Find Unique Values
Step-by-Step Instructions:
- First, see ALL regions (with duplicates):
This shows thousands of rows because each order repeats its region.SELECT Region FROM Orders; - Now, get UNIQUE regions only:
SELECT DISTINCT Region FROM Orders; - You should see exactly 4 unique regions: Central, East, South, West
Try finding other unique values:
SELECT DISTINCT Category FROM Orders;
SELECT DISTINCT ShipMode FROM Orders;
💡 Tips:
DISTINCTremoves duplicate values from results- Very useful for finding all unique categories, regions, products, etc.
- Similar to Excel's "Remove Duplicates" feature
✅ Expected Result: A short list of unique values (4 regions, 3 categories, etc.)
Exercise 4: Aggregate Functions - SUM, AVG, COUNT, MAX, MIN
🎯 This is the SQL equivalent of Day 1's Excel functions!
Step-by-Step Instructions:
- Calculate Total Sales (like Excel's =SUM()):
TheSELECT SUM(Sales) AS TotalSales FROM Orders;AS TotalSalesrenames the output column for clarity. - Find Average Sales (like Excel's =AVERAGE()):
SELECT AVG(Sales) AS AverageSales FROM Orders; - Count Total Orders (like Excel's =COUNT()):
SELECT COUNT(*) AS TotalOrders FROM Orders;COUNT(*)counts all rows.COUNT(ColumnName)counts non-null values. - Find Maximum Sale (like Excel's =MAX()):
SELECT MAX(Sales) AS HighestSale FROM Orders; - Find Minimum Discount (like Excel's =MIN()):
SELECT MIN(Discount) AS LowestDiscount FROM Orders;
💡 Pro Tip - Combine multiple aggregates in one query:
SELECT
SUM(Sales) AS TotalSales,
AVG(Sales) AS AvgSales,
COUNT(*) AS TotalOrders,
MAX(Sales) AS HighestSale,
MIN(Sales) AS LowestSale
FROM Orders;
✅ Expected Result: Single row with calculated aggregate values (just like Excel formulas, but in one query!)
Exercise 5: ORDER BY - Sorting Your Results
Step-by-Step Instructions:
- Sort by Sales (lowest to highest):
SELECT ProductName, Sales FROM Orders ORDER BY Sales ASC LIMIT 10;ASC= Ascending (smallest first) - Sort by Sales (highest to lowest):
SELECT ProductName, Sales FROM Orders ORDER BY Sales DESC LIMIT 10;DESC= Descending (largest first) - Sort by multiple columns:
This sorts by Region first (A-Z), then within each region, sorts by Sales (high to low)SELECT Region, Category, Sales FROM Orders ORDER BY Region ASC, Sales DESC;
💡 Tips:
ORDER BYalways comes at the end of your query (before LIMIT)- ASC is default - you can omit it:
ORDER BY Sales=ORDER BY Sales ASC - You can sort by columns not in your SELECT list
✅ Expected Result: Data sorted in the order you specified
🎯 Practice Goal Checkpoint:
By completing these 5 exercises, you should now be able to:
- Write basic SELECT queries to retrieve data
- Select specific columns or all columns (*)
- Use DISTINCT to find unique values
- Apply aggregate functions: SUM, AVG, COUNT, MAX, MIN
- Sort results with ORDER BY (ASC/DESC)
- See the parallel between SQL and Excel functions
Compare with Day 1: Notice how SELECT SUM(Sales) does the same thing as Excel's
=SUM(D:D)? SQL is just a different way to ask the same questions!
Take a 5-minute break, then move on to Independent Practice!
Independent Practice (60 min):
💡 Hints for Independent Practice:
- Start simple: Begin with basic variations before attempting complex modifications
- Break it down: Divide complex problems into smaller, manageable steps
- Test incrementally: Verify each step works before moving to the next
- Use documentation: Refer to official docs or tutorials when needed
- Experiment: Try different approaches to find what works best
Solve these 8 problems yourself!
- Find maximum and minimum order quantities
- List all unique regions using DISTINCT
- Calculate total profit across all orders
- Find average profit margin (you’ll need to calculate)
- Count distinct customers in database
- Display all orders sorted by Sales (highest to lowest)
- Show OrderID and Profit, sorted by Profit (lowest to highest)
- List unique product categories
Daily Assignment (60 min):
- Replicate ALL Excel Day 1 calculations using SQL
- Compare your results - they should match the Excel answers!
- Document any differences you find
- Save as: Day02_SQL_Practice.sql
Expected Outcomes:
✓ Understand parallel between Excel and SQL aggregates
✓ Write basic SELECT queries with confidence
✓ Complete practice problems with 80%+ accuracy
✓ Submit: Day02_SQL_Practice.sql
📝 Day 2 Quick Quiz - Test Your Knowledge!
Q1. Which SQL clause retrieves data from a database?
Q2. What does DISTINCT do in a SELECT statement?
Q3. Which aggregate function counts the number of rows?
Q4. What is the correct syntax for a basic SELECT query?
Q5. Which function finds the highest value in a column?