Monday, January 31, 2011

Create inner join on 2 tables in SQL

Blog post: Research and test the creation of 2 table variables, insert 3 records into each, both having a column in common, and select all records from both tables by joining them together. You can use a join or an inner join for this.


 Declare @Frogs table  
(
id int primary key identity(1,1) not null
, legs int
, color varchar(36)
, spots int
)
Insert into @Frogs (legs, color, spots) values (5, 'blue', 3)
Insert into @Frogs (legs, color, spots) values (4, 'red', 7)
Insert into @Frogs (legs, color, spots) values (8, 'purple', 22)
declare @Ponds table
(
pid int primary key identity (1,1) not null
, id int
, location varchar (30)
, color varchar (20)
, weeds varchar (30)
)
insert into @Ponds (id, location, color, weeds) values (1, 'Fargo', 'blue', 'Full')
insert into @Ponds (id, location, color, weeds) values (2, 'Grand Forks', 'green', 'Half full')
insert into @Ponds (id, location, color, weeds) values (3, 'Hillsboro', 'blue-green', 'Quarter full')
select f.legs
,f.color
,p.location
from @Ponds p
inner join @Frogs f
on f.id=p.id
order by f.id

No comments:

Post a Comment