SQL Query to Join Two Tables
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: