Solved

get customer name in sql

Posted on 2014-07-18
6
159 Views
Last Modified: 2014-11-05
I have two tables in my database. Table A and B. The unique identifier in both tables is the customer number. All the information I need in my select query is in table A but the problem is I will only be able to see the customer number but with no name to go with it.  I need to include the second table to show the customer name with the customer number in my select query results. How would the sql query look like for this. I am new to SQL.
0
Comment
Question by:paul_techy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 100 total points
ID: 40205008
That would be a JOIN.

<air code, rename the obvious to meet your needs>
SELECT a.customer_number, a.some_column, a.some_other_column, b.customer_name
FROM TableA a
   LEFT JOIN TableB b ON a.customer_number = b.customer_number

Open in new window

If you search articles on JOIN you'll find a lot.   Not sure what your specific needs are, or else I'd mention one.
0
 
LVL 4

Assisted Solution

by:Philip Portnoy
Philip Portnoy earned 100 total points
ID: 40205204
Please note that in the above comment LEFT JOIN will show you all numbers, even when they don't have names in the 2nd column. If you need to show results only for users that do have corresponding records in 2nd table you should use JOIN (which is the same as INNER JOIN).
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 100 total points
ID: 40205456
select a.*, b.name
from tableA a inner join tableB b
on a.customernumber = b.customernumber

If you want all of tableA's information even if tableB does not have a matching customernumber, just change to a left outer join as pointed out above.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Assisted Solution

by:Pooja Katiyar Verma
Pooja Katiyar Verma earned 100 total points
ID: 40205470
Have to perform a join between A and B table. But first you need to decide on data set you want as output. i.e. all the records from A table or only matching records between A and B.

Type of Join:
INNER JOIN :This join returns rows when there is a match for join condition in both the tables.
ex. SELECT *
FROM A t1
LEFT JOIN A t2 ON t1.ID = t2.ID -- Only matching rows

OUTER JOIN:There are three different Outer Join methods.
LEFT OUTER JOIN :This join returns all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.
SELECT t1.*,t2.*
FROM A t1
LEFT JOIN B t2 ON t1.ID = t2.ID -- All rows from A table and matching row's value from B tabke

RIGHT OUTER JOIN :This join returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.
SELECT t1.*,t2.*
FROM A t1
RIGHT JOIN B t2 ON t1.ID = t2.ID -- returns all the rows from B and data from A table only for matching ones

FULL OUTER JOIN :This join combines left outer join and right outer join. It returns row from either table when the conditions are met and returns null value when there is no match.
SELECT t1.*,t2.*
FROM A t1
FULL OUTER JOIN B t2 ON t1.ID = t2.ID -- returns all rows from A and B table
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 100 total points
ID: 40208122
If you are new to SQL then you simply have to learn about joins.

Using SQL without joins is like driving a car but never using the steering wheel (i.e. it won't get you very far).

I suggest this for assistance  visual guide to joins

For your specific question I would recommend you start with an INNER JOIN
then try the LEFT JOIN

If there is no difference in the number of rows produced that means all customers have a name and so you should use the INNER JOIN (as that join type is more efficient).
0
 

Author Closing Comment

by:paul_techy
ID: 40425351
thanks I figured something out with your help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question