๐ Day 7 of 7-Day SQL challenge: Mastering SQL Subqueries: The Final Boss Level!

๐ Letโs Go Deeper Into the SQL World!
Hey future data wizards! ๐งโโ๏ธ You've made it to Day 7 of the SQL Learning Challenge! Thatโs a HUGE milestone! Today we dive into a slightly deeper but super exciting concept: Subqueries.
๐ง What is a Subquery?
A subquery is just a query within another query. Think of it like a mini detective working inside your big detective! ๐ต๏ธโโ๏ธ
They help answer more complex questions and make your queries smarter. ๐
๐งช Use Case 1: Students Who Scored Above Average
SELECT NAME, MARKS
FROM student
WHERE MARKS > (SELECT AVG(MARKS) FROM student);
๐ Explanation:
We want to find all students who scored more than the average of the class.
๐ The part inside the parentheses (SELECT AVG(MARKS) FROM student)
is the subquery.
๐ It finds the average marks, and the outer query shows students whose marks are above that average.
๐ฅ Cool, right? Weโre comparing students with the entire class average dynamically!
๐ฒ Use Case 2: Students with Even Roll Numbers
SELECT NAME, ROLLNO
FROM student
WHERE ROLLNO IN (SELECT ROLLNO FROM student WHERE ROLLNO % 2 = 0);
๐ Explanation:
We want to display students who have even roll numbers.
๐ The inner query finds all even roll numbers.
๐ The outer query then shows names and roll numbers of students who match.
๐ Boom! Clean and powerful!
๐ฏ Other Cool Examples of Subqueries:
โ 1. Students Who Have Highest Marks:
SELECT * FROM student
WHERE marks = (SELECT MAX(marks) FROM student);
โ 2. Students with Lowest Marks in City 'Pune':
SELECT * FROM student
WHERE marks = (SELECT MIN(marks) FROM student WHERE city = 'Pune');
โ 3. Total number of students in a particular city:
SELECT city, (SELECT COUNT(*) FROM student WHERE city = 'Mumbai') AS total_in_mumbai
FROM student;
๐ฎ BONUS: FUN INTERACTIVE PROJECT โ โSQL BATTLE ARENAโ ๐ฎ
Letโs create a mini project that helps you revise ALL your SQL concepts learned over the last 7 days.
๐ฐ Project Name: SQL Battle Arena โ Student vs Database!
๐ Project Summary:
Youโre a Student Warrior ๐งโ๐ armed with SQL queries. Your goal is to complete all stages by solving different SQL puzzles involving:
Level | Topic |
๐งฑ Level 1 | Basic Queries (SELECT, WHERE, ORDER BY) |
๐ Level 2 | JOINS (INNER, LEFT, RIGHT, FULL) |
๐ฒ Level 3 | Aggregate Functions (SUM, AVG, COUNT) |
๐ Level 4 | GROUP BY and HAVING |
๐งฉ Level 5 | Subqueries |
๐ฅ Final Boss | Complex JOIN + Subquery Challenge |
๐๏ธ Database Design:
Table: STUDENT
ROLLNO | NAME | MARKS | CITY |
1 | Arjun | 88 | Pune |
2 | Meera | 78 | Mumbai |
3 | Rahul | 92 | Solapur |
4 | Riya | 85 | Pune |
Table: COURSE
STU_ID | COURSENAME | FEE |
1 | SQL Mastery | 3000 |
2 | Web Dev Boot | 3500 |
3 | Data Science | 5000 |
1 | Python Basics | 2000 |
๐ฅ Sample Tasks by Level:
๐งฑ Level 1: Basic SELECT
SELECT * FROM student WHERE marks > 80;
๐ Level 2: JOIN
SELECT s.name, c.coursename
FROM student s
JOIN course c
ON s.rollno = c.stu_id;
๐ฒ Level 3: Aggregates
SELECT city, COUNT(*)
FROM student
GROUP BY city;
๐ Level 4: GROUP BY + HAVING
SELECT city, AVG(marks)
FROM student
GROUP BY city
HAVING AVG(marks) > 80;
๐งฉ Level 5: Subquery
SELECT name, marks
FROM student
WHERE marks > (SELECT AVG(marks) FROM student);
๐ฅ Final Boss: JOIN + Subquery
SELECT s.name, c.coursename
FROM student s
JOIN course c
ON s.rollno = c.stu_id
WHERE s.marks > (SELECT AVG(marks) FROM student);
๐ Conclusion โ You Did It!
Youโve completed 7 days of focused SQL learning โ from basic queries to subqueries and joins โ and even built a fun review project!
โจ "SQL is not just about databases, it's about thinking logically and asking powerful questions to your data." โ Your Inner Data Guru
If you ever get stuck or just need a quick refresher, Iโve got you covered.
๐ Check out my complete SQL Cheatsheet here:
๐ Click to View on GitHub
Keep it simple. Keep learning. And keep querying! ๐๐ง
Keep querying and stay curious! ๐๐
#SQLMaster
#QueryLikeAPro
#7DaySQLChallenge
Subscribe to my newsletter
Read articles from Vrushabh Jadkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
