The document provides step-by-step instructions for using subqueries to find the difference between the average salary of all employees and the average salary of individual employees. It demonstrates retrieving employee data, calculating the average salary for each employee using a subquery, calculating the overall average salary using another subquery, and joining the results to return the employee information alongside both average salary calculations and their difference.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
42 views
Subquery Exercises - Part Two - Transcript
The document provides step-by-step instructions for using subqueries to find the difference between the average salary of all employees and the average salary of individual employees. It demonstrates retrieving employee data, calculating the average salary for each employee using a subquery, calculating the overall average salary using another subquery, and joining the results to return the employee information alongside both average salary calculations and their difference.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5
INTESAR PATEL
you will use subqueries to solve a very complex exercise
in order to give you that confidence, you need after here. Let us get our hands dirty. Are you ready? Now, what we want to achieve with this particular task is; we want to find the difference between the average salary of all employees and the average salary of individual employees. So, what I've done for you here before I ask you to do that is I want to help you think through the process. So, we want to retrieve a list of all employee number, first name, last name. Also returned the average salary of all the employees and the average salary of each employee. So, what I've done in this- in this place here is to help you see-- this retrieves all the records from the salaries table. So, let me highlight this and run it. Just follow through what I'm doing and you'll notice that an employee at different times has earned different salaries, okay? So, we want to find the average salary of this particular employee number one. Then if you go down, you will see employee number 2 too has earned different salaries. You want to find that average salary at-- keep it in a place. Then, we want to find the average salary of all-- this particular salaries column. We want to find the average of this particular salaries column. That is one thing we want to do. Okay, so what I've done here is the first thing I've done here is to retrieve the employee number, the first name and the last name of the employee. Then, I've written a subquery here. And what this subquery does-- Remember we've-- I think we've done something like this earlier where we returned this. Yeah, I think so. So, if you run this here, okay? I've seen this before. It returned the average salary of all the employees. That is 63,986.10, okay? So, what we want to do is we want to now retrieve the first name and last name which you know that is in the employees table and from your knowledge of joins, you know that I'm just joining us on the salaries table, on the common or the- um- the related column, which is the employee number. So, if I run this-- what it will return is what we've seen earlier where we wrote that join statement and we have the employee number, the first name, the last name and the average salary of all the employees. So, what it does is that it goes through this salary column in this salaries table and find the average and returns it for each row. That is beautiful and quite explanatory. The next thing we now- I have here for you is to return the average of each employ-- employee and I call that emp_avg -salary. So, what I've just done is now I've picked the the salary and I found the average. Then, I have grouped by the employee number to find the average salary of each of the employees. So, let us highlight this and run it. Great. Now, you will see that for employee number one, that had these different salaries he has earned. We now have just the average for that employee number one. We have that for employee number two and all-- for every employee in our table. So, what I want you to do now is to find a way to merge these tables-- this table source, okay? And this table source and return the employee number, the first name, the last name and return a column that returns the average of all the employees and a column that returns the average for each of the employees. So, pause my video right now and try that out. Alright, I hope that was okay. So, let me just-- let's go through it together. So, I'm going to highlight this part here, okay? Let me right-click and copy it. Then, let me come down to the solution box here. Yes, and let's just paste this here. Right-click and paste. Then, let me also copy this one, okay? Right-click and copy it. So, what I want to do is that-- go back to that point. So, from this point here instead of having this-- let me just come down a bit-- instead of having this salaries here, I'll take out this part here and I would have a subquery here. And in that subquery I'm going to paste that second part of the code that I copied, alright? here. So, let me just rearrange this. This is the simple solution. I just rearranged it. So, remember we already know what this will return. This will return the average salary of each of the employees. Just like we have on this result set here and this part of the query will return the first name, the employ- employee number and the last name plus this average salary of all the employees. That's what this subquery here is doing. This shows the average salary of all the employees. So, remember we have to give this a name. Let me just give this a name as, say a. Just the name of the table. We don't have s dot employee number. Now we have a dot employee number, okay? What else will change? Let us see. Now, we want to also return the- the average salary of each of the employees. So, I would just come over here and-- and you know that this part here-- this particular emp_avg_salary is the average salary of each of the employee like we have here. So, I'm just going to say a dot, okay? a dot- I'm properly referencing that table. a dot emp_avg -salary. Okay, so I think we're in good shape and I will add a comma, alright? So, we have how many columns? We have the employee number. We have the first name, okay? We have the last name. We have the- this average salary of each employee. And this last column in this subquery is the average salary of all the employees and we've probably joined-- we've properly joined that. Okay, so let me just highlight this and round it. And this gives us the employee number, right? The first name of that employee. The last name of that employee. The average salary of this employee one, remember it's the same thing. Then the last column is the average salary of all the employees. So, what I want to do next would be to find the difference-- another column here. We'll just have another column that finds the difference between this average salary of each employee and the average salary of all the employees. Let us know those employees that are not really earning high and those ones that are running very high. So, all we need to do is just to copy this. Okay, so I want you to pause my video and try this yourself. Alright, pause my video now and try to find that last column, that finds the difference between this and this. Okay, I believe you were able to figure that out. But let me show you. So, I'm going to come to this point here and paste that other one I have here. So- Good! Then, I'll come over to this place and have an additional column here and that column would take a.emp_avg -salary. That is the- the average salary for each employee then minus this particular one here. Let me just copy this- minus this. Let me copy that, okay? So, minus that. Let me just paste it here. So, what it's doing is taking each one here minus each one here. So, this minus this. So, I can just give that as say salary- average salary- -difference. Let us say avg_salary_diff, okay? So, let's run this and see. That's quite complex, but it's quite explanatory from the way I've, um- explained it. So, let's um- run this now. Very beautiful. So, you'll see the last column average salary difference takes 75- 388.94 minus 63,986-- so we'll see that oh, this person employee earns like about $11,402 more than the average salary. You know it's-- and this person- this one is not even earning as high as the average salary like-- it's-- the place-- employee three average salary is not even as high as the average salaries of all the employees. So, we can decide to say, okay, what employees we want to promote. What employment-- employee, we want to add more to their salary, alright? And that really gives a real-life scenario to us. This last one I have here. I have this code which is the same thing with what I have earlier. Only the different thing is-- I want you to just pause my video for a little while and try to see what I've done here. And let's see if you understand this. Pause my video now and try to look through each of these line of queries to see if you can dissect and understand what is happening, alright? Okay, so were you able to figure out that we are trying to select the employee number, the first name, the last name. The maximum employee number of each employee. That is what this is doing here. So, the only thing is instead of having average like the former exercise 6.2, we just looked at, now we are having the maximum salary. So, I want to see-- we want to see what is the difference between the maximum salary that an employee has earned and the maximum salary of all the employees. So, this part of the query here will return the- each-- the maximum salary of each of the employee. If you run that right now. You'll see that this is the maximum salary of each of the employee. And that's what we have here. And if you run this other part here without this, you'll see that what this one here is doing-SELECT MAX-- this part here. What this part here is doing is returning the maximum salary. It will return only one value. And that value will be the maximum salary of all the employees. That's 145,732. So, what we are doing is basically to say find the difference between this maximum salary of all the employees and the maximum salary of each employee. And call that salary difference. So, if we run this. We will have the same thing we have. But this time around instead of having the average, we'll have the maximum, okay? And you'll see that we have the employee number, the first name, the last name, the employee's maximum salary that is-- so for this employee five, the employee's maximum salary is 94,692. But the maximum salary of all the employees in the table. That is there's a particular employee that earns highest, and that's 145,732. Possibly a manager or even maybe the CEO, okay? Let's see what difference-- you can see that this person um- the difference is about 51,000 between this employee and the highest employee, or the maximum employee. Wow! You have come a long way. I must commend the efforts you have put to work. It will pay off with more practice and consistency. Great job on completing the sixth part of this project. In the task, we learned how to solve some exercises using subqueries in the FROM and the SELECT clause. So, we tried to look at the difference between the average salary of an employee and the average salary of all the employees. And the next one we looked at it was for those who-- remember that there is a WHERE clause, that's why the result is smaller. And this WHERE clause is basically looking at those employee in the Finance- d002 and d003. And that's why the result is smaller. If I don't have this WHERE clause. If I comment this out, okay? amd run this part alone. You'll see now that it returns 100-- all the employees. But because I want us to just see-- let's know what's the difference between the maximum salary of employees that are in the Finance and the um- Human- Human Resource Department. And let's see what the difference between that-- their maximum salary and the maximum salary of all employees. Okay, let us conclude this interesting project by wrapping up our work with more exercises. Now, you've got to finish what you started. In the next task. we'll perfect our knowledge once and for all by working on the last set of exercises. It's going to be amazing. The next task promises to be interesting. Please join me in task seven.