PostgreSQL: Using INNER JOIN with WHERE clause

Updated: January 5, 2024 By: Guest Contributor Post a comment

Overview

In PostgreSQL, combining the power of INNER JOIN and WHERE clauses allows you to query for records that match a particular condition across multiple tables. This tutorial walks you through various scenarios ranging from basic to advanced.

Introduction to INNER JOIN and WHERE Clause

The INNER JOIN clause in PostgreSQL is used to combine rows from two or more tables based on a related column between them. The WHERE clause is then utilized to filter the results that meet a specified condition. When combined, you can perform complex queries that bring actionable insights from your relational data.

Basic Syntax:

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

Example: Imagine you have two tables, employees and departments, and you want to find all employees who work in the ‘Sales’ department.

SELECT employees.name, employees.role
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id
WHERE departments.name = 'Sales';

Basic INNER JOIN with WHERE Clause

Let’s delve into a practical example using the classic employees and departments relational dataset.

Example: Retrieve the names and roles of all employees in the ‘Marketing’ department:

SELECT employees.name, employees.role
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id
WHERE departments.name = 'Marketing';

Here, you’re joining the two tables on the common department_id column and filtering the results for the ‘Marketing’ department using the WHERE clause.

Combining Multiple Conditions

You can also combine multiple conditions within your WHERE clause to further refine your query.

Example: To find employees in the ‘IT’ department with a salary greater than $50000:

SELECT employees.name, employees.salary
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id
WHERE departments.name = 'IT'
AND employees.salary > 50000;

INNER JOIN on Multiple Tables

In complex databases, you may need to join more than two tables to get the desired data.

Example: If there’s a third table, locations, related to departments, to find employees who work at a specific location, you’d:

SELECT employees.name, locations.city
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id
INNER JOIN locations
ON departments.location_id = locations.id
WHERE locations.city = 'New York';

Inner Join Using Aliases

To simplify your queries and enhance readability, you can use aliases for table names.

Example: The previous query with aliases looks like this:

SELECT e.name, l.city
FROM employees e
INNER JOIN departments d
ON e.department_id = d.id
INNER JOIN locations l
ON d.location_id = l.id
WHERE l.city = 'New York';

Advanced Filtering with Subqueries

Subqueries can be powerful, especially when used in a WHERE clause in conjunction with an INNER JOIN.

Example: Finding employees who work in departments with the highest number of employees:

SELECT e.name, d.name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.id
WHERE d.id IN (
    SELECT department_id
    FROM employees
    GROUP BY department_id
    HAVING COUNT(*) = (
        SELECT MAX(emp_count)
        FROM (
            SELECT department_id, COUNT(*) as emp_count
            FROM employees
            GROUP BY department_id
        ) as dept_counts
    )
);

Conclusion

Mastering INNER JOIN and WHERE clauses enables you to craft efficient and powerful queries in PostgreSQL. Progressing from basic join operations to incorporating subqueries lays the foundation for adept database management and analysis.