Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Data
Labs

SQL Fundamentals: Writing Advanced Queries with Subqueries

In this lab, you'll learn how to write advanced SQL queries using subqueries and correlated subqueries. You'll practice selecting data based on subquery results, combining joins and subqueries, and using correlated subqueries for more complex data retrieval.

Lab platform
Lab Info
Level
Intermediate
Last updated
Sep 21, 2025
Duration
1h 6m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Writing Advanced SQL Queries with Subqueries

    SQL Guide

    For each task, replace or update the existing SQL code in the top pane (main.sql) with your answer to the task and run it to see the results in the bottom pane (SQL Viewer).


    Step 1: Writing Advanced SQL Queries with Subqueries

    In this step, you will learn how to write advanced SQL queries using subqueries. A subquery is a query nested within another SQL query, often used to simplify complex queries or return specific data for comparison. The tasks in this step will focus on using subqueries to filter data based on average values. You will write SQL queries that select data based on comparisons to averages, such as product prices or employee salaries, demonstrating the power of subqueries in SQL.

    đź«™ Database Structure

    Tables You Will Be Working With:

    Products:

    | Column Name | Data Type | |-------------|--------------------------------| | ProductID | INT PRIMARY KEY AUTO_INCREMENT | | ProductName | VARCHAR(100) | | Category | VARCHAR(100) | | ListPrice | DECIMAL(10, 2) |


    Employees:

    | Column Name | Data Type | |--------------|--------------------------------| | EmployeeID | INT PRIMARY KEY AUTO_INCREMENT | | FirstName | VARCHAR(100) | | LastName | VARCHAR(100) | | DepartmentID | INT | | Salary | DECIMAL(10, 2) |


    Customers:

    | Column Name | Data Type | |-------------|--------------------------------| | CustomerID | INT PRIMARY KEY AUTO_INCREMENT | | FirstName | VARCHAR(100) | | LastName | VARCHAR(100) |


    Orders:

    | Column Name | Data Type | |--------------|--------------------------------| | OrderID | INT PRIMARY KEY AUTO_INCREMENT | | CustomerID | INT | | OrderValue | DECIMAL(10, 2) |

  2. Challenge

    Combining Joins and Subqueries

    Step 2: Combining Joins and Subqueries

    In this step, you will learn how to combine SQL joins with subqueries to retrieve complex sets of data. A JOIN combines rows from two or more tables based on a related column, while a subquery allows you to calculate or filter additional data. By using INNER JOIN, LEFT JOIN, and RIGHT JOIN, you will retrieve data across multiple tables and filter this data with the help of subqueries. These tasks will give you a deeper understanding of how to join and query data for more complex conditions.

    đź«™ Database Structure

    Tables You Will Be Working With:

    Employees:

    | Column Name | Data Type | |--------------|--------------------------------| | EmployeeID | INT PRIMARY KEY AUTO_INCREMENT | | FirstName | VARCHAR(100) | | LastName | VARCHAR(100) | | DepartmentID | INT | | Salary | DECIMAL(10, 2) |


    Departments:

    | Column Name | Data Type | |----------------|--------------------------------| | DepartmentID | INT PRIMARY KEY AUTO_INCREMENT | | DepartmentName | VARCHAR(100) | | Budget | DECIMAL(10, 2) | | Size | INT |


    Customers:

    | Column Name | Data Type | |-------------|--------------------------------| | CustomerID | INT PRIMARY KEY AUTO_INCREMENT | | FirstName | VARCHAR(100) | | LastName | VARCHAR(100) |


    Orders:

    | Column Name | Data Type | |--------------|--------------------------------| | OrderID | INT PRIMARY KEY AUTO_INCREMENT | | CustomerID | INT | | OrderValue | DECIMAL(10, 2) |

  3. Challenge

    Working with Correlated Subqueries

    Step 3: Working with Correlated Subqueries

    In this step, you will learn how to use correlated subqueries, which are subqueries that rely on data from the outer query. Correlated subqueries are useful for row-by-row comparisons and help solve problems that require dynamic, context-based conditions. In these tasks, you will use correlated subqueries to filter employees, products, and customers based on comparisons like salary or price within specific categories or departments.

    đź«™ Database Structure

    Tables You Will Be Working With:

    Products:

    | Column Name | Data Type | |-------------|--------------------------------| | ProductID | INT PRIMARY KEY AUTO_INCREMENT | | ProductName | VARCHAR(100) | | Category | VARCHAR(100) | | ListPrice | DECIMAL(10, 2) |


    Employees:

    | Column Name | Data Type | |--------------|--------------------------------| | EmployeeID | INT PRIMARY KEY AUTO_INCREMENT | | FirstName | VARCHAR(100) | | LastName | VARCHAR(100) | | DepartmentID | INT | | Salary | DECIMAL(10, 2) |


    Customers:

    | Column Name | Data Type | |-------------|--------------------------------| | CustomerID | INT PRIMARY KEY AUTO_INCREMENT | | FirstName | VARCHAR(100) | | LastName | VARCHAR(100) |


    Orders:

    | Column Name | Data Type | |--------------|--------------------------------| | OrderID | INT PRIMARY KEY AUTO_INCREMENT | | CustomerID | INT | | OrderValue | DECIMAL(10, 2) |

About the author

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight