SQL

The SQL language has several parts:

(Silberschatz et al. 2019, 7:66 chap.3 part.3.1)

Logical Query Processing Order

FROM and JOIN

Step Description
1 Cartesian product
2 Qualification step (i.e. the ON filter)
3 LEFT or RIGHT reservations

If the specified JOIN type is a CROSS JOIN, then the query planner stops at Step 1 and return the cartesian product to the next clause. All other join types proceed to the qualification step.

INNER and OUTER JOINS are called "qualified JOINs", the qualification predicated is specified using the ON keyword, which filters all rows from the previous cartesian step where the predicate is TRUE.

If the request is an INNER JOIN, then the qualified rows are returned to the next clause, if it is an OUTER JOIN, the qualified rows go throught the next step.

Chiastic Order

WHERE

GROUP BY

Stream/Group Aggregation

  • Requires input streams to be sorted.
  • Useful for "Top-N" queries, as it is more memory efficient due to its streaming behaviour.
  • It's called GroupAggregate on the EXPLAIN ANALYZE output.

Hash-based Aggregation

  • Builds a temporary in-memory hash table using the conditions from GROUP BY.
  • Does not require the input stream to be sorted.
  • It's called HashAggregate on the EXPLAIN ANALYZE output.

HAVING

  • Acts as a WHERE clause on the outputs generated by the GROUP BY.

Window Functions

SELECT

DISTINCT

  • Can either happen via a HashAggregate or a combination of SORT/UNIQUE.

UNION / UNION ALL

ORDER BY

OFFSET

LIMIT / FETCH ROWS / TOP

References:

Silberschatz, Abraham, Henry F Korth, Shashank Sudarshan, and others. 2019. Database System Concepts. Vol. 7. McGraw-Hill New York.

Backlinks: