PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - LEFT JOIN

In PostgreSQL, as in many other relational database systems, the LEFT JOIN or LEFT OUTER JOIN operation returns all rows from the left table (the table listed before the JOIN keyword) and the matched rows from the right table (the table listed after the JOIN keyword). If there is no match for a particular row in the left table, the columns for the right table will contain NULL values.

Syntax:

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;

Example:

Let's say we have two tables, students and registrations.

The students table:

student_idstudent_name
1Alice
2Bob
3Charlie

The registrations table:

reg_idcoursestudent_id
1Math1
2History1
3Science3

To find out which students are registered for which courses, and also include students who are not registered for any course, we'd use a LEFT JOIN:

SELECT s.student_name, r.course
FROM students s
LEFT JOIN registrations r ON s.student_id = r.student_id;

The result would be:

student_namecourse
AliceMath
AliceHistory
BobNULL
CharlieScience

Note that:

  • Alice appears twice because she's registered for two courses.
  • Bob appears once with a NULL course because he's not registered for any course.
  • Charlie appears once, registered for Science.

Key Points:

  • LEFT JOIN returns all rows from the left table, whether or not there's a matching row in the right table.

  • If there's no match for a particular row in the left table, the result will contain NULL values for columns from the right table.

  • This type of join is particularly useful when you want to list all items from one table and any corresponding matches (or lack thereof) from another table.

  1. PostgreSQL LEFT JOIN example:

    • Perform a basic LEFT JOIN between two tables.
    SELECT * FROM table1
    LEFT JOIN table2 ON table1.column_name = table2.column_name;
    
  2. LEFT JOIN vs. INNER JOIN in PostgreSQL:

    • LEFT JOIN returns all records from the left table, while INNER JOIN returns only the matching records.
    SELECT * FROM table1
    LEFT JOIN table2 ON table1.column_name = table2.column_name;
    
  3. Multiple tables LEFT JOIN in PostgreSQL:

    • Extend LEFT JOIN to involve multiple tables.
    SELECT * FROM table1
    LEFT JOIN table2 ON table1.column_name = table2.column_name
    LEFT JOIN table3 ON table1.column_name = table3.column_name;
    
  4. Filtering results with LEFT JOIN in PostgreSQL:

    • Apply additional filtering conditions to the LEFT JOIN results.
    SELECT * FROM orders
    LEFT JOIN customers ON orders.customer_id = customers.customer_id
    WHERE customers.country = 'USA';
    
  5. Using aliases with LEFT JOIN in PostgreSQL:

    • Simplify queries using table aliases with LEFT JOIN.
    SELECT * FROM employees AS e
    LEFT JOIN departments AS d ON e.department_id = d.department_id;
    
  6. LEFT JOIN and WHERE clause in PostgreSQL:

    • Filter results using conditions in the WHERE clause after a LEFT JOIN.
    SELECT * FROM customers
    LEFT JOIN orders ON customers.customer_id = orders.customer_id
    WHERE orders.order_id IS NULL;
    
  7. LEFT JOIN with aggregate functions in PostgreSQL:

    • Utilize aggregate functions with LEFT JOIN for summary data.
    SELECT department_name, COUNT(employee_id) AS employee_count
    FROM departments
    LEFT JOIN employees ON departments.department_id = employees.department_id
    GROUP BY department_name;
    
  8. Nested LEFT JOIN in PostgreSQL:

    • Perform a LEFT JOIN within another LEFT JOIN for more complex relationships.
    SELECT * FROM orders
    LEFT JOIN customers ON orders.customer_id = customers.customer_id
    LEFT JOIN employees ON customers.salesperson_id = employees.employee_id;
    
  9. Combining LEFT JOIN with other JOIN types in PostgreSQL:

    • Combine LEFT JOIN with other types like INNER JOIN or RIGHT JOIN.
    SELECT * FROM table1
    LEFT JOIN table2 ON table1.column_name = table2.column_name
    INNER JOIN table3 ON table1.column_name = table3.column_name;
    
  10. LEFT JOIN on non-primary key columns in PostgreSQL:

    • LEFT JOIN can be used on columns other than primary keys.
    SELECT * FROM employees
    LEFT JOIN departments ON employees.department_name = departments.department_name;
    
  11. JOIN conditions and NULL values in LEFT JOIN in PostgreSQL:

    • Be cautious with NULL values when using LEFT JOIN.
    SELECT * FROM orders
    LEFT JOIN customers ON orders.customer_id = customers.customer_id;
    
  12. Using LEFT JOIN with subqueries in PostgreSQL:

    • Integrate LEFT JOIN with subqueries for advanced queries.
    SELECT * FROM employees
    LEFT JOIN (SELECT employee_id, AVG(salary) AS avg_salary FROM salaries GROUP BY employee_id) AS avg_salaries
    ON employees.employee_id = avg_salaries.employee_id;
    
  13. ORDER BY with LEFT JOIN in PostgreSQL:

    • Use ORDER BY to sort results of a LEFT JOIN.
    SELECT * FROM products
    LEFT JOIN reviews ON products.product_id = reviews.product_id
    ORDER BY products.product_name;
    
  14. Comparing LEFT JOIN with EXISTS in PostgreSQL:

    • Compare the use of LEFT JOIN with the EXISTS condition.
    SELECT * FROM employees
    WHERE EXISTS (SELECT 1 FROM salaries WHERE salaries.employee_id = employees.employee_id);
    
  15. LEFT JOIN and foreign key relationships in PostgreSQL:

    • Utilize LEFT JOIN for querying tables with foreign key relationships.
    SELECT * FROM orders
    LEFT JOIN customers ON orders.customer_id = customers.customer_id;