In what sequence are queries executed by the SQL engine?

  • SELECT * FROM employees
    where salary in (select min_salary from jobs where jobs.Job_ID = employees.Job_ID)

    can anyone explain this for me?

    especially in

    jobs.Job_ID = employees.Job_ID

    what result once the SQL engine execute the subquery statement above?


    Jobs Table


    Employees Table

  • Did you setup the 2 tables and try it to see what results you would get?

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • The way this query is written is using what's called a correlated subquery, in which each row of table Employees will cause an execution of the subquery.  Is the Job_ID column unique in the Jobs table?  If so, this probably should just be written as a JOIN instead of a subquery.
    SELECT e.*
      FROM Employees e
        INNER JOIN Jobs j ON e.Job_ID = j.Job_ID
      WHERE e.salary = j.min_salary)

  • There is an order in which things are executed. It'll help interpret any query you're having problems with.
    FROM
    WHERE
    GROUP BY
    HAVING
    SELECT
    ORDER BY

    There's a break down in the FROM and SELECT where other processing occurs, but that's the basics. I'd suggest getting a copy of Itzik Ben-Gan's book on T-SQL.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply