MySQL Sub Queries

I this post I would like to write about sub queries. A Sub-query is also a query, which is defined under a main query and embedded within the WHERE clause . It always sends a value to its nearest main query. Sub-query is also know as Inner query or Nested query.

A Sub-query is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved. Sub-queries must be enclosed within parentheses.

If a Sub-query send single value to its nearest main query then that sub query is called single valued sub query.

If a Sub-query sends multiple values to its nearest main query then that sub query is called multi valued sub query. in this case instead of embedded within the WHERE clause you must use WHERE IN clause.

Sub-queries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.

Syntax of sub query

Example table structure

TABLE NAME – tab_employee.

To display employee details, who are working under sathish deportment we will right something like below shown

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.