562 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Derived Tables in SQL Part one of a two-part series of blogs |
---|
A derived table is a technique for creating a temporary set of records which can be used within another query in SQL. You can use derived tables to shorten long queries, or even just to break a complex process into logical steps.
This blog is part of our complete SQL tutorial. You can learn more about derived tables and many other techniques on our Advanced SQL training course. |
A derived table is an example of a subquery that is used in the FROM clause of a SELECT statement to retrieve a set of records. You can use derived tables to break a complex query into separate logical steps and they are often a neat alternative to using temporary tables.
You can learn more about derived tables - and their successors, CTEs or Common Table Expressions - on one of our SQL courses.
To create a derived table you need to start with a valid SELECT statement. Here's an example of a query which we will convert into a derived table:
SELECT
FilmName
,FilmRunTimeMinutes
FROM
tblFilm
To turn this into a derived table we need to nest it within the FROM clause of another query, like so:
SELECT
*
FROM
(
SELECT
FilmName
,FilmRunTimeMinutes
FROM
tblFilm
) AS MyDerivedTable
WHERE
FilmRunTimeMinutes < 100
Notice that the original query must be enclosed in a set of parentheses and that it must also be given an alias - here we have inventively called the derived table MyDerivedTable.
The example we've given above isn't particularly useful other than to demonstrate the basic syntax of a derived table. The next part of this series will cover a more complex example to show how derived tables can actually be useful!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.