|
Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - Using Subqueries in the FROM Clause
By: FYIcenter.com
(Continued from previous topic...)
How To Use Subqueries in the FROM Clause?
If you have a query returning many rows of data, and you want
to perform another query on those rows, you can put the first query
as a subquery in the FROM clause of the second query. A subquery used
in this way become a temporary table, and you must provide a table alias
name for the subquery as in "SELECT ... FROM (SELECT ...) aliasName".
The following statement shows you how to use a subquery as base table for the main query:
SELECT * FROM (SELECT l.id, l.url, r.comment
FROM fyi_links l LEFT OUTER JOIN fyi_rates r
ON l.id = r.id) WHERE url LIKE '%er%'
GO
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'WHERE'.
SELECT * FROM (SELECT l.id, l.url, r.comment
FROM fyi_links l LEFT OUTER JOIN fyi_rates r
ON l.id = r.id) s WHERE s.url LIKE '%er%'
GO
101 dev.fyicenter.com The best
102 dba.fyicenter.com Well done
103 sqa.fyicenter.com Thumbs up
107 www.winrunner.com NULL
The error on the first query is caused by the missing alias name to name output of the subquery
as a temporary table.
(Continued on next topic...)
- How To Join Two Tables in a Single Query?
- How To Write a Query with an Inner Join?
- How To Define and Use Table Alias Names?
- How To Write a Query with a Left Outer Join?
- How To Write a Query with a Right Outer Join?
- How To Write a Query with a Full Outer Join?
- How To Write an Inner Join with the WHERE Clause?
- How To Name Query Output Columns?
- What Is a Subquery in a SELECT Query Statement?
- How To Use Subqueries with the IN Operators?
- How To Use Subqueries with the EXISTS Operators?
- How To Use Subqueries in the FROM Clause?
- How To Count Groups Returned with the GROUP BY Clause?
- How To Return the Top 5 Rows from a SELECT Query?
- How To Return the Second 5 Rows?
- How To Use UNION to Merge Outputs from Two Queries Together?
- How To Use ORDER BY with UNION Operators
|