DEV Community

Elayaraj C
Elayaraj C

Posted on

1 1 1 1 1

DAY5 OF PSQL Using Join Query

Types of JOINs in PostgreSQL

  1. INNER JOIN

    Returns only the rows that have matching values in both tables.

    Syntax:

SELECT table1.column1, table2.column2
FROM table1
INNER JOIN table2 ON table1.common_column = table2.common_column;

Example:

SELECT employees.id, employees.name, departments.name AS department
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
Enter fullscreen mode Exit fullscreen mode
  1. LEFT JOIN (LEFT OUTER JOIN)

    Returns all rows from the left table and matching rows from the right table. If no match, it returns NULL for the right table columns.

    Syntax:

SELECT table1.column1, table2.column2
FROM table1
LEFT JOIN table2 ON table1.common_column = table2.common_column;

Example:

SELECT employees.id, employees.name, departments.name AS department
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;
Enter fullscreen mode Exit fullscreen mode
  1. RIGHT JOIN (RIGHT OUTER JOIN)

    Returns all rows from the right table and matching rows from the left table. If no match, it returns NULL for the left table columns.

    Syntax:

SELECT table1.column1, table2.column2
FROM table1
RIGHT JOIN table2 ON table1.common_column = table2.common_column;

Example:

SELECT employees.id, employees.name, departments.name AS department
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;
Enter fullscreen mode Exit fullscreen mode
  1. FULL JOIN (FULL OUTER JOIN)

    Returns all rows when there is a match in either table. If no match, NULL values are returned.

    Syntax:

SELECT table1.column1, table2.column2
FROM table1
FULL JOIN table2 ON table1.common_column = table2.common_column;

Example:

SELECT employees.id, employees.name, departments.name AS department
FROM employees
FULL JOIN departments ON employees.department_id = departments.id;
Enter fullscreen mode Exit fullscreen mode
  1. CROSS JOIN

    Returns the Cartesian product of both tables (each row of table1 is combined with every row of table2).

    Syntax:

SELECT table1.column1, table2.column2
FROM table1
CROSS JOIN table2;

Example:

SELECT employees.name, departments.name AS department
FROM employees
CROSS JOIN departments;
Enter fullscreen mode Exit fullscreen mode
  1. SELF JOIN

    A table joins itself to compare rows.

    Example:

    SELECT e1.name AS employee, e2.name AS manager
    FROM employees e1
    JOIN employees e2 ON e1.manager_id = e2.id;

  2. JOIN using USING (Simplified JOIN)

    If both tables have the same column name, you can use USING instead of ON.

    Example:

SELECT employees.id, employees.name, departments.name AS department
FROM employees
JOIN departments USING (department_id);

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay