3

I have a question about the basic syntax of joining with derived tables.

Is this teh basic syntax that is used:

select *
from table1 a

inner join (select * from table2) as T1

on 1.ID = T1.ID

Will that work?

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
Neil Keicher
  • 39
  • 1
  • 1
  • 5

3 Answers3

11

you're asking about joining two subqueries? try:

select * from
(select * from table1) t1 inner join
(select * from table2) t2 on
t1.id = t2.id
Beth
  • 9,531
  • 1
  • 24
  • 43
1

In your posted query change on 1.ID = T1.ID to on a.ID = T1.ID and it should work fine but why do you even need that? You can directly join with table1 and no need of inline/derived table like

select *
from table1 a
inner join 
table2 T1
on a.ID = T1.ID
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Okay, thanks Rahul. You and Beth both gave pretty good answers. I'll mark hers as the answer, but only because she posted first. But thank you so much for your help. I appreciate it! – Neil Keicher Oct 31 '14 at 21:06
0

Also useful if you're trying to use derived table defined using VALUES, i.e., to get around the 1000 row limit on inserts to temporary tables (multiple row insert). Make sure to specify the names of the variables/columns in the VALUES statement i.e., (ids), after the alias i.e., v.

Example:

SELECT *
FROM (VALUES (1,'c1'),(2,'c2'),(3,'c3')) v (ids, textcol)
JOIN TABLE1 t1 on t1.ids = v.ids
Brian D
  • 2,570
  • 1
  • 24
  • 43