Skip to main content

SQL Subqueries ๐Ÿš€

Mentor's Note: A subquery is essentially a "Question within a Question." It's one of the most powerful tools for solving complex business problems in a single step! ๐Ÿ’ก


๐ŸŒŸ The Scenario: The Inception Logic ๐ŸŽž๏ธโ€‹

Imagine you want to find all students who scored higher than the average mark.

  • The Problem: You don't know the average mark yet! ๐Ÿ“ฆ
  • The Inner Query: First, you ask the database: "What is the average mark?" (Query 1). ๐Ÿ”ข
  • The Outer Query: Then, you use that answer to ask: "Now show me everyone who scored higher than [Answer 1]." (Query 2). ๐Ÿ“ฆ
  • The Result: A perfectly nested solution. โœ…

๐Ÿ“– Concept Explanationโ€‹

1. What is a Subquery?โ€‹

A subquery is a SQL query nested inside a larger query. It is also known as an Inner Query or Nested Query.

2. Common Placementsโ€‹

  • In WHERE: Filter data based on dynamic values.
  • In FROM: Use the result of one query as a "Virtual Table" for another.
  • In SELECT: Pull in a single specific value from a related table.

๐ŸŽจ Visual Logic: The Russian Doll ๐Ÿช†โ€‹


๐Ÿ’ป Implementations (Dialect Comparison)โ€‹

-- ๐Ÿš€ Action: Find employees earning more than the company average
SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

๐Ÿ“Š Sample Dry Runโ€‹

Goal: Find students in the same city as 'Vishnu'

StepQuery PartLogicResult
1InnerFind city of 'Vishnu'"Surat"
2OuterSELECT name FROM students WHERE city = "Surat"Ankit, Priya...

๐Ÿ“‰ Technical Analysisโ€‹

  • Execution Order: The database usually runs the Inner Query once first, gets the result, and then hands it over to the Outer Query.
  • Correlated Subqueries: These are special subqueries that run once for every row in the outer query. They are powerful but can be very slow! โš ๏ธ

๐ŸŽฏ Practice Lab ๐Ÿงชโ€‹

Task: The Empty Department

Task: Find all departments in the DEPARTMENTS table that have no employees. Hint: Use WHERE dept_id NOT IN (SELECT dept_id FROM employees). ๐Ÿ’ก


๐Ÿ’ก Interview Tip ๐Ÿ‘”โ€‹

"Interviewers love asking: 'What is the difference between a Join and a Subquery?' Answer: A Join is usually faster for large data, but a Subquery is often easier to read for complex filtering logic!"


๐Ÿ’ก Pro Tip: "Inception is possible. You just need to go deeper." - Dom Cobb (Inception)


โ† Back: Grouping | Next: Data Management (DDL) โ†’

๐Ÿ“ Visit Us

๐Ÿซ VD Computer Tuition Surat

VD Computer Tuition
๐Ÿ“ Address
2/66 Faram Street, Rustompura
Surat โ€“ 395002, Gujarat, India
๐Ÿ“ž Phone / WhatsApp
+91 84604 41384
๐ŸŒ Website

Computer Classes & Tuition โ€” Areas We Serve in Surat

Adajanโ€ขAlthanโ€ขAmroliโ€ขAthwaโ€ขAthwalinesโ€ขBhagalโ€ขBhatarโ€ขBhestanโ€ขCanal Roadโ€ขChowkโ€ขCitylightโ€ขDumasโ€ขGaurav Pathโ€ขGhod Dod Roadโ€ขHaziraโ€ขJahangirpuraโ€ขKamrejโ€ขKapodraโ€ขKatargamโ€ขLimbayatโ€ขMagdallaโ€ขMajura Gateโ€ขMota Varachhaโ€ขNanpuraโ€ขNew Citylightโ€ขOlpadโ€ขPalโ€ขPandesaraโ€ขParle Pointโ€ขPiplodโ€ขPunaโ€ขRanderโ€ขRing Roadโ€ขRustampuraโ€ขSachinโ€ขSalabatpuraโ€ขSarthanaโ€ขSosyo Circleโ€ขUdhnaโ€ขVarachhaโ€ขVed Roadโ€ขVesuโ€ขVIP Road
๐Ÿ“ž Call Sir๐Ÿ’ฌ WhatsApp Sir