0 votes
in Sql by
What is a Subquery? What are its types?

1 Answer

0 votes
by

A subquery is a query within another query, also known as nested query or inner query . It is used to restrict or enhance the data to be queried by the main query, thus restricting or enhancing the output of the main query respectively. For example, here we fetch the contact information for students who have enrolled for the maths subject:

SELECT name, email, mob, address

FROM myDb.contacts

WHERE roll_no IN (

SELECT roll_no

FROM myDb.students

WHERE subject = 'Maths');

There are two types of subquery - Correlated and Non-Correlated.

A correlated subquery cannot be considered as an independent query, but it can refer the column in a table listed in the FROM of the main query.

A non-correlated subquery can be considered as an independent query and the output of subquery is substituted in the main query.

Q   =>   Write a SQL query to update the field "status" in table "applications" from 0 to 1.

Q   =>   Write a SQL query to select the field "app_id" in table "applications" where "app_id" less than 1000.

Q   =>   Write a SQL query to fetch the field "app_name" from "apps" where "apps.id" is equal to the above collection of "app_id".

Related questions

0 votes
0 votes
asked Jun 12, 2023 in Sql by Robin
0 votes
asked Jul 12, 2020 in Sql by Robindeniel
0 votes
asked Dec 13, 2020 in Sql by SakshiSharma
...