Fetching data from two or more tables(Joining of tables)

Selecting data from two or more tables(Joining of tables)

Hello Friends, today we will learn how to select data(records) from more than one table. For this purpose we will use joining of tables. Joining simply join one or two or more than two tables by some conditions as per need. One there is a joining between one table means one table is joined with self it is called self joining.

I will here consider a general example for joining which will be used mostly.

Create two tables as tbl_userlogin and tbl_userdetails as below:

Design view of tbl_userlogin:

create table  tbl_userlogin
create table tbl_userlogin

Query:

CREATE TABLE [dbo].[tbl_userlogin](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[username] [varchar](50) NULL,
[password] [varchar](50) NULL
)

Design view of tbl_userdetails

create table tbl_userdetails
create table tbl_userdetails

 

Query:

CREATE TABLE [dbo].[tbl_userdetails](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[username] [varbinary](50) NOT NULL,
[firstname] [varchar](50) NULL,
[lastname] [varchar](50) NULL,
[emailid] [varchar](50) NULL,
[phone] [varchar](50) NULL,
[address] [varchar](300) NULL
)

Now insert some records into both tables.

insert into dbo.tbl_userlogin(username,password) values(‘vipin’,’mishra’)
insert into dbo.tbl_userlogin(username,password) values(‘user1′,’pass1’)

insert into dbo.tbl_userdetails(username,firstname,lastname,emailid,phone,address)
values(‘vipin’,’vipin’,’mishra’,’vipin@pjsindia.com’,’8981881162′,’kolkata’)
insert into dbo.tbl_userdetails(username,firstname,lastname,emailid,phone,address)
values(‘user1′,’user’,’name’,’support@pjsindia.com’,’8013985540′,’varanasi’)

Now we will fetch records from both tables. See the query:

select u.username,d.firstname,d.lastname,d.emailid,d.phone,d.address from dbo.tbl_userlogin u
join tbl_userdetails d on u.username=d.username

This will return all the records where the joining condition will full fill. Here whenever there is same username in both tables the record will be fetched.

Now see the next example:

select u.username,d.firstname,d.lastname,d.emailid,d.phone,d.address from dbo.tbl_userlogin u
join tbl_userdetails d on u.username=d.username where u.username=’vipin’

This will return records only having username=’vipin’ in both tables.

Enjoy programming….

 

 

 

16 Replies to “Fetching data from two or more tables(Joining of tables)”

  1. buy and sell philippines

    Some shoppers may hesitate to buy a product because of the hidden costs involved.
    ‘If the customer is typing in Spanish, that gets routed through
    a translation engine and the English equivalent gets presented to the agent and
    gets captured as part of the interaction history, so the original and the translated form are both presented,’ said Nikhl Govindaraj,
    vice president of products at Moxie. If you do buy goods
    via the Internet and you experience problems with delivery of with the items you will have the same rights as you would if you have
    made the purchases in person, which means
    that you can go down the same routes to get the issues resolved.

  2. Nike Court Royale White Metallic Silver

    Howdy excellent website! Does running a blog like this take a
    lot of work? I have no knowledge of coding but I was hoping to start my own blog soon. Anyway,
    if you have any recommendations or tips for new blog owners please share.
    I know this is off subject but I simply had to ask.
    Thanks a lot!

  3. Xavier

    Wow, superb blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is great, let alone the content!

  4. To Boot New York Geoff

    Hi! This is my first comment here so I just wanted to give a quick shout out and say
    I really enjoy reading through your articles. Can you suggest any other blogs/websites/forums that
    go over the same topics? Thanks a lot!

  5. Rockport RocSport Lite Business Moc

    Undeniably believe that which you said. Your favourite reason appeared to be on the internet the simplest thing to keep in mind of.
    I say to you, I definitely get annoyed even as other folks consider issues that they plainly don’t recognise about.
    You managed to hit the nail upon the top and also defined out the entire thing without having side effect , other folks can take a signal.
    Will probably be back to get more. Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *