Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

Welcome back.

In this task,
you will learn how to use subqueries in the FROM clause.
I believe you quite understand the rules and how we have used
subqueries in the WHERE clause. We would see a similar approach
here too. Let us start immediately.
So, I want to have something like, let me just have this query
right here like this.
Okay, so with this, I want to explain some things where
we have subquery in the FROM clause.
So, what I have here is, I have select all records
of all customers that live in the southern region.
So, if you do this, okay? and run this.
You'll see that if you come through this place to the region,
you'll see that all the regions returned here is the southern
region.
Although, I could have just had this not--
This is quite redundant for now.
Yeah, but I want to use this to explain a couple of things
Now, this is how you have a subquery, same way we have
our parentheses we have the software inside.
And so what happens with the FROM clause is that any subquery
you write- you put in the FROM clause becomes a source
of data.
Not really a table as the case might be.
Um- because usually, you know that the FROM clause takes
a table, but in this case we would have a source of data
and that's why it's important that I'll name-- I'll give this source
of data a name.
In this case, I just want to give it a name
a. I can give it any name-- I can give it b, but I just want
to give it a name- a,
alright?
So, what this is saying is that we want to have all the records
from the- of-- from the customers table where the region is
the southern region,
okay? So, with this instead of having, um- this-- if I
run this, this returns-- one minute-
This returns 134 rows,
okay? If I run all of this again. It will return the same value-
134 rows.
And that's why I said it's quite redundant.
But the reason why I have this is to show you how the-
-the subquery is written in a FROM clause.
So, one thing I can do here quickly is; say I want to return
the customer's name,
alright?
And the age of the customer,
okay? So, I can do something like a dot
okay? customer-
-name, then, a.age,
okay? And what this is trying to say-
tell us here, is that now out of this--
This is now a source of data.
If I run this again, now you will notice that the--
this particular part I ran has the customer name and the age.
So, what I'm trying to say is that- that this becomes a source
of data.
So, I'm trying to tell SQL that from this place,
I want a customer name and just the customer age,
okay? Let me run all of this.
And you will see that it returns only the customer name
and the age.
What happens is that SQL is seeing this part of the query-
this subquery as a source of data, not necessarily a table.
And because I'm giving it a name- a, it makes complete sense.
I can even decide to include probably, the region- a
dot region,
right? And if I run this again, it would go
through that source of data and return the customer name,
the age and the region. Peradventure
something happens.
And instead of having this my star (*) in between here,
I have something like this.
I have something like returns the customer name,
alright?
And let's say the customer age.
Let's give it an alias as, um- say customer-
-age. Let me break this down here,
okay? Now, what I'm trying to say is now that SQL, right?
I want you to return from this customers table; return
the customer name and the age.
But now give the name- the age as a name customer_age.
So, let us run this subquery part here.
Let me show you that.
Let's highlight that and run it.
You'll see that returns the customer name and the customer age,
okay? And you'll see now that the customer name remains.
Instead of having age like we had before, we now have
customer_age as a new name.
What do you think will happen if I rerun all of this now, it
would definitely throw up an error.
And the reason for that error--
You'll see it will throw up an error. The reason for that error is to tell
you that I don't know any- any column called a.age.
So, it's seeing this particular, um- subquery as a source
of data,
okay? Or as a-- not really a table- like a source of data.
And we are calling that a.
So-- and because we've renamed the age column to customer-
-age. It's not more seeing this.
To remedy this.
All we have to do is to just come over here and do customer
underscore age.
But if we run this, it will throw up another error, let us try
this and see what error it will bring up this time.
I'm trying to show you how these things work.
Now it's throwing up an error. It's saying that a dot region does not
exist. And why is that?
Because in this my subquery here, I have just selected only the
customer age-- customer name and customer age.
But I'm now saying in the outer query that
I want this outer query to reference this inner query,
okay? And return the region.
Whereas I have not returned the region here. To remedy
that all I need to do is just come over here and return the
region,
okay? At this point, it now knows that if I run this, I'm
just trying to tell SQL run this and return three columns for
me, okay?
Okay?
And out of those three columns
now, I want the outer query to reference this table or this
source of data now and return this particular column for me,
alright?
So, let me highlight this now, and run it.
Beautiful!
Now, we see the customer name, the customer age and the region
returned because in the inner query that we are referencing, we
have properly selected those columns.
This shows you how powerful subqueries can be.
Let me do one more thing.
Let me say comma here,
and do another subquery here.
Do SELECT a customer_id,
okay? comma category FROM the sales table.
Now, I will give this name as b.
So, what does this look to you that
oh, so there can be two sources of data in the FROM clause.
So, this is one source of data and this is another source of
data. Not necessarily a table.
Just a source of data that can be referenced in the outer
query. So, what this is saying--
Let me also come here and say for example including customer-
-id because customers table
also has that,
alright? Then, what I'm trying to say is from this second
table. I want to select the customer_id
and the category to show you how to reference table from the
outer query.
So, you'll see that works correctly.
If I run this other first source of data which is a now,
okay? You'll see now that that works correctly. Now what happens
if I come over to this place and say I also want the customer-
-id. Let's see what will happen.
I also want the customer_id. Let's run this and let
us see what will happen.
I hope you're following.
What happens is that this throw- throws up an error.
And that's because it says customer_id
is ambiguous.
That's because it says we are telling SQL that return
customer_id.
But we have this customer_id
in this first source of data called a.
We also have a customer_id.
We have customer_id column in this second source of data b,
right? So, it's too ambiguous.
So, I'm just trying to show you or emp-- emphasize with this
example is to that-- is to show you that whenever you're
referencing, um- a subquery, make sure you are referencing it
properly.
So, I can just say give me a dot
that, alright?
I'm sorry for that.
Yeah, give me a dot the customer_id,
and also give me b dot the category.
Now, this is properly referenced.
SQL now understands what you're saying,
okay? I want to add the customer_id from the first source
of data called a. I want to have the customer name from the
first source of data and that's because we properly retrieved
that in the first source of data.
And I want to have the category
for example, from this second source of data.
In fact, we could have three sources of data from the, um- so far they are
in a subquery and they are properly referenced.
So, let me highlight this. Now
you'll see how that plays out. And that works correctly runs-- It returns
the customer_id,
it now knows which table it's referencing, which source of
data it's referencing, the customer name, the customer age, the
region and the category of that particular customer.
I believe that makes complete sense.
Let us go on-- what I want to do is to retrieve a list of
managers with their employee names-
oh sorry-- with their department names.
Yes, so what we want to do is to retrieve a list of managers
and their department names.
Of course that looks as if it's quite easy to you.
But the truth is that the department manager is one table.
If you highlight that and run it.
You'll notice that this department manager does not have the
name of the departments, right?
The department's name is in the department table.
So, what we need to do is-- if you are doing it normally you would
have written a join statement to join the department manager
to the depart-- to the departments table but we can use
the SQL subquery to achieve this. So, I will say SELECT dm.* --
I'll explain what that means very soon.
Then, d dot department name because I said the department
table contains the department name. So, from department manager,
okay? comma.
Okay, let me give this a name as dm. That's like to know I'm
referencing that table-- comma.
Now, I want to have a subquery. SELECT the department number and the
department name from-- from the departments table.
So, remember we have to give this as a name d. If I pause a
little bit if I go on, and I highlight this up to this point and
run it, okay? What it will return is the-- those nine
departments. There are nine departments and you'll see that
department one all through department nine.
Um-- you will see department nine is customer service and all of
that. So, we are trying to say is that now we want d to be the
name of this source of data and we want out of that-- this
source of data.
We want to select the department name,
okay? And this other one which is the department managers
that we've seen before.
We want to select all of the record.
That is what this dot star means. If I do this, right? and run
it. This would not give me exactly the result we want.
This would give me about 216 records.
That's saying that because there are nine departments, remember?
And there are 24 depart-- department managers,
right? So, it does something like 24 times nine.
It's kind of do that combination. So, to able to solve that
problem, all we need to do is just to add a WHERE clause here. And say
WHERE the department manager
dot department number like WHERE the department number from
the department managers
table match with the-- that for the departments table,
okay? So, this is all we need to solve that.
So, if I'm going to run this now,
alright?
You will see that it will return just 24- 24 records, which is the 24
managers and you can now see the data output; the employee
number, the department number of that department-- of that- of
that manager
rather, when the person became a manager, when they stopped
becoming a manager and the department's name.
So, you will see department one is market-- marketing department,
two is Finance and we already have the answer as we should
have it.
Okay. So, what I wanted to do next for me-- as an exercise for
for me now is to- to retrieve a list of managers their first
name, the last name and their department names.
Now, the only thing that will come into this place is, this
same query we have here will not change. One thing that will change is
that, we want their first name and their last name. And remember if you
do SELECT * FROM employees, that will show you that we have the
first name and last name of the employees from the employees
table. So, all you just need to do is to add another table in
the FROM clause.
So, I want you to pause my video now and try that out.
Okay, so I believe this is what your answer looks like right.
All I needed to do is just to add a new table employees as e,
and now I included the employees, first name, and the
employees last name properly referencing that table employees
as e. And I added another and here to show that where the
employee number match with the employee number
from-- WHERE the employee number from
the department managers table-- for that manager.
That is where the employee manager matches with the employees
uh, number in the employees table,
okay? So, if we run this, right? This will return the same 24
-24 records,
alright?
And this time I do not just have the department name of that
of that manager.
I also have the first name and last name of that manager.
If you were to do this, normally, you would have written like
three join statements- two-three joins-- like two or three times
just to achieve this.
So, SQL offers you something quite easier.
Beautiful!
That was amazing, right?
I believe you are learning some interesting stuff here.
Great job on completing the third part of this project. In
this task, we learned how to use subqueries in the FROM
clause. I showed you how to get columns from two or three
tables in the subqueries.
Remember you must given an alias to the source of data and
properly reference the columns. In the next task, we'll pick it
up from where we left off here, and we will continue by learning
how to use subqueries in the SELECT clause.
Let us keep going.

You might also like