Page 52 - Modular_V2.0_SQL_Flipbook
P. 52
Output:
+--------+----------+---------------+------------+-----+
| Stu_id | Stu_name | Subject_group | DOB | Age |
+--------+----------+---------------+------------+-----+
| 104 | Rani | Science | 2002-04-01 | 20 |
+--------+----------+---------------+------------+-----+
In this case, the outer query's result is totally dependent on the result of the inner subquery.
Rules to be followed for subqueries are as follows:
A subquery must be enclosed within parentheses ().
A subquery is always executed before the main (outer) query.
A subquery can return a single value or multiple values.
Subqueries can be used with SELECT, FROM, or WHERE clauses.
Use appropriate comparison operators such as =, IN, ANY, ALL, or EXISTS with subqueries.
The data type of the column used in the subquery must match the data type in the outer query
for comparison.
Subqueries cannot manipulate their results internally, therefore ORDER BY clause cannot be
added inside a subquery. An ORDER BY clause can be used in the main SELECT statement (outer
query), where it will appear as the last clause.
Subquery with Operators
You can use different types of operators with subqueries. Let us understand some of them. For
example, consider the following Table - PRODUCT
Table-PRODUCT
P_id P_name Quantity
1001 Pen 100
1002 Pencil 200
1003 Drawing Sheet 130
1004 Stencil 145
1005 Eraser 90
SELECT P_name FROM PRODUCT WHERE Quantity > 100;
Only those rows where the product quantity is greater than 100 will be selected. The P_name values
of these records will be considered and displayed.
Output:
+---------------+
| P_name |
+---------------+
| Pencil |
+---------------+
| Drawing Sheet |
+---------------+
| Stencil |
+---------------+
50
Touchpad MODULAR (Ver. 2.0)

