nested select sql

Today, We want to share with you nested select sql.In this post we will show you sql nested select join, hear for sql multiple nested queries we will give you demo and example for implement.In this post, we will learn about inline query in sql with an example.

sql multiple subqueries in (select statement)

In this tutorial we learn to all about nested select sql Examples like as a SQL – Sub Queries, Subqueries with the INSERT Statement, UPDATE Statement, DELETE Statement, Nested SQL Queries, multiple nested SELECTs in one statement or many more.
Also Read: Nested Queries Subqueries in SQL Example

SQL – Sub Queries

SQL> SELECT * 
   FROM MEMBERS 
   WHERE ID IN (SELECT ID 
         FROM MEMBERS 
         WHERE SALARY > 5487) ;

Subqueries with the INSERT Statement

SQL> INSERT INTO MEMBERS_BKP
   SELECT * FROM MEMBERS 
   WHERE ID IN (SELECT ID 
   FROM MEMBERS) ;

Subqueries with the UPDATE Statement

SQL> UPDATE MEMBERS
   SET SALARY = SALARY * 0.25
   WHERE AGE IN (SELECT AGE FROM MEMBERS_BKP
      WHERE AGE >= 27 );

Subqueries with the DELETE Statement

SQL> DELETE FROM MEMBERS
   WHERE AGE IN (SELECT AGE FROM MEMBERS_BKP
      WHERE AGE >= 27 );

Examples of Nested SQL Queries

SELECT AVG(number_of_pupils)
FROM classes
WHERE teacher_id IN (
    SELECT id
    FROM teachers
    WHERE course = 'Laravel' OR course = 'Vuejs');

multiple nested SELECTs in one statement.

SELECT *
FROM pupils
WHERE class_id = (
    SELECT id
    FROM classes
    WHERE number_of_pupils = (
        SELECT MAX(number_of_pupils)
        FROM classes));

Using subqueries outside of WHERE

SELECT course, MAX(salary_by_course.avg_salary) AS max_salary
FROM (
    SELECT course, AVG(monthly_salary) AS avg_salary
    FROM teachers
    GROUP BY course) salary_by_course;

SQL Nested subqueries

SELECT work_id,AVG(salary) 
FROM members   
GROUP BY work_id   
HAVING AVG(salary)<           
(SELECT MAX(AVG(min_salary))            
FROM works             
WHERE work_id IN                 
(SELECT work_id FROM work_history                  
WHERE department_id                   
BETWEEN 50 AND 100)             
GROUP BY work_id);

Example -2 : Nested subqueries

SELECT req_num,req_date,req_amount,advance_amount
FROM orders 
WHERE req_amount>5014 
AND req_date<'01-AUG-08' 
AND ADVANCE_AMOUNT <         
ANY(SELECT OUTSTANDING_AMT		
FROM MEMBER	
WHERE GRADE=3 
AND CUST_COUNTRY<>'Lodhika'	
AND opening_amt<8457 
AND EXISTS		        
(SELECT * 				 
FROM agents				  
WHERE commission<.12));

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

Leave a Comment