Nested Queries | SQL | Tutorial 18
Nested Queries in SQL
In this tutorial, Mike explains how to use nested queries in SQL to get specific information. He demonstrates how to write a query that involves using multiple select statements and shows how to get the names of employees who have sold over $30,000 worth of products to a single client.
Getting Employee IDs
- To get all the employee IDs that have sold more than $30,000 worth of products to a single client:
- Write a query:
SELECT works_with.employee_id FROM works_with WHERE total_sales > 30000;
- The result is employee IDs 102 and 105 (105 appears three times).
Getting Employee Names
- To get the first name and last name of employees who have sold over $30,000 worth of products to a single client:
- Write a query:
SELECT employee.first_name, employee.last_name FROM employee WHERE employee.emp_id IN (SELECT works_with.employee_id FROM works_with WHERE total_sales > 30000);
- This query uses a nested query where the inner query gets all the relevant employee IDs and the outer query gets their first names and last names.
Overall, this tutorial provides an introduction to nested queries in SQL and demonstrates how they can be used to extract specific information from databases.
Nested Queries
In this section, the speaker explains how to use nested queries in SQL. The speaker provides an example of using a nested query to find employees who have made over 30,000 and then goes on to explain how to use nested queries to find all clients handled by the branch that Michael Scott manages.
Using Nested Queries
- A nested query is used when we need data from one table to inform data from another table.
- We can use a nested query to find employees who have made over 30,000 by checking if their employee ID is in the result of another query.
Finding Clients Managed by Michael Scott
- To find all clients managed by Michael Scott's branch, we first need to figure out the branch ID of the branch he manages.
- We can do this by selecting the manager ID from the branches table where the manager ID is equal to Michael Scott's ID (which we assume we know).
- Once we have the branch ID, we can select all clients whose branch ID matches that of Michael Scott's branch.
- It's important to limit our results to one value when using equality statements like this. We can do this with "limit 1".
Writing Complex Queries
In this section, the speaker discusses how to write complex queries and provides tips on how to improve your skills in writing nested queries.
Tips for Writing Complex Queries
- Break down the query into individual parts.
- Practice writing nested queries and using different combinations of SQL commands.
- The more you practice, the better you will become at writing complex queries.
Conclusion and Call to Action
In this section, the speaker concludes the video and encourages viewers to leave feedback and support Draft Academy.
Conclusion
- If you enjoyed the video, please leave a like and subscribe to Draft Academy's channel.
- Constructive criticism is welcome in the comments section.
Call to Action
- To help support Draft Academy's growth, visit draftacademy.com/contribute.
Turn any video into a summary like this
YouTube links, meetings, lectures. With transcripts, search, and chat.