get customer name in sql

Posted on 2014-07-18
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.
Question by:paul_techy
LVL 65

Accepted Solution

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.

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).
LVL 32

Assisted Solution

awking00 earned 100 total points
ID: 40205456
select a.*,
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.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


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 *
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.*
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.*
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.*
FULL OUTER JOIN B t2 ON t1.ID = t2.ID -- returns all rows from A and B table
LVL 48

Assisted Solution

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).

Author Closing Comment

ID: 40425351
thanks I figured something out with your help

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

786 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