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
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
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.
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.


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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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. …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

732 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