Skip to main content

SQL Joins and Subqueries Explained Step-by-Step

SQL Joins and Subqueries Explained Step-by-Step

SQL Joins and Subqueries Explained Step-by-Step

SQL Joins and Subqueries are powerful tools for combining and filtering data across multiple tables. This guide will help you understand how they work with syntax, use cases, and examples.


Topic 5: SQL Joins

SQL Joins are used to combine rows from two or more tables based on a related column.

1. INNER JOIN

Returns records that have matching values in both tables.

SELECT Employees.Name, Departments.DeptName
FROM Employees
INNER JOIN Departments
ON Employees.DeptID = Departments.ID;

2. LEFT JOIN (LEFT OUTER JOIN)

Returns all records from the left table and the matched records from the right table. If no match, NULLs are returned.

SELECT Customers.Name, Orders.OrderDate
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

3. RIGHT JOIN (RIGHT OUTER JOIN)

Opposite of LEFT JOIN. Returns all rows from the right table and matched rows from the left table.

4. FULL JOIN (FULL OUTER JOIN)

Returns all rows from both tables. Unmatched rows show NULLs.

5. SELF JOIN

A self join is a regular join but the table is joined with itself.

SELECT A.Name AS Employee, B.Name AS Manager
FROM Employees A
JOIN Employees B
ON A.ManagerID = B.EmployeeID;

6. CROSS JOIN

Returns the Cartesian product of both tables.


Topic 6: SQL Subqueries

A subquery is a query inside another query. It's used to return data that will be used in the main query.

1. Nested Subquery

Used inside WHERE or HAVING clause.

SELECT Name
FROM Employees
WHERE DeptID = (
  SELECT ID
  FROM Departments
  WHERE DeptName = 'Sales'
);

2. Correlated Subquery

References column from outer query and runs for each row.

SELECT Name
FROM Employees E
WHERE Salary > (
  SELECT AVG(Salary)
  FROM Employees
  WHERE DeptID = E.DeptID
);

3. Subquery in FROM clause

SELECT Dept, AVG_Salary
FROM (
  SELECT DeptID AS Dept, AVG(Salary) AS AVG_Salary
  FROM Employees
  GROUP BY DeptID
) AS Sub;

Why Use Joins and Subqueries?

  • Combine data from multiple tables
  • Simplify complex queries
  • Improve reporting and data analysis

Also read: More SQL tutorials on Sabbir93s Blog

FAQs

  • Q: What is the difference between INNER JOIN and LEFT JOIN?
    A: INNER JOIN shows only matched rows; LEFT JOIN shows all left table rows even without matches.
  • Q: When should I use a subquery?
    A: When you need to filter data using values calculated from another query.
  • Q: Are joins faster than subqueries?
    A: Usually yes, but it depends on the database engine and indexing.

Labels: SQL Joins, SQL Subqueries, SQL Tutorial, SQL Syntax

Comments

Popular posts from this blog

Beatriz Taufenbach vs Natalie Burn: Real Truth Behind the Viral Toxic Trailer Scene (2026) — Explained

Beatriz Taufenbach vs Natalie Burn: Real Truth Behind the Viral Toxic Trailer Scene (2026) — Explained The teaser for “ Toxic: A Fairy Tale for Grown-Ups ” starring Yash exploded across the internet after its January 2026 release, not just for its gritty action but for one sensuous, viral moment in a cemetery sequence. Almost immediately, fans and content sites were searching terms like “Toxic trailer mystery actress,” “Beatriz Taufenbach identity,” and “Natalie Burn Toxic scene” to understand who the foreign woman in that intimate car sequence really is. What followed was a mix of misinformation, viral curiosity, and eventual clarification straight from the director’s official account. Here’s the complete, true story behind the confusion — what’s factual, what’s rumor, and who the real actress is. The Viral Moment That Sparked the Searches When the Toxic teaser dropped on January 8, 2026 — intentionally released on Yash’s birthday — millions of viewers were captivated by a bold and in...

How to Earn from Clipster.gg in 2025

How to Earn from Clipster.gg (2025 Guide) 💸 How to Earn from Clipster.gg in 2025 Clipster.gg is a platform where you can earn money from viral videos using three simple methods: logo promotion , UGC content , and content clipping . Let’s break them down: 🔥 1. Logo Promotion Pick any brand’s logo from the campaign section and simply overlay it on top of your short-form videos (Reels, Shorts, TikTok). Once your videos get views, you get paid based on performance. No need for fancy editing. 🎥 2. UGC Content Creation Create your own reaction, meme, skit or short-form video based on the campaign theme. If your video performs well (goes viral), you earn money based on views. This is perfect for creators who want to make engaging content. 📁 3. Content Clipping (Copy & Paste Method) Clipster provides ready-made clips in Google Drive or folders. You simply download the videos and repost them as Sho...

Headai – AI Marketing Platform to Automate Your Brand

Headai – AI Marketing Platform to Automate Your Brand Headai – AI Marketing Platform to Automate Your Brand Exclusive Access: Click to Sign Up 🚀 What is Headai? Headai is an AI-powered marketing platform that allows you to automate your entire brand strategy. From generating ads to writing content and analyzing performance, Headai does it all using artificial intelligence. No more manual ad creation or guessing what content works— Headai does it smartly for you! ✨ Key Features 🎯 AI-powered Ad Generator (Google, Facebook, Instagram) 📝 Smart Content Creation for Blogs & Social Media 📊 Performance Insights & Auto Optimization 📆 Social Media Scheduler with AI 🧠 Full AI Brand Manager 🔗 Internal Resource Also Read: SQL Joins and Subqueries Explained Step-by-Step — One of our mos...