'Join two simple SQL queries

I have two tables, ORDER and CART.

The following query returns all OrderId's associated with the customer email

SELECT [OrderId], [Email]
FROM [ORDER]
WHERE [Email] = '[email protected]'

Now I manually copy pasting OrderId to following quarries to return all CartId's associate with those OrderId's

SELECT TOP (1000) [CartId], [OrderId], [Name]
FROM [CART]
WHERE [OrderId] = 123
   OR [OrderId] = 456
   OR [OrderId] = 789

How can I join this two SQL queries to ease my work? I'm just a beginner.



Solution 1:[1]

Use join as follows

SELECT [cartid],
       C.[orderid],
       [name],
       [email]
FROM   [cart] C
       JOIN [order] O
         ON O.[orderid] = C.[orderid]
WHERE  o.[email] = '[email protected]'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1