Multiple-row subqueries in SQL return more than one row as the result and are typically used with operators like IN, ANY, ALL, or EXISTS.
Consider a table named Orders
with columns OrderID
, CustomerID
, and OrderDate
. We want to
retrieve all orders made by customers from a specific city.
SELECT OrderID, OrderDate FROM Orders WHERE
CustomerID IN (SELECT
CustomerID FROM Customers WHERE City = 'New York');
In this example, the subquery (SELECT CustomerID FROM Customers WHERE City = 'New York')
returns all CustomerIDs of customers from New York. The outer query then
retrieves the OrderIDs and OrderDates of orders where the CustomerID matches
any of those returned by the subquery. This query will return all orders made
by customers from New York.
Comments
Post a Comment