How to Join Three Tables in PostgreSQL: A Step-by-Step Guide
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Joining three tables in PostgreSQL is a crucial skill for efficiently combining data from various sources. owever, joining three or more tables can introduce complexity. Let’s explore strategies to overcome these challenges and ensure your queries retrieve the desired data efficiently.
The JOIN clause combines rows from two or more tables based on a related column between them. INNER JOIN, the most common type, returns rows when matches exist in both tables.
Consider three tables: employees, departments, and projects, where employees belong to departments and work on projects. Key columns include:
employees: employee_id, name, department_iddepartments: department_id, department_nameprojects: project_id, project_name, employee_idTo combine these tables, use two JOIN clauses. The SQL query below selects all employees along with their departments and assigned projects:
SELECT employees.name, departments.department_name, projects.project_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id
INNER JOIN projects ON employees.employee_id = projects.employee_id;
Here, the first INNER JOIN merges employees with departments using department_id. The second INNER JOIN then connects to projects through employee_id.
Incorporate WHERE clauses for specific criteria and ORDER BY clauses for sorting. For example, to list ‘Engineering’ department employees by project:
SELECT employees.name, departments.department_name, projects.project_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id
INNER JOIN projects ON employees.employee_id = projects.employee_id
WHERE departments.department_name = 'Engineering'
ORDER BY projects.project_name;
This adjusted approach streamlines data from different relational database areas, optimizing your queries to meet diverse data retrieval requirements.
Written by
Senior Engineer at Basedash
Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.