SQL fetch multiple values on join - sql

Hi I have an SQL table which has two tables which make reference to the same foreign key in a separate table twice... something like
SALES table
idSales idClient1 idClient2
1 1 2
CLIENT table
idClient ClientName
1 Bob
2 Mick
I want to join the SALES table to the CLIENT table and return data as follows:
idSales idClientClientName1 idClientClientName2
1 Bob Mick
Can anyone help with the SQL for this? I'm getting ambiguous column name errors on my join.
Thank you

You need to basically join table Client on table Sales twice because there are two columns on table Sales that are dependent on table Client.
SELECT a.idSales,
b.ClientName ClientName1,
c.ClientName ClientName2
FROM Sales a
INNER JOIN Client b
ON a.idClient1 = b.idClient
INNER JOIN Client c
ON a.idClient2 = c.idClient
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
But when one of the columns or both columns are nullable, INNER JOIN will not give you all records from Sales because it will only select where it has atleast one match on the other table. Instead use LEFT JOIN.

I might add that in cases like this, I use table aliases that hint at what entity you are linking to in the joined table. If for example, the foreign keys were to an address table, and you had a work address, and a Home address, I would use tables aliases of h and w for the two joins. In your case, i.e.,
Selext s.idSales,
c1.ClientName ClientName1,
c2.ClientName ClientName2
From Sales s
Join Client c1
On c1.idClient = s.idClient1
Join Client c2
On c2.idClient = s.idClient2

For those beginner SQL folks who may see this question in the future, it's helpful to add in the AS words, it makes it clearer still:
SELECT
Sale.idSales,
c1.ClientName AS ClientName1,
c2.ClientName AS ClientName2
FROM
Sales AS Sale
INNER JOIN Client AS c1 ON Sale.idClient1 = c1.idClient
INNER JOIN Client AS c2 ON Sale.idClient2 = c2.idClient

Related

Parent child relationship Join tables in oracle sql

I have a requirement below.
Now I have to get output like below
How can this be achieved ?
I have written the below SQL but parent_position_id is coming, not parent_position_code
select
hapf.position_code,
pphf.parent_position_id
from
hr_all_positions_f hapf, PER_POSITION_HIERARCHY_F pphf
where
hapf.position_id = pphf.position_id
Should I write a sub query? How should I proceed ?
This is Oracle SQL
Thanks,
Shivam
Noone ever said you could only join a table in once:
select
chi.position_code,
par.position_code as parent_position_code
from
hr_all_positions_f hapf
INNER JOIN PER_POSITION_HIERARCHY_F chi on hapf.position_id = chi.position_id
INNER JOIN PER_POSITION_HIERARCHY_F par on hapf.parent_position_id = par.position_id
Bear it in mind; I see people coming to thinking all the time that they can only join a table once. If one table decodes a value in 3 different columns, then you sure can join that same table in 3 times... Imagine if it were an address table, and a Student had a HomeAddressId, WorkAddressId and StudyAddressId, and the Address table held all these addresses - you'd join the addresses table to the Student table 3 times to get all the data..

SQL INNER JOIN without linked column

I have an UltraGrid displaying customer information in it. The way the database is set up, there are 2 tables. Customers and Customer_Addresses. I need to be able to display all of the columns from Customers as well as Town and Region from Customer_Addresses, but I'm under the impression that I'd need Town and Region columns in the Customer table to be able to do this? I've never used an INNER JOIN before so I'm not sure if this is true or not, so can anybody give me pointers on how to do this, or if I need the matching columns or not?
Does it even require an INNER JOIN, or is there an alternative way to do this?
Below are the design views of both of the tables - Is it possible to display Add4 and Add5 from Customer_Addresses with all of Customers?
As long as you have another key column you can use to link the tables (ex. ID_Column), it is better that you use LEFT JOIN.
Example:
SELECT c.col1, ... , c.colN, a.town, a.region FROM Customers c
LEFT JOIN Customer_Addresses a ON a.ID_Column = c.ID_Column
In order to clarify how JOIN types work, look at this picture:
In our case, using a LEFT JOIN will take all information from the Customers table, along with any found matching (on ID) information from Customer_Addresses table.
First of all you need some column in common in two tables, all what you have to do is:
CREATE TABLE all_things
AS
SELECT * (or columns that you want to have in the new table)
FROM Costumers AS a1
INNER JOIN Customer_Addresses AS a2 ON a1.column_in_common = a2.column_in_common
The point is what kind of join do you want.
If you can continue the process without having information in table Costumers or in table Customer_Addresses maybe you need OUTER JOIN or other kind of JOIN.

how to join one table to multiple other tables

i have a query which is used to generate reports. There are multiple fields to be displayed. One requirement is such that i need to join one table to different tables with different aliases for data. e.g., table 1 employee id with employee table for knowing the full name. similarly table 2 employee id with employee table for table 2 employee id full name. PFB the query:
select * from office o
left join employee e
on e.id=o.id
left join master m
on m.id=o.id
left join student s1
on e.id=s1.id
left join student s2
on m.id=s2.id
Can we optimize this query to use only one join statement of student table instead of multiple table join statement? I need to reduce the number of tables used in the query since i'm getting the error as too many tables in the query maximum allowed is 50. Please help. Appreciate.
Can we optimize this query to use only one join statement of student
table instead of multiple table join statement?
No, I would not call it query optimization. However, for reporting purposes you reduce the joins by creating views.
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1510/html/iqrefso/X315714.htm

Inner joins with 2 foreign keys to one primary key

I have a table called
branch (branchid, branchname)
and another table called transfer
transfer(tranferid, sourcebranch, destinationbranch)
both sourcebranch and destinationbranch are Fk to the branchid of of branch table.
I need to show a query that looks like this
Tranferid Source Destination
4 uk us
but all I can get is something like this
Tranferid Source Destinationid
4 uk 3
query sample
select tranferid, branch.branchname, transfer.destinationbranch
from transfer
inner join branch on branch.branchid == transfer.sourcebranch
How do I get the destination branch to show. CTE on my mind
You need to join table branch on table transfer twice so you can get the value for each column.
SELECT a.*,
b.branchName AS sourceBranchName,
c.branchName AS destinationBranchName
FROM transfer a
INNER JOIN branch b
ON a.sourcebranch = b.branchID
INNER JOIN branch c
ON a.destinationbranch = c.branchID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins

Retrieve different row from same table

i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.