LESSON 31 / 62 · Subqueries with multiple rows and one columnSubqueries with multiple rows and one column
Subqueries using ANY, IN, ALL operators
If a subquery returns more than one row, it cannot be used with comparison operators in the same way as scalar subqueries could be used as described here.
However, with subqueries that return multiple rows and one column, there are three additional operators that can be used.
Subquery and ALL operator
With the ALL operator, we can compare an individual value to each value in the set obtained by the subquery. In this case, this condition will return true only if all comparisons of an individual value with values in the set return true.
For example, the synthetic query below checks whether the condition that a residential unit is cheaper than 200 is true for all residential units.
SELECT 200 > ALL(SELECT price FROM Rooms)Or a more practical example: we need to find the names of all property owners who have never rented a place themselves. To obtain this list, we can proceed as follows:
idINTidINTuser_idINTroom_idINTidINTowner_idINTidINTreservation_idINT
SELECT DISTINCT name FROM Users INNER JOIN Rooms
ON Users.id = Rooms.owner_idGet a list of identifiers of all users who have rented a place
SELECT DISTINCT user_id FROM ReservationsFilter the first list of all property owners by the condition that the property owner identifier is not equal to any of the identifiers of users who have ever rented a place
SELECT DISTINCT name FROM Users INNER JOIN Rooms
ON Users.id = Rooms.owner_id
WHERE Users.id <> ALL (
SELECT DISTINCT user_id FROM Reservations
)Subquery and IN operator
The IN operator checks whether a specific value is included in a set of values. Such a set can be a subquery that returns multiple rows with one column.
For example, if we need to obtain all information about property owners whose property costs more than 150 units, we can do it as follows:
SELECT * FROM Users WHERE id IN (
SELECT DISTINCT owner_id FROM Rooms WHERE price >= 150
)Subquery and ANY operator
The conditional expression with ANY has similar behavior, but it returns true if at least one comparison of an individual value with a value in the set returns true.
Let's use it to write the same query that we did with the IN operator: find users who own at least one residential unit that costs more than 150.
SELECT * FROM Users WHERE id = ANY (
SELECT DISTINCT owner_id FROM Rooms WHERE price >= 150
)Python Academy
Practice
Solve it in the editor. The query runs against a sample database in your browser.
Write a query and run it to see the results.
Write a query and run it to see the results.
Write a query and run it to see the results.