
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we nest a subquery within another subquery?
If a subquery is nested inside another subquery then it is called a nested subquery. To make it understand we are creating the nested subquery from the following tables data −
mysql> Select * from Cars; +------+--------------+---------+ | ID | Name | Price | +------+--------------+---------+ | 1 | Nexa | 750000 | | 2 | Maruti Swift | 450000 | | 3 | BMW | 4450000 | | 4 | VOLVO | 2250000 | | 5 | Alto | 250000 | | 6 | Skoda | 1250000 | | 7 | Toyota | 2400000 | | 8 | Ford | 1100000 | +------+--------------+---------+ 8 rows in set (0.02 sec) mysql> Select * from Customers; +-------------+----------+ | Customer_Id | Name | +-------------+----------+ | 1 | Rahul | | 2 | Yashpal | | 3 | Gaurav | | 4 | Virender | +-------------+----------+ 4 rows in set (0.00 sec) mysql> Select * from Reservations; +------+-------------+------------+ | ID | Customer_id | Day | +------+-------------+------------+ | 1 | 1 | 2017-12-30 | | 2 | 2 | 2017-12-28 | | 3 | 2 | 2017-12-29 | | 4 | 1 | 2017-12-25 | | 5 | 3 | 2017-12-26 | +------+-------------+------------+ 5 rows in set (0.00 sec)
Below is a nested subquery −
mysql> Select * from Customers where customer_id IN (Select Customer_id from reservations where id = (Select id from cars WHERE name = 'BMW')); +-------------+---------+ | Customer_Id | Name | +-------------+---------+ | 2 | Yashpal | +-------------+---------+ 1 row in set (0.00 sec)
- Related Articles
- How can we create a MySQL view with a subquery?
- How can we use a MySQL subquery with INSERT statement?
- How can we use a MySQL subquery with FROM clause?
- How can we filter data with the help of MySQL subquery?
- How can we test for the existence of any record in MySQL subquery?
- Subquery in SQL
- How can I use MySQL subquery as a table in FROM clause?
- How to correctly enclose subquery in MySQL?
- How can we use a subquery that contains a reference to a table that also appears in the outer query?
- How MySQL evaluates if we use EXISTS operator with a subquery that returns no rows?
- How MySQL evaluates if we use EXISTS operator with the subquery that returns NULL?
- Subquery to exclude a particular row in MySQL
- MySQL SELECT from a subquery and then perform DELETE?
- How do I return multiple results in a MySQL subquery with IN()?
- Get second largest marks from a MySQL table using subquery?

Advertisements