Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

get customer name in sql

Posted on 2014-07-18
6
Medium Priority
?
172 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 66

Accepted Solution

by:
Jim Horn earned 400 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 400 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 400 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
LVL 1

Assisted Solution

by:Pooja Katiyar Verma
Pooja Katiyar Verma earned 400 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 49

Assisted Solution

by:PortletPaul
PortletPaul earned 400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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…

670 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