How do you join two tables? Please give an example.
💡 Model Answer
In SQL, joining two tables allows you to combine rows that share a related column. The most common join types are INNER JOIN, LEFT (OUTER) JOIN, RIGHT (OUTER) JOIN, and FULL (OUTER) JOIN. For example, suppose you have a Customers table (CustomerID, Name) and an Orders table (OrderID, CustomerID, OrderDate). To retrieve all orders along with the customer name, you would write:
SELECT c.CustomerID, c.Name, o.OrderID, o.OrderDate
FROM Customers c
INNER JOIN Orders o ON c.CustomerID = o.CustomerID;
This INNER JOIN returns only rows where a matching CustomerID exists in both tables. If you want to include customers who have no orders, use a LEFT JOIN:
SELECT c.CustomerID, c.Name, o.OrderID, o.OrderDate
FROM Customers c
LEFT JOIN Orders o ON c.CustomerID = o.CustomerID;
The LEFT JOIN returns all customers, and NULLs appear for OrderID and OrderDate when there is no matching order. Understanding these join types is essential for querying relational data effectively.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500