nested select sql – What Is a Nested Query in SQL?

nested select sql – A nested SELECT is a query within a query. A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the WHERE clause.

nested select sql

nested select sql A subquery can be nested inside the WHERE or HAVING clause of an outer SELECT , INSERT , UPDATE , or DELETE statement.

SQL – Sub Queries

SELECT column_name [, column_name ]
FROM   table1 [, table2 ]
WHERE  column_name OPERATOR
   (SELECT column_name [, column_name ]
   FROM table1 [, table2 ]
   [WHERE])

Examples of Nested SQL Queries

SELECT AVG(number_of_students)
FROM classes
WHERE member_id IN (
    SELECT id
    FROM members
    WHERE country = 'India' OR country = 'Pakistan');

Consider the UserS table having the following records −

SQL> SELECT * 
   FROM UserS 
   WHERE ID IN (SELECT ID 
         FROM UserS 
         WHERE BALANCE > 4500) ;

Subqueries with the INSERT Statement

SQL> INSERT INTO UserS_BKP
   SELECT * FROM UserS 
   WHERE ID IN (SELECT ID 
   FROM UserS) ;

Subqueries with the UPDATE Statement

Read Also:  DataTables Pagination Server-side Processing using PHP and MySQL

SQL> UPDATE UserS
   SET BALANCE = BALANCE * 0.25
   WHERE POINT IN (SELECT POINT FROM UserS_BKP
      WHERE POINT >= 27 );

don’t Miss : Nested Select Sql Multiple Subqueries

Subqueries with the DELETE Statement

SQL> DELETE FROM UserS
   WHERE POINT IN (SELECT POINT FROM UserS_BKP
      WHERE POINT >= 27 );

SELECT within SELECT Tutorial

SELECT name FROM cricket
  WHERE teams >
     (SELECT teams FROM cricket
      WHERE name='India')

I hope you get an idea about nested select sql.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.