>
Part | Description |
comparison | An expression and a comparison operator that compares the expression with the results of the subquery. |
expression | An expression for which the result set of the subquery is searched. |
sqlstatement | A SELECT statement, following the same format and rules as any other SELECT statement. It must be enclosed in parentheses. |
SELECT * FROM ProductsUse the ALL predicate to retrieve only those records in the main query that satisfy the comparison with all records retrieved in the subquery. If you changed ANY to ALL in the above example, the query would return only those products whose unit price is greater than that of all products sold at a discount of 25 percent or more. This is much more restrictive. Use the IN predicate to retrieve only those records in the main query for which some record in the subquery contains an equal value. The following example returns all products with a discount of 25 percent or more:
WHERE UnitPrice > ANY
(SELECT UnitPrice FROM OrderDetails
WHERE Discount >= .25);
SELECT * FROM ProductsConversely, you can use NOT IN to retrieve only those records in the main query for which no record in the subquery contains an equal value. Use the EXISTS predicate (with the optional NOT reserved word) in true/false comparisons to determine whether the subquery returns any records. You can also use table name aliases in a subquery to refer to tables listed in a FROM clause outside the subquery. The following example returns the names of employees whose salaries are equal to or greater than the average salary of all employees having the same job title. The Employees table is given the alias "T1."
WHERE ProductID IN
(SELECT ProductID FROM OrderDetails
WHERE Discount >= .25);
SELECT LastName,FirstName, Title, SalaryIn the preceding example, the AS reserved word is optional. Some subqueries are allowed in crosstab queries — specifically, as predicates (those in the WHERE clause). Subqueries as output (those in the SELECT list) are not allowed in crosstabs. See Also INNER JOIN Operation; LEFT JOIN, RIGHT JOIN Operations; SELECT Statement; SELECT...INTO Statement; UNION Operation. Example Some of the following examples assume the existence of a hypothetical Salary field in an Employees table. This example lists the name, title, and salary of every sales representative whose salary is higher than that of all managers and directors.
FROM Employees AS T1
WHERE Salary >=(SELECT Avg(Salary)
FROM Employees
WHERE T1.Title = Employees.Title) Order by Title;
SELECT LastName, FirstName, Title, Salary FROM EmployeesThis example lists the name and unit price of every product whose unit price is the same as that of Aniseed Syrup.
WHERE Title LIKE "*Sales Rep*" AND Salary > ALL (SELECT Salary
FROM Employees
WHERE (Title LIKE "*Manager*") OR (Title LIKE "*Director*"));
SELECT ProductName, UnitPrice FROM ProductsThis example lists the company and contact of every customer who placed an order in the second quarter of 1995.
WHERE UnitPrice = (SELECT UnitPrice FROM [Products]
WHERE ProductName = "Aniseed Syrup");
SELECT ContactName, CompanyName, ContactTitle, PhoneThis example lists employees whose salary is higher than the average salary for all employees.
FROM Customers WHERE CustomerID IN (SELECT CustomerID FROM Orders
WHERE OrderDate BETWEEN #04/1/95# AND #07/1/95#);
SELECT LastName, FirstName, Title, Salary FROM Employees T1This example selects the name of every employee who has booked at least one order. This could also be done with an INNER JOIN.
WHERE Salary >= (SELECT AVG(Salary) FROM Employees
WHERE Employees.Title = T1.Title) ORDER BY Title;
SELECT FirstName, LastName FROM Employees AS E
WHERE EXISTS (SELECT * FROM Orders AS O
WHERE O.EmployeeID = E.EmployeeID);