DEV Community

Cover image for PostgreSQL - JOIN day4
R.Shobika CSE
R.Shobika CSE

Posted on

PostgreSQL - JOIN day4

JOIN:

  • join clause is used to combine the two or more table based on related column.

  1. INNER JOIN:
  • The INNER JOIN keyword selects records that have matching values in both tables.
select * from product INNER JOIN customer on product_id = cus_id;
Enter fullscreen mode Exit fullscreen mode

output:

  1. LEFT JOIN:
  • The LEFT JOIN keyword selects ALL records from the "left" table, and the matching records from the "right" table. The result is 0 records from the right side if there is no match.
select * from product LEFT JOIN customer on product_id = cus_id;

Enter fullscreen mode Exit fullscreen mode

output:

  1. RIGHT JOIN:
  • The RIGHT JOIN keyword selects ALL records from the "right" table, and the matching records from the "left" table. The result is 0 records from the left side if there is no match.
select product_id,product_name from product RIGHT JOIN customer on product_id = cus_id;

Enter fullscreen mode Exit fullscreen mode

output:

  1. OUTER JOIN:
  • The FULL JOIN keyword selects ALL records from both tables, even if there is not a match. For rows with a match the values from both tables are available, if there is not a match the empty fields will get the value NULL.
select * from product FULL OUTER JOIN customer on product.price = customer.price;

Enter fullscreen mode Exit fullscreen mode

output:

To Be Discussed:

  1. Having

  2. CROSS JOIN

Top comments (0)