Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm a little confused, e.g say that We have a query and it looks like in the following:

select * from (select c1,c2 from table_name where condition) table_name;

I want to understand how it works? I mean I never use in my during sql time subquery after the from clause, but I want to understand how it works?

What I have tried:

I readed a book about sql and there is a sentence that subquery means that it generate derived table. and as I understand that's why we can use subquery after the from clause and sql generate sucessfully syntax because of this rule, isn't it?
Posted
Updated 23-Oct-22 19:05pm

1 solution

The example you give isn't too useful: it doesn't need to include a subquery because it just selects everything from it anyway.

Subqueries are useful when you want to extract specific data, and then use it to match other data together: for example in a WHERE clause.

Suppose you have a table of employees, and you want to find all the employees in your department whose salary is higher than yours - you use a subquery.
First you write a query to return your salary:
SQL
SELECT Salary FROM Employees 
 WHERE EmpName = 'Proqramlaşdırma Dünyası'
That returns a single value - your salary.
You can then use that as a subquery to find the info you want:
SQL
SELECT * FROM Employees
 WHERE Salary > (SELECT Salary FROM Employees 
                  WHERE EmpName = 'Proqramlaşdırma Dünyası')
   AND Department = 'Software'
Make sense?
 
Share this answer
 
Comments
vor_zakon_developer 24-Oct-22 1:53am    
That's not what I meant here. What I meant was this. How sql decided that a subquery after the from clause like table?
OriginalGriff 24-Oct-22 2:22am    
Do you want to try that again using English this time?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900