Solved

get customer name in sql

Posted on 2014-07-18
6
150 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
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 31

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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.
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now