SQL Query

SQL Server: How to Join Two Tables in SQL

SQL Server guide: Combine data from multiple tables using INNER JOIN, LEFT JOIN, or RIGHT JOIN.

This guide is specifically for SQL Server syntax.

How to Join Two Tables in SQL

Combine data from multiple tables using INNER JOIN, LEFT JOIN, or RIGHT JOIN.

Quick Answer

SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.table1_id;

Explanation

INNER JOIN returns only matching rows. LEFT JOIN keeps all left table rows. RIGHT JOIN keeps all right table rows. Use table aliases for cleaner queries.

Query Variants

Inner Join

SELECT orders.id, customers.name FROM orders INNER JOIN customers ON orders.customer_id = customers.id;

Left Join

SELECT customers.name, orders.id FROM customers LEFT JOIN orders ON customers.id = orders.customer_id;

Multiple Joins

SELECT o.id, c.name, p.product_name FROM orders o JOIN customers c ON o.customer_id = c.id JOIN products p ON o.product_id = p.id;

Pro Tips

  • Always specify join conditions with ON
  • Use table aliases for readability
  • Consider NULL handling with outer joins

Related SQL Queries

Continue learning with more SQL query examples:


SQL Server-Specific Notes

This page covers SQL Server syntax. Other databases may have different syntax for similar operations.

Related Content

From Our Blog

Try it yourself

Practice this query in our interactive SQL sandbox.