sql nested query code example

Example 1: sql nested select

SELECT * FROM students WHERE notes > ( SELECT AVG(notes) FROM students );

SELECT * FROM students WHERE class_id = (
    SELECT id FROM classes WHERE number_of_students = (
        SELECT MAX(number_of_students) FROM classes
    )
);

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

Example 2: NESTED QUERY SQL

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

Example 3: subquery in sql

SUBQUERY IS NESTED QUERY INSIDE A 
SELECT, INSERT OR UPDATE METHODS.
OR INSIDE ANOTHER SUBQUERY

Tags:

Sql Example