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

Vrushabh JadkarVrushabh Jadkar
4 min read

๐Ÿ” 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:

LevelTopic
๐Ÿงฑ Level 1Basic Queries (SELECT, WHERE, ORDER BY)
๐Ÿ”— Level 2JOINS (INNER, LEFT, RIGHT, FULL)
๐ŸŽฒ Level 3Aggregate Functions (SUM, AVG, COUNT)
๐Ÿ” Level 4GROUP BY and HAVING
๐Ÿงฉ Level 5Subqueries
๐Ÿ’ฅ Final BossComplex JOIN + Subquery Challenge

๐Ÿ—๏ธ Database Design:

Table: STUDENT

ROLLNONAMEMARKSCITY
1Arjun88Pune
2Meera78Mumbai
3Rahul92Solapur
4Riya85Pune

Table: COURSE

STU_IDCOURSENAMEFEE
1SQL Mastery3000
2Web Dev Boot3500
3Data Science5000
1Python Basics2000

๐Ÿ”ฅ 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

0
Subscribe to my newsletter

Read articles from Vrushabh Jadkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vrushabh Jadkar
Vrushabh Jadkar