Home > OS >  How to convert SQL subquery into Join Clause?
How to convert SQL subquery into Join Clause?

Time:04-20

Good day everyone,

I have been attempting to convert the following SQL statement into a join clause, however I have been unable to figure out how to do it and was wondering if I could get some help.

Here is the statement:

SELECT OrderID FROM Orders
WHERE OrderDate = (SELECT orderDate FROM Orders WHERE OrderID = 10280);

I am essentially trying to get all orderIDs that were placed on the same date as the Order with OrderID 10280.

Thanks,

CodePudding user response:

Try this:

SELECT o.OrderId 
FROM Orders o
JOIN Orders ord 
ON o.OrderDate = ord.OrderDate 
WHERE ord.OrderID = 10280;

You can have a better understanding from here: https://www.sqlservertutorial.net/sql-server-basics/sql-server-self-join/#:~:text=A self join allows you,join or left join clause.

  • Related