Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

No, it should come after WHERE, GROUP BY but before ORDER BY. From my favorite SQL tutorial[1]:

The lexical ordering is:

    SELECT
    FROM
    WHERE
    GROUP BY
    HAVING
    UNION
    ORDER BY
while the logical order is:

    FROM
    WHERE
    GROUP BY
    HAVING
    SELECT
    UNION
    ORDER BY
[1] https://blog.jooq.org/10-easy-steps-to-a-complete-understand...


Your example for a row-oriented just adds UNION.

But you need first to order by column to get the rows in the desired order, while SELECT just indicates which column from the rows to return.

source table -> filtered rowset -> grouped rowset -> filtered grouped rowset -> ordered rows -> ordered rows with selected columns only

Source: I was working several years as a Human Query Planner for the columnar DB ;)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: