When it comes to SQL Server interviews, there are often questions that test your knowledge of specific syntax or concepts. One such question is whether it is possible to join two tables without using the JOIN keyword. While this may seem like an unusual question, it is worth exploring the answer.
Yes, it is indeed possible to join two tables without using the JOIN keyword in SQL Server. The alternative method involves using the comma-separated table names in the FROM clause. Let’s take a look at an example:
SELECT *
FROM [Sales].[Invoices], [Purchasing].[PurchaseOrders]The above query will result in a cross join between the two tables mentioned. In other words, it will combine every row from the first table with every row from the second table, resulting in a Cartesian product. This type of join is known as a cross join.
Furthermore, you can also join multiple tables (more than two) by simply adding them to the FROM clause with commas between them. They will all be cross joined together.
If you want to add a condition to the join and convert the cross join to an inner join, you can use the WHERE clause. Here’s an example:
SELECT *
FROM TableA a, TableB b
WHERE a.condition1 = b.condition2By specifying the condition in the WHERE clause, you can filter the results and create an inner join based on the relationship between the two tables.
It’s important to note that while this alternative syntax works and can produce efficient results for inner joins and cross joins, it is not the preferred ANSI standard syntax. To ensure clarity and adhere to standards, it is recommended to use the explicit JOIN keywords.
In conclusion, joining two tables without using the JOIN keyword is possible in SQL Server. However, it is advisable to use the explicit JOIN syntax for better code readability and adherence to standards.
For more information on joins and related topics, you may find the following blog posts helpful:
- SQL SERVER – Introduction to JOINs – Basic of JOINs
- SQL – Difference Between INNER JOIN and JOIN
- SQL SERVER – What is the Difference Between An INNER JOIN and WHERE Clause
- What is a Self Join? Explain with Example – Interview Question of the Week #064