Summary: In this tutorial, you will learn about SQL subquery and how to use subqueries to form flexible SQL statements.
Basic SQL Subquery
Consider the followingEmployees
Ydepartments
tables ofdefault database:

Suppose you need to find all employees in location ID 1700. The following solution might come to your mind.
First find all apartments on site with ID 1700:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSE*OUTSIDE OFdepartmentsOSlocation_id =1700;

Second, find all employees belonging to location 1700 using the list of department IDs from the query above:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEEmployee_ID, First Name, Last NameOUTSIDE OFEmployeesOSdepartment idEM(1,3,8,10,11)DOMAIN VONName surname;

This solution has two problems. first you looked at themdepartments
Table to check which department belongs to site 1700. However, the original question did not refer to specific departments; he was referring to the 1700 location.
Due to the small volume of data, you can easily get a list of departments. However, in real system with large amounts of data, this can be problematic.
Another problem was that every time you want to find employees who are in a different location, you have to go through queries.
A much better solution to this problem is to use a subquery. By definition, a subquery is a query nested within another query, for exampleTO CHOOSE
,INSERTION
,TO UPDATE
, oEXTINGUISH
Opinion. In this tutorial, we will focus on the subquery used with theTO CHOOSE
Opinion.
In this example, you can rewrite the above two queries as follows:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEEmployee_ID, First Name, Last NameOUTSIDE OFEmployeesOSdepartment idEM(TO CHOOSEdepartment idOUTSIDE OFdepartmentsOSlocation_id =1700)DOMAIN VONName surname;
The query enclosed in parentheses is called a subquery. It is also known as inner query or inner select. The query that contains the subquery is called an outer query or outer select.
To run the query, the database system must first run the subquery, replacing the subquery in parentheses with its result, a department ID number, which is at position 1700, and then run the outer query.
You can use a subquery in many places, for example for example.:
- With him
EM
oNOT IN
Operator - Swindlercomparison operators
- With him
AVAILABLE
oDOES NOT EXIST
Operator - With him
ANY
oEM
Operator - Military
OUTSIDE OF
clause - Military
TO CHOOSE
clause
Examples of SQL subqueries
Let's give some examples of using subqueries to understand how they work.
Subconsulta SQL con operador IN o NOT IN
In the example above, you saw the subquery used withEM
Operator. The following example uses a subquery with theNOT IN
Operator to find all employees not in location 1700:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEEmployee_ID, First Name, Last NameOUTSIDE OFEmployeesOSdepartment idNO EM(TO CHOOSEdepartment idOUTSIDE OFdepartmentsOSlocation_id =1700)DOMAIN VONName surname;

SQL subquery with comparison operator
The following syntax illustrates how to use a subquery with a comparison operator:
Code message: SQL (Structured Query Language) (sql)
Comparison operator (subquery)
whereincomparison operatoris one of these operators:
- equal (=)
- greater than (>)
- less than (<)
- Greater than or equal to (>=)
- Less than equals (<=)
- Not equal to (!=) or (<>)
The following example finds the employees with the highest salary:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEEmployee_ID, First Name, Last Name, SalaryOUTSIDE OFEmployeesOSsalary = (TO CHOOSE maximum(Wage)OUTSIDE OFEmployees)DOMAIN VONName surname;

In this example, the subquery returns the highest salary of all employees, and the outer query finds employees whose salary equals the highest.
The following statement finds all employees whose salaries are greater than the average salary of all employees:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEEmployee_ID, First Name, Last Name, SalaryOUTSIDE OFEmployeesOSSalary > (TO CHOOSE average(Wage)OUTSIDE OFEmployees);

In this example, the subquery first returns the average salary of all employees. The outer query uses the greater than operator to find all employees whose salaries are above average.
SQL-Unterabfrage sends either EXIST or NOT EXIST operator
HeAVAILABLE
The operator checks if there are any rows returned by the subquery. Returns true if the subquery contains rows. Otherwise, false is returned.
The syntax ofAVAILABLE
The operator is as follows:
Code message: SQL (Structured Query Language) (sql)
EXISTS (subquery)
HeDOES NOT EXIST
The operator is the opposite ofAVAILABLE
Operator.
Code message: SQL (Structured Query Language) (sql)
DOES NOT EXIST (subquery)
The following example finds all departments that have at least one employee with a salary greater than 10,000:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEdepartment nameOUTSIDE OFdepartments ofOS AVAILABLE(TO CHOOSE 1 OUTSIDE OFemployees, for exampleOSsalary >10000 Ye.department_id = d.department_id)DOMAIN VONDepartment name;

Likewise, the following statement finds all departments that do not have an employee with a salary greater than 10,000:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEdepartment nameOUTSIDE OFdepartments ofOS NO AVAILABLE(TO CHOOSE 1 OUTSIDE OFemployees, for exampleOSsalary >10000 Ye.department_id = d.department_id)DOMAIN VONDepartment name;
SQL subquery with the ALL operator
The subquery syntax when used with theEM
The operator is as follows:
Code message: SQL (Structured Query Language) (sql)
ALL comparison operator (subquery)
The following condition evaluates to true ifx
is greater than any value returned by the subquery.
Code message: SQL (Structured Query Language) (sql)
x > ALL (Unterabfragge)
Suppose the subquery returns three values, one, two, and three. The following condition evaluates to true ifx
is greater than 3.
Code message: SQL (Structured Query Language) (sql)
x > ON (1,2,3)
The following query uses theGROUP BY
clause andMINIMUM()
Function to find the lowest salary by department:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSE MINIMUM(Wage)OUTSIDE OFEmployeesGROUP VONdepartment idDOMAIN VON MINIMUM(Wage)DESCRIPTION;

The following example finds all employees whose salaries are greater than the lowest salary in each department:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEEmployee_ID, First Name, Last Name, SalaryOUTSIDE OFEmployeesOSSalary >=EM(TO CHOOSE MINIMUM(Wage)OUTSIDE OFEmployeesGROUP VONdepartment ID)DOMAIN VONName surname;

SQL-Unterabfrage with the ANY operator
Below is the syntax for a subquery using theANY
Operator:
Code message: SQL (Structured Query Language) (sql)
ANY comparison operator (subquery)
For example, the following condition evaluates to true if x is greater than any value returned by the subquery. So the conditionx > SOME (1,2,3)
evaluates to true if x is greater than 1.
Code message: SQL (Structured Query Language) (sql)
x > QUALQUER (subquery)
Note that theSOME
operator representsANY
operator so you can use them interchangeably.
The following query finds all employees whose salaries are greater than or equal to the highest salary in each department.
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEEmployee_ID, First Name, Last Name, SalaryOUTSIDE OFEmployeesOSSalary >=SOME(TO CHOOSE maximum(Wage)OUTSIDE OFEmployeesGROUP VONdepartment identification);

In this example, the subquery finds the highest salary for employees in each department. The external query looks at these values and determines which employee salaries are greater than or equal to the highest salary per department.
SQL subquery in FROM clause
You can use a subquery inOUTSIDE OF
clause ofTO CHOOSE
declaration as follows:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSE*OUTSIDE OF(subquery)Etable name
In this syntax, thetable nicknamesis necessary because all the tables in theOUTSIDE OF
The clause must have a name.
Note that thesubquerydeclared notOUTSIDE OF
Clause means aderived table in mysqloOnline Preview in Oracle.
The following statement returns the average salary for each department:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSE average(salary) average_salaryOUTSIDE OFEmployeesGROUP VONdepartment identification;

You can use this query as a subquery inOUTSIDE OF
Clause for calculating the average salary of the departments as follows:
Code message: SQL (Structured Query Language) (sql)
TO CHOOSE REDONDO(average(average salary)0)OUTSIDE OF(TO CHOOSE average(salary) average_salaryOUTSIDE OFEmployeesGROUP VONdepartment ID) department salary;

SQL subquery in SELECT clause
A subquery can be used anywhere an expression can be used in theTO CHOOSE
-Clause. The following example finds the salaries of all employees, their average salary, and the difference between each employee's salary and the average salary.
Code message: SQL (Structured Query Language) (sql)
TO CHOOSEEmployees_id, first, last, salary, (TO CHOOSE REDONDO(average(Wage),0)OUTSIDE OFemployees) average_salary, salary - (TO CHOOSE REDONDO(average(Wage),0)OUTSIDE OFEmployees)DifferenceOUTSIDE OFEmployeesDOMAIN VONName surname;

Now you should understand what an SQL subquery is and how to use subqueries to create flexible SQL statements.
Was this guide helpful?