Table of content

Subqueries in SQL

The queries used inside the other query is known as the Subqueries. The subquery is also called as Inner query and Nested query. The subquery is used when we know how to search using the statements but don't know the exact value in the whole database.

The query can reside in the WHERE, SELECT, FROM. The subqueries are mostly inserted in the WHERE statement.

Example for subquery :

SELECT first_name,last_name,salary,age,gender
FROM persons_table
WHERE salary IN (SELECT salary
                 FROM persons_table
                 WHERE last_name='Patil');

data-of-table
The above table is the data retrieved by the persons_table.
sub-query-sql
In the above query, the first_name, last_name, salary, age, and gender are selected from the persons_table. But we don't know the salary of that particular person. So, we use a subquery to give furthermore details based on the last_name of the person.

The most commonly used functions in the subqueries are as follows.

  • UPDATE
  • DELETE
  • INSERT
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions