Yes, you can choose the values from a row depending on whether another row is distinct using a subquery. Here is an example:
Suppose you have a table called "employee" with columns "name", "salary", and "department". You want to select the salary of all employees in departments where there is at least one employee with a salary greater than $100,000.
You can achieve this using the following query:
SELECT salary
FROM employee
WHERE department IN (
SELECT DISTINCT department
FROM employee
WHERE salary > 100000
)
The subquery selects the distinct departments where there is at least one employee with a salary greater than $100,000. The outer query then selects the salary of all employees in those departments.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-30 10:32:41 +0000
Seen: 11 times
Last updated: May 30 '23