RianAcademy
Search
⌘
K
Premium

Subquery with one row and one column

In this lesson, let's take a closer look at a subquery that returns one row and one column. This type of subquery is also known as a scalar subquery.

It can be used in various parts of the main SQL query, but most often it is used in selection restriction conditions using comparison operators (=, <>, >, <).

Examples

The following simplest query demonstrates the output of a single value (company name). In this form, it doesn't make much sense, but your queries can be much more complex.

MySQL 8.4
SELECT (SELECT name FROM company LIMIT 1) AS company_name;
company_name
Don_avia

Similarly, scalar subqueries can be used to filter rows using WHERE, using comparison operators.

MySQL 8.4
SELECT * FROM FamilyMembers
    WHERE birthday = (SELECT MAX(birthday) FROM FamilyMembers);
member_idstatusmember_namebirthday
8daughterWednesday Addams2005-01-13T00:00:00.000Z

With this query, it is possible to obtain the youngest family member. The subquery in this case is necessary to obtain the maximum date of birth, which is then used in the main query to filter rows.

The importance of matching the operator used and the subquery result

When using the result of a subquery with comparison operators, as in our example, it is important that the subquery returns exactly a scalar value (1 row and 1 column).

If this subquery returned multiple values, the DBMS would return an error, indicating that it expected the subquery to return only 1 record: «ER_SUBQUERY_NO_1_ROW: Subquery returns more than 1 row».

Therefore, it is important to be careful when writing subqueries and to understand what result the subquery will return, and what operators we can use together with the resulting set.

Tasks for self-testing
0 / 2
1. Looking for the owner

Output all information about the user from the Users table, who owns the most expensive property.
Users
id
INT
name
VARCHAR
email
VARCHAR
email_verified_at
DATETIME
password
VARCHAR
phone_number
VARCHAR
Reservations
id
INT
user_id
INT
room_id
INT
start_date
DATETIME
end_date
DATETIME
price
INT
total
INT
Rooms
id
INT
home_type
VARCHAR
address
VARCHAR
has_tv
BOOLEAN
has_internet
BOOLEAN
has_kitchen
BOOLEAN
has_air_con
BOOLEAN
price
INT
owner_id
INT
latitude
FLOAT
longitude
FLOAT
Reviews
id
INT
reservation_id
INT
rating
INT
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Result
Users
Reservations
Rooms
Reviews

Run a query to see the result, or open a table above