select all columns from 2 tables having duplicated columns - sql

I have 2 tables Customer and Customer_address connected by a customer_code column.
I need to create a select query to bring all columns from both tables. The problem is that I need to use aliases for the duplicated columns.
I tried:
select * from Customer left join Customer_address on Customer.customer_code=Customer_address.customer_code
Not working as expected. How can I avoid the duplicated without having to type all columns ?

If the only duplicate column name is the one used for the JOIN, you can use the USING clause:
select *
from Customer c left join
Customer_address ca
using (customer_code);

Related

Left Join two tables with one common column and other diff columns

I have two tables where main table has 10+ columns and second table has 3 columns with one common field. My problem here is I am not able to get exact count with left outer join as main table. I am seeing more count than actual. It might be due to one of the field I am trying to get is not in main table which is in second table.
Table 1: master_table
Table 2: manager_table
Master_table :
ID,
Column1,
Column2,
...
Column10
manager_table:
ID,
Column2_different,
Column3_different
I am trying to join using Left Join to get same records as present in master table.
Select table1.columns, table2.columns
From table1
Left join table2 on table1.ID = table2.ID
The above is not giving me exact record count as in master table (table1) , it is giving me more count as the table 2 other field is not present in table 1 .
Can someone help me here ?
TIA
I believe that an INNER JOIN would be better than a LEFT JOIN. Need some sample data to be sure, but if you're getting a higher count than you'd expect upon joining the tables this is probably because the LEFT JOIN is returning everything from both tables. An INNER JOIN will only return data that appears in both tables.

Duplication of records due to Left Join

I have 2 tables named Product Category, and Transactions. In Transaction Columns there are two columns called Transaction ID, and Product_category_code. In Product Category Table there are two columns called Product_category_code, and Product Name. I'm trying to combine the two tables by having the corresponding product name written next to the transaction ID. I'm using Left JOIN but somehow it is giving me same results as right join.
The code I'm using is:
SELECT *
FROM [dbo].[Transactions] AS T1
LEFT JOIN [dbo].[Product category] AS T2
ON T1.prod_cat_code=T2.prod_cat_code
order by transaction_id desc
I'm getting 5 records for the first transaction after using left join when I should only be only getting one. How can I fix this?
First few entries of T1 is
First few entries of T2 is
First few entries of output is
Thanks
You need to join on both the category and sub-category codes

Creating a table out of two tables in SQL

I'm trying to create a table based off of two tables. For example, I have a column in one table called Customer_ID and a column in another table called Debit_Card_Number. How can I make it so I can get the Customer_ID column from one table and the Debit_card_number from the other table and make a table? Thanks
Assuming Two Table Names as TableOne and TableTwo and CustomerID as a common Attribute.
CREATE TABLE NEW_TABLE_NAME AS (
SELECT
TableOne.Customer_ID,
TableTwo.Debit_Card_Number
FROM
TableOne,
TableTwo
Where
tableOne.CustomerID = tableTwo.CustomerID
)
Look into using a join. Use Left Join to give you the id, even if there isn't a matching card number for that id. Your value to match on will probably be the id, assuming that value is in the table with the card number
create table joined_table as(
select t1.customer_id, t2.debit_card_number
from t1
inner join t2
on t1.matchValue = t2.matchValue
)

SQL - joining two source columns to one target column

I have two tables. The first table contains the code number, the second table contains the name corresponding to that code number. The first table has a source code number and a target code number. The second table contains both these code numbers in separate rows. So the tables look like this
Table1
|Source_code|Target_code|
|253 |568 |
Table2
|Code|Name |
|253 |John |
|568 |Steve|
I want a result like this:
|source_code|name |target_code|name |
|253 |John |568 |Steve|
I did this by joining both the tables twice in a single query, as follows:
select A.source_code, B.name, A.target_code, C.name
from table1 A
join table2 B on A.source_code = B.code
join table2 C on A.target_code = C.code
This doesn't look like the best way to handle this query. Is there a better way to do this?
Joining to table 2 twice is the way to handle this. Why? because you have in table1 TWO foreign keys to Table2. Since tables should be joined on keys and you have two keys to one table, your solution is almost appropriate.
The only issue I see is you should be joining on table2's code not the names.
SELECT A.source_code, B.name, A.target_code, C.name
FROM table1 A
INNER JOIN table2 B
on A.source_code = B.Code
INNER JOIN table2 C
on A.target_code = C.code

Compare a column in two different tables

Say I have two tables, Table A and Table B, and I want to compare a certain column.
For example,
Table A has the columns: IP,Host,App
Table B has the columns: IP,Datacenter,Server,Model,Last_Updated
How do I compare the IP column between the two tables to get the differences?
I know if the tables have the same columns I can use a union and 'minus' to get the differences but I wasn't able to figure out a way if the tables have different columns.
Thanks!
SELECT *
FROM A
FULL JOIN
B
ON a.IP = b.IP
WHERE a.IP IS NULL OR b.IP IS NULL
This will output all columns from non-matching rows in both tables, with NULLs on either side.
select distinct column_A FROM table_1 where column_A not in (SELECT column_A FROM table_2)
you mean you want to get all IPs in table A that are not in table B?
select IP from table A
MINUS
select IP from table B
Did I understand the question correctly?