A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
https://www.tutorialspoint.com/sql/sql-sub-queries.htm
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
MariaDB [classicmodels]> use vlrinst;
Database changed
MariaDB [vlrinst]> show tables;
+-------------------+
| Tables_in_vlrinst |
+-------------------+
| emp |
| emp1 |
+-------------------+
2 rows in set (0.001 sec)
MariaDB [vlrinst]> selct * from emp;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual t
MariaDB [vlrinst]> select * from emp;
+----+---------+-----------+------+-------+
| id | name | loc | age | sal |
+----+---------+-----------+------+-------+
| 10 | venkat | kphb | 35 | 30000 |
| 11 | praveen | pune | 28 | 35000 |
| 12 | mounika | hyderabad | 22 | 39000 |
| 13 | revathi | jntu | 26 | 20000 |
| 14 | anji | gutta | 28 | 20000 |
| 15 | harika | lb nagar | 26 | 30100 |
| 16 | praveen | jntu | 33 | 100 |
| 17 | pandu | munipeda | 35 | 30300 |
| 18 | pandu | pune | 23 | 39300 |
| 27 | pandu | pune | 18 | 50000 |
+----+---------+-----------+------+-------+
10 rows in set (0.059 sec)
MariaDB [vlrinst]> select name from emp where sal > (
-> select sal from emp where name="venkat");
+---------+
| name |
+---------+
| praveen |
| mounika |
| harika |
| pandu |
| pandu |
| pandu |
+---------+
6 rows in set (0.085 sec)
MariaDB [vlrinst]>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.