To join on the same model (table) twice, it is necessary to create a model alias to represent the second instance of the table in a query. Consider the following model:

    When performing a join that uses a , it is necessary to specify the join condition using the keyword argument. In this case we are joining the category with its parent category.

    Using subqueries

    This will generate the following SQL query: