🎯 Objective
📋 Database Schema
Description: Stores student information including ID, name, and age.
Description: Contains match details including ID, name, and venue.
Description: Junction table linking students to matches with the date played.
💡 Key Concepts
INNER JOIN
Returns records that have matching values in both tables.
GROUP BY
Groups rows sharing a property so aggregate functions can be applied.
HAVING Clause
Filters grouped records based on aggregate function conditions.
DISTINCT
Eliminates duplicate rows from the result set.
📝 Implementation
Populate All Tables
Create Tables:
Insert Sample Data:
Sample Data Overview:
| Student Table | ||
|---|---|---|
| sid | sname | age |
| S001 | Amit | 21 |
| S002 | Priya | 22 |
| S003 | Rahul | 20 |
| S004 | Neha | 23 |
| S005 | Vikram | 21 |
| Match Table | ||
|---|---|---|
| mid | mname | venue |
| B10 | Cricket Championship | Delhi |
| B11 | Football League | Mumbai |
| B12 | Basketball Tournament | Delhi |
| B13 | Tennis Open | Bangalore |
| B14 | Badminton Cup | Mumbai |
Find all information of students who have played match number B10
Expected Output:
| sid | sname | age |
|---|---|---|
| S001 | Amit | 21 |
| S002 | Priya | 22 |
Find the name of matches played by Amit
Expected Output:
| mname |
|---|
| Cricket Championship |
| Football League |
Find the names of students who have played a match in Delhi
Expected Output:
| sname |
|---|
| Amit |
| Priya |
| Neha |
Find the names of students who have played at least one match
Expected Output:
| sname |
|---|
| Amit |
| Priya |
| Rahul |
| Neha |
| Vikram |
Find the IDs and names of students who have played two different matches on the same day
Expected Output:
| sid | sname |
|---|---|
| S002 | Priya |
| S003 | Rahul |
Find the IDs of students who have played a match in Delhi or Mumbai
Expected Output:
| sid |
|---|
| S001 |
| S002 |
| S003 |
| S004 |
Find the average age of students
Expected Output:
| average_age |
|---|
| 21.4 |
🎓 Learning Outcomes
- ✓ GROUP BY Clause: Learned to group records for aggregate operations
- ✓ HAVING Clause: Applied conditions on grouped data using aggregate functions
- ✓ Aggregate Functions: Used COUNT(), AVG(), and DISTINCT for data analysis
- ✓ Multi-table Queries: Successfully queried data across three related tables
- ✓ Data Filtering: Applied WHERE, IN, and comparison operators effectively
📚 Practice Exercises
Exercise 1: Advanced Filtering
Find students who played matches in exactly two different venues.
Exercise 2: Complex Aggregation
Find the venue where the maximum number of matches were played, along with the count.
Exercise 3: Date Analysis
List all matches that had more than one student participating on the same day.
Exercise 4: Subqueries
Find students who are older than the average age and have played at least one match.
⚡ SQL Best Practices
1. Use Meaningful Aliases
Always use clear table aliases (S for Student, M for Match, P for Play) to improve readability.
2. Apply DISTINCT Appropriately
Use DISTINCT to eliminate duplicates when joining tables that may produce multiple matching rows.
3. Optimize JOIN Conditions
Always specify JOIN conditions using ON clause rather than WHERE clause for better query optimization.
4. HAVING vs WHERE
Use WHERE to filter rows before grouping, HAVING to filter groups after aggregation.
⚠️ Common Mistakes to Avoid
❌ Forgetting JOIN Conditions
Always specify the relationship between tables using ON clause to avoid Cartesian products.
❌ Using Aggregate Functions Without GROUP BY
When using aggregate functions with other columns, ensure proper GROUP BY clause is included.
❌ Ignoring NULL Values
Be aware that aggregate functions typically ignore NULL values, which can affect results.
❌ Not Using DISTINCT When Needed
JOINs can produce duplicate rows. Use DISTINCT when you need unique results.
🔄 SQL Query Execution Order
📌 Summary
This experiment demonstrated the practical application of SQL JOIN operations combined with GROUP BY and HAVING clauses. Students learned to:
- Create and populate relational database tables with proper constraints
- Perform INNER JOIN operations to combine data from multiple tables
- Use GROUP BY to aggregate data based on specific columns
- Apply HAVING clause to filter aggregated results
- Utilize aggregate functions like COUNT(), AVG(), and DISTINCT
- Write complex multi-table queries with proper filtering conditions
- Understand the difference between WHERE and HAVING clauses
📖 Additional Resources
📘 Reference Materials
Database System Concepts by Silberschatz, Korth & Sudarshan
💻 Online Practice
SQLZoo, LeetCode, HackerRank SQL challenges
🎥 Video Tutorials
Stanford DB Course, MySQL Tutorial for Beginners
📚 Documentation
Official MySQL, PostgreSQL, Oracle documentation