Rafal SGag
Rafal SGag

Reputation: 13

how the query is processed?

Can someone explain step by step how this query is processed?

SELECT F.ID,
       F.FirstName,
       h.UserID,
       h.TYPE,
       L.UserID,
       L.LOGGEDIN
FROM   users AS F
       INNER JOIN history AS h
         ON h.USERID = F.ID
       LEFT JOIN Login AS L
         ON L.USERID = f.MONEY 

If i remove the L.UserID, L.LOGGEDIN and LEFT JOIN it gives me this result:

SELECT F.ID,
       F.FirstName,
       h.UserID,
       h.TYPE
FROM   users AS F
       INNER JOIN history AS h
         ON h.USERID = F.ID 

ID  FirstName   UserID  TYPE
6   rafal            6  A
6   rafal            6  A
6   rafal            6  A
5   rafal            5  B

Does it mean that the result is returned by the INNER Join in the very first step and it is further LEFT Joined?

Upvotes: 1

Views: 73

Answers (2)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115520

Yes. You could also write the FROM part of the query as:

FROM   ( users AS F
         INNER JOIN history AS h
           ON h.USERID = F.ID
       )
       LEFT JOIN Login AS L
         ON L.USERID = f.MONEY 

if that helps you. This is how the FROM part is parsed.

Now the order or processing is first the FROM clause, then the SELECT. For more complicated queries it's:

FROM -> WHERE  -> GROUP BY -> HAVING -> SELECT -> DISTINCT -> 
-> UNION -> ORDER BY -> OFFSET -> LIMIT 

Notice that is does not mean that the DBMS will process the query in this order. It can choose to take a different path. No matter how it processes it though, the result set should be the same as if it was processed this way.

Upvotes: 1

Wrikken
Wrikken

Reputation: 70460

Most likely, yes, run EXPLAIN followed by your query and it will tell you.

Upvotes: 0

Related Questions