From the course: CompTIA Data+ (DA0-002) Cert Prep

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Nested queries

Nested queries

- [Instructor] A nested query, also known as a subquery embeds one SQL query inside of another. The inside query gets executed first, and then the results of that query are used in the outer query. For example, imagine that you wanted to know which of your employees had more than average vacation hours saved up. Well, to answer that question, first we need to know what the average number of vacation hours is among our employees, and hopefully you already have a good idea of how to do that. We can select the average vacation hours from human resources.employee. When we run that, we find that the average number is 50, so the average employee has 50 vacation hours. But what if we wanted a list of those employees? I could write another query. Let's write select business entity ID and vacation hours from Humanresources.employee, where vacation hours is greater than 50. And when I run that query, it gives me the list of 147 employees, and that is about half of the 290 employees that we…

Contents