SQL Subqueries

Introduction

In this chapter, we will focus on subqueries in SQL. A subquery, also known as an inner query or nested query, is a query within another SQL query. Subqueries can be used to perform complex queries and return data that can be used in the main query. This chapter will cover the definition, types, syntax, and provide examples to help you understand how to use subqueries effectively.

What is a Subquery?

A subquery is a query nested inside another query, such as a SELECT, INSERT, UPDATE, or DELETE statement. The result of the subquery is used by the main query.

Types of Subqueries

  1. Single-Row Subquery: Returns a single row.
  2. Multiple-Row Subquery: Returns multiple rows.
  3. Multiple-Column Subquery: Returns multiple columns.
  4. Correlated Subquery: References columns from the outer query.
  5. Non-Correlated Subquery: Independent of the outer query.

Syntax for Subqueries

Basic Syntax

SELECT column1, column2, ...
FROM table1
WHERE column_name operator (SELECT column_name FROM table2 WHERE condition);

Single-Row Subquery

SELECT first_name, last_name
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE department_name = 'HR');

Multiple-Row Subquery

SELECT first_name, last_name
FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1);

Multiple-Column Subquery

SELECT first_name, last_name
FROM employees
WHERE (department_id, job_id) IN (SELECT department_id, job_id FROM job_history WHERE end_date IS NULL);

Correlated Subquery

SELECT first_name, last_name
FROM employees e
WHERE EXISTS (SELECT 1 FROM departments d WHERE e.department_id = d.department_id AND d.location_id = 1);

Non-Correlated Subquery

SELECT first_name, last_name
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE department_name = 'Finance');

Step-by-Step Example

Sample Tables

First, let's create sample tables named employees and departments.

CREATE TABLE departments (
    department_id INT PRIMARY KEY,
    department_name VARCHAR(50),
    location_id INT
);

CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email VARCHAR(100),
    department_id INT,
    salary DECIMAL(10, 2),
    FOREIGN KEY (department_id) REFERENCES departments(department_id)
);

Inserting Sample Data

INSERT INTO departments (department_id, department_name, location_id)
VALUES
(1, 'HR', 1),
(2, 'Finance', 1),
(3, 'IT', 2);

INSERT INTO employees (first_name, last_name, email, department_id, salary)
VALUES
('Ramesh', 'Kumar', 'ramesh.kumar@example.com', 1, 50000),
('Sita', 'Patel', 'sita.patel@example.com', 2, 60000),
('Arjun', 'Singh', 'arjun.singh@example.com', 1, 55000),
('Priya', 'Sharma', 'priya.sharma@example.com', 2, 62000),
('Ravi', 'Verma', 'ravi.verma@example.com', 3, 50000);

Single-Row Subquery Example

To find employees in the HR department:

SELECT first_name, last_name
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE department_name = 'HR');

Output

first_name last_name
Ramesh Kumar
Arjun Singh

Multiple-Row Subquery Example

To find employees in departments located in location_id 1:

SELECT first_name, last_name
FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1);

Output

first_name last_name
Ramesh Kumar
Sita Patel
Arjun Singh
Priya Sharma

Correlated Subquery Example

To find employees whose department is in location_id 1 using a correlated subquery:

SELECT first_name, last_name
FROM employees e
WHERE EXISTS (SELECT 1 FROM departments d WHERE e.department_id = d.department_id AND d.location_id = 1);

Output

first_name last_name
Ramesh Kumar
Sita Patel
Arjun Singh
Priya Sharma

Non-Correlated Subquery Example

To find employees in the Finance department:

SELECT first_name, last_name
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE department_name = 'Finance');

Output

first_name last_name
Sita Patel
Priya Sharma

Conclusion

Subqueries are used in SQL for performing complex queries and retrieving specific data sets. This chapter covered the different types of subqueries, their syntax, and provided examples to illustrate their use. Understanding how to use subqueries effectively will greatly enhance your ability to query and manipulate data in a relational database.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare