(SQL examples for Beginners)
In this end-to-end example, you will learn – SQL Tutorials for Business Analyst: How to use Sub-Queries in MySQL.
MySQL SubQuery Tutorial with Examples
What are sub queries?
A sub query is a select query that is contained inside another query. The inner select query is usually used to determine the results of the outer select query.
Let’s look into the sub query syntax –
A common customer complaint at the MyFlix Video Library is the low number of movie titles. The management wants to buy movies for a category which has least number of titles.
You can use a query like
SELECT category_name FROM categories WHERE category_id =( SELECT MIN(category_id) from movies);
It gives a result
Let’s see how this query works
The above is a form of Row Sub-Query. In such sub-queries the , inner query can give only ONE result. The permissible operators when work with row subqueries are [=, >, =, <=, ,!=, ]
Let’s look at another example ,
Suppose you want Names and Phone numbers of members of people who have rented a movie and are yet to return them. Once you get Names and Phone Number you call them up to give a reminder. You can use a query like
SELECT full_names,contact_number FROM members WHERE membership_number IN (SELECT membership_number FROM movierentals WHERE return_date IS NULL );
Let’s see how this query works
In this case, the inner query returns more than one results. The above is type of Table sub-query.
Till now we have seen two queries , lets now see an example of triple query!!!
Suppose the management wants to reward the highest paying member.
We can run a query like
Select full_names From members WHERE membership_number = (SELECT membership_number FROM payments WHERE amount_paid = (SELECT MAX(amount_paid) FROM payments));
The above query gives the following result –
Sub-Queries Vs Joins!
When compare with Joins , sub-queries are simple to use and easy to read. They are not as complicated as Joins
Hence there are frequently used by SQL beginners.
But sub-queries have performance issues. Using a join instead of a sub-query can at times give you upto 500 times performance boost.
Given a choice, it is recommended to use a JOIN over a sub query.
Sub-Queries should only be used as a fallback solution when you cannot use a JOIN operation to achieve the above
Summary
- Subqueries are embedded queries inside another query. The embedded query is known as the inner query and the container query is known as the outer query.
- Sub queries are easy to use, offer great flexibility and can be easily broken down into single logical components making up the query which is very useful when testing and debugging the queries.
- MySQL supports three types of subqueries, scalar, row and table subqueries.
- Scalar sub queries only return a single row and single column.
- Row sub queries only return a single row but can have more than one column.
- Table subqueries can return multiple rows as well as columns.
- Subqueries can also be used in INSERT, UPDATE and DELETE queries.
- For performance issues, when it comes to getting data from multiple tables, it is strongly recommended to use JOINs instead of subqueries. Sub queries should only be used with good reason.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.
PostgreSQL tutorial for Beginners – PostgreSQL – Sub Queries
MySQL Tutorials for Business Analyst: MySQL WHERE Clause with Examples – AND, OR, IN, NOT IN