Repaso
EspaƱol
Databases & SQLIntermediateDB-42

In what logical order does SQL evaluate a query?

Options

  1. SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
  2. FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT (Correct option)
  3. ORDER BY, SELECT, FROM, WHERE, GROUP BY, HAVING
  4. WHERE, SELECT, FROM, HAVING, GROUP BY, ORDER BY

Answer

FROM/JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT

Why

You write SELECT first, but the database processes it later:

  1. FROM / JOIN: choose and combine tables.
  2. WHERE: filter rows.
  3. GROUP BY: form groups.
  4. HAVING: filter groups.
  5. SELECT: compute columns and aliases.
  6. ORDER BY: sort.
  7. LIMIT: cut.

This explains why you cannot use a SELECT alias inside WHERE, but you can in ORDER BY.

Remember

FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.

Key terms

Practice this question in Repaso