

Derived table vs subquery
source link: https://www.programmerinterview.com/database-sql/derived-table-vs-subquery/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What is the difference between a derived table and a subquery? Explain it with an example.
Both derived tables and subqueries can look the same and many people may even think they are the same thing, but there are definitely differences. Don’t worry, even if you don’t know what a derived table or a subquery is in SQL, it should be clear by the time you are done reading below.
A subquery is a SELECT statement that is nested within another statement – that’s why it’s called a subquery, because it’s like having a query within another query . Subqueries are usually used in the WHERE clause as a way to filter out certain rows returned in the result set of the outer query.
Let’s say that we have a table called employee with columns employee_name, last_name, employee_salary, and employee_number. And we also have another table called department that has columns called manager_employee_number and department_name.
Using those tables as our sample data, here is what a subquery looks like:
An example of a subquery
select employee_name from employee where employee_salary > -- this is a subquery: (select avg(employee_salary) from employee)
The SQL above will find all employees who have a salary that is above average. That SQL could be written much more simply, but it is just for illustrative purposes to show you what a simple subquery would look like.
Another example of a subquery
SELECT last_name FROM employee WHERE employee_number IN -- this is also a subquery: (SELECT manager_employee_number FROM department)
The SQL above can be used to find the last name of all employees who have managers.
Example of a subquery not used with a where clause
It is possible to have a subquery that is not used in conjunction with a WHERE clause. Here we are using a subquery to select an id and name from the student_details table and then insert that data into the math_study_group table.
INSERT INTO math_study_group(id, name) -- this is a subquery: SELECT id, name FROM student_details WHERE subject= 'Math'
Derived tables
A derived table is basically a subquery, except it is always in the FROM clause of a SQL statement. The reason it is called a derived table is because it essentially functions as a table as far as the entire query is concerned.
But, remember that a derived table only exists in the query in which it is created. So, derived tables are not actually part of the database schema because they are not real tables.
An example of a derived table will help clarify:
Example of a derived table
select max(age) from ( -- this part of the query is a derived table: select age from table ) as Age -- must give derived table an alias
In the SQL above, you can see that the derived table is inside the FROM portion of the SQL. The results of the “Select age from table” query are considered to be the derived table. And, hopefully it’s fairly obvious to you how the derived table essentially acts as a table from which something else is selected.
You may have noticed the “as Age” text on the bottom of the SQL above. We must include that otherwise we will get an error that says something like “Every derived TABLE must have its own alias”.
Of course, instead of using a derived table in the query above, you can simply write something like “select max(age) from table”, but this example was chosen for it’s simplicity in illustrating what a derived table would look like, certainly not for its real world applicability.
Summary of the difference between derived tables and subqueries
In simplest terms, a summary of the differences between derived tables and subqueries are:
- derived tables are used in the FROM clause - subqueries are used in the WHERE clause, but can also be used to select from one table and insert into another as we showed above
Recommend
-
36
This is a new series that has been percolating in my head for a while. When I help clients with SQL, we’re often adding (or removing) indexes to help SQL performance. It has been said (though not by me) that SQL re-write...
-
6
How to Improve ActiveRecord Query Performance with Subquery Caching Updated Aug 4, 2020 2 comments 10 minute read ...
-
6
SQL - Subquery returned more than one value. How to fix? advertisements -- Using orders, customers, employees, and order details tables, selec...
-
6
Fundamentals of table expressions, Part 9 – Views, compared with derived tables and CTEs This is the 9th part in a series about named table expressions. In
-
5
万向区块链蜂巢研习社 | 深度对话波卡生态基础设施:SubQuery、Subscan、zCloak NetworkWeb3.0 训练营二期的培训一直在稳步进行中,万向区块链蜂巢研习社第 61 期举行了 Web3.0 训练营二期基础设施专场直播分享,邀请了 Parity 亚洲负责人 Yaoqi...
-
7
2021-09-08 10:46 融资新闻 | 去中心化数据聚合器SubQuery完成900万美元融资 SubQuery 900万美元 轮次 ...
-
5
Subquery - Number and Quantity advertisements Looking to find all part numbers (d046d) with at least one record having vaule greater than zero...
-
8
What is the difference between correlated subqueries and uncorrelated subqueries? Let’s start out with an example of what an uncorrelated subquery looks like, and then we can compare that with a correlated subquery. Here is an exam...
-
9
October 24, 2021Comparison ModifiersComparison operators such as greater than, less than, equal, and not equal can be modified in interesting ways to enhance compariso...
-
9
MySQL「 Every derived table must have its own alias」错误修复方法 HiJiangChuan · 6天之前 · 13...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK