1
UPDATE employees
   SET job_id = (SELECT job_id
                   FROM employees
                  WHERE employee_id = 205),
       salary = (SELECT salary
                   FROM employees
                  WHERE employee_id = 205)
 WHERE employee_id = 114;

This is the query i have been using. Here i use 2 subqueries but they have the same where condition.. The seek time is doubled.. Is there a way to optimize the whole query to a single subquery?

Thanks in advance

2
  • if you post code, including SQL or XML, please highlight those lines in question and use the "code" button (101 010) on the editor toolbar to nicely format those!! Makes it just soooo much easier to read and understand! Commented Mar 19, 2010 at 9:47
  • @joseph does it (this syntax) work in oracle? stackoverflow.com/questions/2476206/optimize-the-sql-query/… Commented Mar 19, 2010 at 10:29

1 Answer 1

5

you can remove a subquery if you update a set of columns:

UPDATE employees
   SET (job_id, salary) 
        = (SELECT job_id, salary FROM employees WHERE employee_id = 205)
 WHERE employee_id = 114;
Sign up to request clarification or add additional context in comments.

2 Comments

+1 - I didn't know you could do SET (job_id, salary) = SELECT (Job_id, Salary). Thank you.
@Vincent, Is that syntax applicable for SQL server as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.