Ask Your Question
3

Can you choose the values from a row depending on whether another row is distinct?

asked 2023-05-30 10:32:41 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-30 10:41:01 +0000

ladyg gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-05-30 10:32:41 +0000

Seen: 11 times

Last updated: May 30 '23