DEV Community

Cover image for PostgreSQL - JOIN day4
Keerthana M
Keerthana M

Posted on

PostgreSQL - JOIN day4

SQL JOINS:

  • SQL Joins are used to combine data from two or more tables based on a related column.

why join:

  • Retrieving connected data stored across multiple tables.
  • Matching records using common columns.
  • Improving data analysis by combining related information.
  • Creating meaningful result sets from separate tables.

1. INNER JOIN

  • INNER JOIN is used to combine rows from two or more tables based on a related column.
  • It returns only the rows that have matching values in both tables, filtering out non-matching records.
  • It is commonly used in relational databases and useful for working with related data.


select * from product INNER JOIN customer on product_id = cus_id;
Enter fullscreen mode Exit fullscreen mode

output:

OUTER JOIN:

  1. LEFT JOIN
  2. RIGHT JOIN
  3. FULL JOIN

LEFT JOIN:

LEFT JOIN returns all rows from the left table and matching rows from the right table.

  • Returning all records from the left table.
  • Displaying matching records from the right table.
  • Showing NULL values when no matching record exists.


select * from product LEFT JOIN customer on product_id = cus_id;
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

RIGHT JOIN
RIGHT JOIN returns all rows from the right table and the matching rows from the left table.

  • Returning all records from the right table.
  • Displaying matching records from the left table.
  • Showing NULL values where no match exists.
  • Retrieving complete data from the right table.


select product_id,product_name from product RIGHT JOIN customer on product_id = cus_id;
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

FULL JOIN :

FULL JOIN combines the results of both LEFT JOIN and RIGHT JOIN.

Returning all rows from both tables.
Displaying matching records from both tables.
Showing NULL values where no match exists.
Retrieving complete information from both tables.
Enter fullscreen mode Exit fullscreen mode


select * from product FULL OUTER JOIN customer on product.price = customer.price;

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

Top comments (0)