What is the use of comparison operators with MySQL subquery?


The subquery can return at most one value. The value can be the result of an arithmetic expression or a column function. MySQL then compares the value that results from the subquery with the value on the other side of the comparison operator. MySQL subquery can be used before or after any of the comparison operators like =, >, >=, <, <=, !=, <>, <=>. Following is the example in which we are using a subquery with a < comparison operator.

Example

mysql> SELECT * from Cars WHERE Price < (SELECT AVG(Price) FROM Cars);
 +------+--------------+---------+
| ID   | Name         | Price   |
+------+--------------+---------+
| 1    | Nexa         | 750000  |
| 2    | Maruti Swift | 450000  |
| 5    | Alto         | 250000  |
| 6    | Skoda        | 1250000 |
| 8    | Ford         | 1100000 |
+------+--------------+---------+
5 rows in set (0.00 sec)

Similarly, the subquery can be used with other comparison operators too.

Updated on: 22-Jun-2020

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements