What is executed first in SQL?
Explanation
FROM clause is executed first because it forms the set of records all the following clauses (like WHERE and SELECT) will operate over.
Theory
  • SQL Query Order of Operations

    The order in which SQL directives get executed:
    • FROM clause
    • WHERE clause
    • GROUP BY clause
    • HAVING clause
    • SELECT clause
    • ORDER BY clause
    Read more: SQL Query Order of Operations
  • Optimization - FROM Clause

    Since this clause executes first, it is our first opportunity to narrow down possible record set sizes. This is why I put as many of my ON rules (for joins) as possible in this area as opposed to in the WHERE clause:
    FROM
        contact c
    INNER JOIN
        display_status d
    ON
        (
                c.display_status_id = d.id
            AND
                d.is_active = 1
            AND
                d.is_viewable = 1 
        )
    
    This way, by the time we get to the WHERE clause, we will have already excluded rows where is_active and is_viewable do not equal 1.
    Read more: SQL Query Order of Operations

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з SQL
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести