Subquery Exercises - Part One - Transcript
Subquery Exercises - Part One - Transcript
In this
task, we'll continue what we have started doing with subqueries
in this project so far.
What we'll do now is that, um- I will ask you to think
through the solutions to different problems.
However, I will make sure to guide you through by giving
you hints on what to do.
Let us keep and join this exciting
ride and let us get started.
So, I want you to return a list of all employees who are
in the customers service department,
okay? A list of all employees who are in the customer service
department.
Let us-- let me give you a hint before you pause my video.
Um-- okay, so I would like to run this dept_emp table.
I'm not sure we've seen this table before but it's a table
that contains both the employees details and the department
details.
So, you'll see the employee number, the department, when
they joined the department and when they left the department
or if they are currently at the department
like this particular guy here.
So, if you go down a bit, you'll see this department- this
employee number 10 that first came to the company and joined
the department 004 between the date range and then moved to
department six.
So, you see it combines both the employees data and the
departments data.
Then, we have the departments table that only takes the
department number and the department name.
So, what I want you to do with this task is to return a list
of employees who are in the customer service department.
So, you have something-- let me guide you through.
You'll have something like select all records from the
department employment table.
Then, you'll have where the department number then you'll have IN.
Then, you'll have your subquery.
So, I want you to pause my video and try that out.
Now, I want you to really try that out.
Do not skip this part.
Okay, welcome.
So, I believe this is what your answer looks like.
This is the records from the department employment table
because we want all employees in a particular department,
okay? Then, we have the department where the department number--
So, we want to see those department number that are in--
for customer service.
So, let's
highlight this.
So, you will see the results.
It's d009.
So, department-- department, the department number for those
department that are, um-- those people that are in the depart- customer
service department is d009. So what it's, um-- SQL is doing is,
it's checking where we have the department number as d009 and
it will go through the department employment table and
checking through and returning only those departments--
Only those records for employees that are in this d009.
So, if we highlight all of this now and run it, okay? You will
see that we have all of these--
Now the result set is smaller and it's about 937 rows
now as against the high number of-- If you go through this and
highlight this,
okay? Let's go through the messages.
We'll see that about 13,042 rows, right?
Are in this employment-- department employment table, but when
we ran only this part,
okay? We had just, um- about how many records?
Let's check.
We had only about 937 rows.
That is those that are in the customer service department
alone. So, the next thing I want to do is-- let me just copy
this, okay?
Because what I want to do next is I want to include the- the
name of the employee and the employee number.
It makes sense that,
okay? We have a record, right?
That has the employee number.
The department of that employee, that is the customer service
department, when the person joined that particular department.
And it will make sense to have the first name and last name
of that particular employee.
So, let me just paste this and you know that the first name
and last name is in the employees table.
So, what I would do here is-- I want you to follow what I want to
do right about now.
So, I'm going to say SELECT, right? The employee number, then the--
let's say the department number, right?
Just follow through.
Then, I have the first name and the last name.
Good! Then, from the employees table
okay? What I would do is I would join this employees table to
this other table and this will become a new source of data--
this part here.
So, what I want to do here is to simply do JOIN, okay? I want
to join this part.
So, I'm going to put a bracket here.
This becomes-- so this becomes one source of data.
The first table.
This becomes another source of data, which is this one that
we have the results here.
So, I'm going to put this in a bracket here,
alright?
And I'm going to do something-- the next we want to do is-- you
know when you join two things, you have to specify, um- ON-- like
on the common column that you are joining on.
So, I'm going say ON, right? ON employee number.
So, it's important to give this a name.
Let me give this a name as a. Let me give this my other table-
source of data as b.
So, I'm going to say I want to join on-- the common column
there is the employee number,
alright?
I hope this is not too complex. Then,
b.emp_no. Then like we said, it's also always
good to order by in the outside query- ORDER BY by the employee
number,
okay? Then, one more thing is if you do this, it is clearly drop an
error because it doesn't understand what you're talking about.
So, I'm going to say let's this a be the source of the first
data. And department number here is in the-- is in the 2nd
table. So, I'm going to say b dot
that, the first name is in the employees table- a dot that, then I
have the a dot the last name.
So, what we have done here is this-- is now what we have from
the last exercise I gave you now becomes a source of data,
okay? called b.
And from that b, we want to select only the department
number. Then, because we want to have the first name and the
last name.
We have to just try to join this particular table here or
source of data to the employees table on the employees number
which is the common column.
So, let us run this and we'll see how the result will look
like. So, we know-- we have the employee number.
Let's see the messages still-- we still have 937 rows.
That means we are correct.
Now, we now have together with that the first name, the last name
of that employee.
That makes complete sense.
Beautiful,
right? Now it's your turn to practice again.
I want you to retrieve a list of all managers now-- who became
managers after the 1st January 1985
and in the Finance or HR departments.
So, I've written this to guide you.
This returns those department managers that became managers
after 1st of January 1985.
We can check that
here, alright?
You will see that all of these people became managers after the
1st- the first of January 1985.
Okay. So, I want you to pause my video right now and try to
retrieve the-- all managers who became this, right?
And then including those that, um- are in the Finance and the HR
departments.
So, you might decide to run this part here to know which, um-
departments we have here.
So, we have the Finance- we have the Finance here and we have
the Human Resources which is the HR- HR department.
So, pause my video and try that out now.
So, I believe this is the result you got.
So, we have this part already for those that are after the 1st
of January 1985.
Those that were employed as managers, then we want to have those
that are in the Finance or in the Human Resources department.
So, we see what I've done in this subquery.
If I run this part of the subquery alone,
okay? The result I will have is those depart-- the department
number of those that are in Finance and the Human Resources
department.
So, what this does is that it goes through the department
managers table and tries to see where we have only d002
and d003.
So, if we run this now.
Remember that there were 24 managers.
But let us see how many are-- where employed after 1985--
that were-- became managers after 1985.
And also in the department, d002 which is Finance or d-
-003 which is Human Resources.
So, you can pause my video to actually go through that again.
Let us continue. The next thing I want you to think about
right now.
This-- all this is to help you to really um- reinforce your
knowledge-- is to retrieve a list of all employees that earn
above 120,000 and in the Finance and HR Department.
What-- the only thing that would change is just this part of
the, um- this part here.
It's the only thing that would change,
okay? This is only part that would change. The remaining part will
remain the same.
So, I want you to pause my video and try that out now.
So, I believe that was not too tasking for you to understand.
I said retrieve a list of all employees,
okay? I didn't necessarily say all managers. So that was kind
of dicey for you.
So, what you need to do is you check the depart-- salaries
table, you have the employee number.
Then when you check the department employment table, we also
have the employee number.
Remember the department employment table covers both the
employees table and the departments table.
And I'm asking you to retrieve a list of all employees that earn
above 120,000 and in the H.R
or Finance department.
And we know that the Finance is d-
-002, and the H.R is, um- d003,
okay? All we need to just do now is just to run this and see
those managers-- or those employees rather not managers in
this case.
And we'll see those employees, all of these employees earn above--
So, this particular employee 19427--with employment
employment number 19427 has earned over 120,000
about four times,
okay? That and that's what the solution looks like.
Beautiful! One other
thing I've done is to put an alternative result that instead of
having in this WHERE clause here department number as d003
or department number has-- you can just have department number IN--
So, what IN will do is-- IN operator will check if this is true
and if this is true and return.
So, if you run this, okay? We would have the same results as what
I have before.
And you'll see these employment--employ-- employees that earned
above 120,000.
So, one thing we can decide to do finally that I want you to
pause my video to do now is to retrieve also the average
salary instead of retrieving the salary.
I want the average salary.
But yet I want to salaries that above 120,000.
But this time I want the average salary.
All I need to do is just to, um- copy this, right? and come down here
to paste it,
alright? Now,
I want you to pause my video and try to figure out how you can also
select the average salary.
Good! Instead of having the salary, I would just come over
and say, let me add a ROUND() because it will produce a messy
result.
Let me say average salary,
okay? To two decimal places,
alright? I can give it a name, as average salary. Then,
because I'm using a group-- um, uh-- an aggregate function here,
I have to GROUP BY.
So, I'm going to say, come over here.
Let me remove my statement terminator. Come down here and write
my GROUP BY statement.
So, I'm going to GROUP BY the employee number,
okay? and down here I'm going to ORDER BY. Let me just ORDER
BY the average salary in descending order.
So, what I have just done here is to also return the average
salary instead of the salaries.
And if you run this, okay? You will see now that for each of those
employees that were returned before, we now have the average
salary of those employees that are in the Finance or HR
department,
right? That's first condition. Then, they also earn above 120,000
in salary.
So, we're trying to find their average salary of that particular
employee.
I believe that was quite a long one but it was actually very
beautiful. Wow! Amazing work there.
I believe you are learning some interesting concepts here. At
this point in this project,
you can start writing subqueries as you wish to query and
manipulate databases.
However, we'll see some more exercises to do.
I want you to make sure that after you leave this course, you
can pick up a problem, think through the solution to the
problem in SQL and recommend this project to others to
reinforce their SQL arsenal.
Great job on completing the fifth part of this project.
At this point, you have learned a lot. In this task, we learned
about how subqueries work in SQL. To demonstrate our
understanding, we tried to solve some real world problems
together with my help.
I hope you enjoyed it and I hope you are ready to attempt
more questions.
I must say you have been superb but it is not enough to start
well, it is important to finish
well. You cannot afford to stop working
now. We are moving close to the end of this interesting
project.
Let us keep exploring. In the next task, we will continue to write
SQL subqueries to solve some exercises.
Let us get going.
Please join me in task six.