write a nested select statement in sql 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])

Tags:

Sql Example