![]() When the value in the manager_id column is null, that employee does not report to anyone. The value in the manager_id column shows the manager to whom the employee directly reports. In this employee table, the manager_id column references the employee_id column. ( 8, 'Salley', 'Lester', 3) Code language: SQL (Structured Query Language) ( sql ) The following statements create the employee table and insert some sample data into the table. Suppose, you have the following organizational structure: Let’s set up a sample table for the demonstration. Let’s take some examples of using self-joins. ![]() LEFT JOIN table_name t2 ON join_predicate Code language: SQL (Structured Query Language) ( sql ) PostgreSQL self-join examples In this syntax, the table_name is joined to itself using the INNER JOIN clause.Īlso, you can use the LEFT JOIN or RIGHT JOIN clause to join table to itself like this: SELECT select_list INNER JOIN table_name t2 ON join_predicate Code language: SQL (Structured Query Language) ( sql ) The following query uses an INNER JOIN that joins the table to itself: SELECT select_list To form a self-join, you specify the same table twice with different table aliases and provide the join predicate after the ON keyword. In practice, you typically use a self-join to query hierarchical data or to compare rows within the same table. Introduction to PostgreSQL self-joinĪ self-join is a regular join that joins a table to itself. Summary: in this tutorial, you will learn how to use the PostgreSQL self-join technique to compare rows within the same table.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |