In SQL, the ALL keyword is used in conjunction with comparison operators to compare a value with a set of results returned by a subquery. It allows the comparison of a value to all values in the result set. It can be used with operators such as =, !=, >, <, >=, and <=.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE expression operator ALL (subquery);
Key Points:
- The
ALLkeyword can be used with any comparison operator. - It applies the comparison to all the results returned by the subquery.
- The subquery must return a result set for
ALLto compare against.
Example Usage:
1. ALL with > (Greater than):
SELECT employee_name, salary
FROM employees
WHERE salary > ALL (SELECT salary FROM employees WHERE department = 'Sales');
This query retrieves the names and salaries of employees who earn more than everyone in the 'Sales' department.
2. ALL with = (Equal to):
SELECT product_name
FROM products
WHERE price = ALL (SELECT price FROM products WHERE category = 'Electronics');
This query retrieves the names of products that have the same price as all products in the 'Electronics' category.
How ALL works:
- The query will return the result only if the comparison is true for every value in the result set of the subquery.
- For example, if using
>withALL, the condition will only be true if the main query’s value is greater than every value returned by the subquery.
Let me know if you'd like further clarification!
No comments:
Post a Comment