What are subqueries in SQL and how to use them in our query?
What are subqueries in SQL and how to use it in our query?
In this post, we will discuss in detail
So what is a subquery?
A query inside another or nested query is known as a subquery.
see the image above the query marked in green is a subquery and the query marked in red is the main query.
How does subquery work?
See the image below the subquery always runs first which means that
whatever the query is written in the inner query it always runs first and after it is done then our main query runs.
Why use subquery?
a subquery is helpful in some cases for example in the below image our inner query runs first so it will give us the average marks of students which is 50.16 and after that our query becomes
SELECT * FROM class_10 WHERE marks>50.16;
because the inner query work is done and it gives us 50.16.
If you want to run the same query without an inner query then you have to first find the average marks and then use that average marks in the next query.
but with an inner query, it can be done very easily.
so that’s how the inner query is helpful to us.
and in the output, it selects only those rows in which marks are greater than the average.
so that’s about the inner query all the content I post is created by me so feel free to share with your network or download it.
Also, feel free to share your thoughts on how much you like this content or if you have any questions till then happy coding
Want to receive more subscribe to my newsletter
You can also help me by contributing to me or buying me a coffee by clicking the below tip button.
Thank you for reading this post