In this video, we will learn Subqueries in PostgreSQL in a simple and practical way.
A subquery means writing a query inside another query. It is also called an inner query or nested query. Subqueries are very useful when you need to calculate something first and then use that result in another SQL query.
In this PostgreSQL tutorial, we will understand what is wrong with normal aggregate queries, why we need subqueries, and how scalar subqueries and list subqueries work with real examples.
We will also cover the syntax of subqueries, best practices, tips and tricks, and live practical query solving so that you can understand how to use subqueries in real projects.
In this video, you will learn:
What is a subquery in PostgreSQL? Why do we need subqueries? What is a scalar subquery? What is a list subquery? Difference between scalar subquery and list subquery How to use subqueries with WHERE clause How to use AVG(), MAX(), and IN with subqueries PostgreSQL subquery syntax Best practices for writing clean subqueries Practical PostgreSQL subquery examples
This video is perfect for beginners who are learning PostgreSQL, SQL queries, database concepts, and backend development.
⌛TIMELINE⏳ 0:00 Intro to Subqueries in PostgreSQL 0:20 What is wrong with this query? 1:10 What is a subquery? 2:10 Types of subqueries 3:30 Scalar subquery explained 5:20 List subquery explained 7:30 Syntax of subqueries 8:40 Best practices, tips and tricks 10:30 Query 1 - Practical session 11:53 Query 2 - Practical session
AI Generated·May contain errors
Video Summary
AI Generated
This video provides a comprehensive introduction to subqueries in PostgreSQL, explaining how to nest one query inside another to perform complex data retrieval that standard aggregate functions cannot handle alone in a `WHERE` clause.
Hello, what's up, guys?
Welcome to all of you
I'm PostgreSQL series
Today in this video, we will see
Subqueries
This is a very important topic.
Why do we have to be liked?
I'm giving you a simple example here
Your boss asked you
Bro, I need the list of all those courses
Give water to water
Jinka price na
The course whose price is higher than the average
That sounds very simple when you hear it.
For example, here you can see
Just look at this method.
How easy it is
Select everything from courses
Where the fee is greater than
Normal your aggregate function
We already have it.
Average fee
I feel like I'll keep doing it, I'll keep doing it for you
I will get the data for all those courses.
Jinka price
The average price is higher.
But the thing is
Bro, he won't do this work at all.
This doesn't work with tarika.
Bas
Here, when you run this
When you get an error
There will be tension in the tab.
Oh my God
What should I do now?
If you understand the concept of subqueries here
Just look at you, so relaxed—what are you going to do?
Look at this
Is tarika se
From courses where the fee is greater than
Look here, from this side
I'm looking at this part.
What you have written inside the parentheses
Matlab, this is one of your queries.
Alright
You wrote another query inside it.
So this is your subquery
Query within query
A query inside a query
This is it
This is your subquery
Wherever
Select star from courses
Where the fee is greater than
Greater than
I need to find the Hume average price, right?
I cannot directly use the aggregate function.
That's why
Select
Average
Your aggregate
From courses
Is this one of your queries, sir?
Who goes first
This one will be yours first.
Whenever you use a subquery
It's an inner query
That will run first
It has been running before, hasn't it?
Whatever output it has
Let's say 4,000
What on earth has happened?
Fee greater than 4000
What is its output
And aaram sab
A list of all those courses
Show it by tapping tap tap tap
Here you go, boss
This is your subquery
I hope you now know it
Alright, nobody then
We have several types of subqueries.
You must understand this
And there is one more type
I haven't kept it for my own sake.
I'll keep the video in the main gallery.
If only you understood this much today
What is a subquery?
Guys, your life will become easier.
And a perfect example
I've already shown it to you.
So what we have
We have an inner query
Sometimes the inner query
Subquery
What kind of like
You can use it together with this.
Not even by a single name
You can represent Inko
You've got that type of thing figured out, then
As the subquery
What query has been written here?
A query inside another query
I've finished speaking.
Simple, yes
In general
Grab it, because this is what it is
Yes, that's your one in general.
Hey brother
What is a subquery?
A query inside another query
Yes, one
Yes, that's it.
What can I say, obviously
Yeah, inner
And written too
The inner query will be
Whoever runs first
Of course, it will go first.
Then the outer query
This is the outer query.
The one that is the first query
That value there
Based on his foundation
Your result
Who will take the lake?
For example
Find employees
Whose salary
More than
Average salary
First, the courses
They were talking
Here we are
An employee's
Data retrieval
Talk, please
Jinki's salary
I am earning an average salary
It's more.
Basically
No way
To wo kuch is trike se
Select name salary
From employees
Where is the salary?
Up to here
You have not yet
The bad one had left.
It just ki
Directly
Aggregate function
Average salary
Instead of writing
We need to make sure
To write this parenthesis
And then
An inner query
You will be written
To inner query ka
Whatever result comes out
This will be used here.
And based on that
First, your this
The outer query will execute
And then
You will get the
Final result
Right
Then what we have
We have this.
Scalar query
Ab scalar ka
It's a simple MATLAB.
Here
Whatever the inner query
You will use it
That will return
Only one single value
What is written here?
Waise
Yes, that's exactly what it says.
Only one value
What is MATLAB?
Look
If you look here
What is written here
Select
Max
From employee
You are a
I mean, one thing
Please tell me this
If you are referring to this
Max aggregate function
Used by you
Hi there
Let's imagine
Here
You have
100 years
You have
200 pieces
You have
300 they
And then you have
600
Main aap se
If I were to say that
There's something I want to tell you
Say it
Among these
Maximum
Which number is it?
Brother
Any rest at all
Dress a lot
And that's your
This one
This one here
You are the best.
This one is your maximum.
You keep thinking, then
Why did I tell you this?
Here
My main reason
This is it
Hi
What does scalar mean?
It will return
Only one value
A single value
You're the one, aren't you?
Jab
If you
In this way
Aggregate function
You will use
To whom it may concern
Aapko single value
It will be returned soon.
To yeh
It is a path.
Look at this
Well then, this one too
Subquery is there
Because we have
A query inside
A query
Hi there
But our query to
Andar bhi
If someone_HIDDEN
Typed from you
Taken it.
Alright, buddy
Inner query me
Just take a look
How much?
Use Hua Hai
To you
A single value
Hi here
I am having it.
So it's a
Scalar subquery
This is a scalar query
You are doing it this way
You can do it.
Subquery
Find employee
With the highest salary
In this way
And by the way
This is also yours
Scalar subquery
Who is here even here
You are given a single value
It's Milne.
Then
We have this list
Subquery
It's too simple.
Scalar means one
List
There are too many items on the list.
So
Yes
Inner query
You give me multiple values
You are returning
I mean
Don't even bother arguing inside.
Subquery
Tell him to do it
Represent Karlo
Such subqueries
That returns
Many values
In one column
It is used with
In
I understand.
Not only in
I think
I don't remember.
I'll show it here.
In hai
You have it all.
One more two are there
If you
You will use them
Right
If you are referring to this
Already PostgreSQL
They provide Hume.
If you
You use them
Obviously
Will receive multiple values
Right
If you remember
To in karke
When we used to write
In
And here we are
2, 3, 4
MATLAB
This guy here
It could happen to me too.
It could happen to me too.
To multiply multiple values
It is returning.
To this subquery
That returns
Many values
That falls
That fall
Under your
List subquery
Here
You can see that
Me
What is there to know?
Over here
Find employees
Working in the Mumbai department
Their employees'
List required
On the Mumbai department
You are working
I don't know
Mumbai Department
What is the name of the department?
But I guess
People from Mumbai state
Yeah
Right
Working in Mumbai
Mumbai's department
So
Select
Name Department ID
Fromเพิ่ม
You see here
Department ID
Me ye jo
It is written
After that
You see here
Here
Just look here
This subquery of yours
We have an ID.
From department
Where is the location?
Equal to Mumbai
To
At location
Anywhere in Mumbai
Hogi
Departments
From the table
Their ID, please.
Milega
From now on
Here
It could be possible.
It is possible.
It could be a teenager.
Four as much as
To
Ab
Is it my department?
Basically
Which employees
They are working.
Me him
List
It will go.
And
Here
For you all
Quick memory tip
It's raining.
Like
Subquery
MATLAB
Query within a query
You in general
It's extremely general.
That's exactly how it is.
Someone pushed him from inside.
To we have
Basically MATLAB
It returns a value
Such a subquery
And jo multiple values
You are returning
Your list subquery
Alright
Here it is
We are going.
Like Hamisa
Main will give you the syntax
Potato ho
Here it is.
We have already seen it
But still I
Quickly
Select it for you
Column name
From your table
That's normal.
You write
Where clause such as
There's no tension.
After that
This is what appeared
This is yours
Subquery
You have come here
An inner query
Wrote it down
Select
Average
Jaha
If you
After where
I want to write
As in other cases
Where condition
If you
Subquery
Want to do it
That's it
Is trike se
Here
You can see
An operator
To you
Hello
Lagega
Because
Where
Your
Condition me
It is there.
Where
Greater than
Less than
Equal to
In
Between
All of these are those ones
And
Wrote it too
Notice
The mandatory
Parentheses
Around the inner query
This inner query here
I'll show it to you
In the middle of it
What's this parenthesis here
You must
That's what I need
Then
Subquery
With
The IN clause
Abhi just
We saw it, then
Like
Where
For an id
In
And then
Here
You can see
Select id
From another
Table
Even here too
Understand it now
Query
Inside another
Query
Seal
So
This is it
Here
A little bit of Doshra
Tips and tricks
Humesa's daughter
For you all
Here
And
We are very
Clearly
You can see
Ki
Parentheses
Mandatory
Yes, this is for you
Hello
Logega
No matter what
Please
Don't play dumb with me
More than one
Row error
So your audience
Okay, we're talking about you people.
This common mistake
If you
Write
If you write
Where is the fee?
Equal to
Select
Tuition from courses
The database
Will panic
Because the
Inner query
Retains 20
Various fees
But the equals sign
Is
Got it, guys
What is happening here?
What's happening here is happening right here.
Who are you equal to?
What are you using?
Over here
You are
Using
Equal to
Assignment operator
And whenever you
Equal to
Write
What will you write?
4 is equal to
4
Okay, right
Is 4 equal to
5
No
But if anyone says this
Is 4 equal to
5
6
7
8
9
10
Alright, there's also 4.
12
13
Bro, are you even thinking, man?
What happened?
Main kisko kisi
Main 4 ko
Five to do
6 to do it
7 to do it
That same problem is occurring.
And what is this?
It is said that
If you have
Multiple values here
To go with
In
We had already seen this.
If you remember
So this is it
You'll have to think about it in Matlab
Where are you?
You are using clause on me
What do you think will happen?
Here is the assignment
Operator
In operator
Whatever it may be
Greater than
Less than
Hi there
So and then, guys
Final tip
We have a performance.
Warning
And this is
Your
Correlated
Sub-queries
I won't do it for you
Bataya
Main aapko
In the Kalke video
I'll do it.
Anyway, whatever it is
We will do this
Stay there
And
Humesa ki taar
For you all
Just mine
Practice questions are available
Iska aap
Screenshot
The bridge
Nehi to
I am with you
Notes
Everything then
Just sharing, that's all
In description
And comment section
Where are you going?
You get it
You can do it
Alright, guys
To aap hum
Siddha is ours
Questions come up
Simple one
The question is
Since our users
Don't have an age
Column
Let's use the
Courses
Table
To find premium
Content
We want to
Find all courses
That cost strictly
More than the
Average course fee
Guys, it's simple.
That's what I told you
It's going well.
To me, all of them
List of courses
Need
Jinka price
Average course fee
Easy
Once you know
It's over then.
It's easy.
Select title
And then fee
Courses
Fee
He works.
I'm removing this.
You think this
If we were to do this
Let's run it
To
You see here
Functions
Aggregate functions
Not allowed
Aggregate function
If you
Where clause
Not at all
What can you do
About your doubt
It's cleared.
Your
That's it.
For him
It's a natural hum.
Do it.
If only it were tomorrow
Run this.
Will do
To
You
Barabar output
Will come
Another job
You could have done that
Your output
Is it the same or not?
For him
Run it.
And run it
Karo
Do you know this?
Go ahead
Okay
Here is the course
Me jo
Average price
Where are you?
3,610 rupees
And jab
Mai isko run
Card game
What I am getting
I am getting
All the
List
Of
Courses
Whose values
Greater
Then
The average
Values
And look here
It's above 3600.
This is mine
Perfectly
He is working.
And then
The second one
It is there.
Nowhere
Simple.
Let's find
The names
And emails
Of all
The user
Who belongs
To the
Engineering
Department
Surat Ashmās is also spreading, and the lofty four are descending, the great Pāṇḍava's Ajāvūrṇa, and Ajāvūrṇa's.
Pro Max
iPhone 16 standard model
It all comes together, doesn't it?
I'll keep it simple.
and that's your one
I like doing
Tell me in the comments why we have this I like
rather than just like
Tell me by commenting
name, email
Names and emails are required from the simple users table
We have put it down.
Where their department should belong
From engineering
Department ID
Also put it aside
What was needed here was that mechanical thing.
Maybe civil
Multiple values are being returned.
Your subquery is here
That's the reason why we are using the in operator.
and select ID from departments
where
The name I like is equal to
Your way of handling the department like this
The name we are using is this one.
Here too, it is here.
If you run it in this manner
And boom, look at this, look at this
He has come comfortably.
Not for today's video, though
I hope you'll really like the video.
If you have any doubts, please definitely let me know by commenting.
Please definitely like the video
Definitely set up the bar with your friends.
See you in the next video
Give me the til, the cow has thrown it away.
What is tech?