What will be the column name after joining two tables in SQL with different join columns - sql

Let's say I write the query
SELECT * FROM table1 JOIN table2 ON table1.ID = table2.student.Number
There are the same values in column "ID" of table1 and in column "Number" of table2.
How will the column with the joined values be named in the result table?
"ID" or "Number"?

Both of them. JOIN does not remove common columns - indeed, if you're doing a join on two tables which share a column name, the result will have two columns with that name. (You can disambiguate them using the table name, e.g. SELECT table1.id FROM table1 JOIN table2 ON (table1.x = table2.y).)

Related

How to check if the values of a column exist in a column of another table in SQL Presto or Hive?

So, let's say that I have two tables. Table1 and Table2 and I want to see if the values of the column ID of Table1, also exist in column ID of Table2.
The query that I tried was this one:
SELECT ID
FROM Table1
WHERE ID IN (SELECT ID FROM Table2)
but although I see that they have some values that are the same, the result is 0.
So, how can I see which IDs of Table1, also belong to the ID column of Table2?
Try with join:
SELECT ID FROM Table1 LEFT JOIN Table2 ON Table1.ID=Table2.ID

Using While SQL Server

I have two tables and both contains columns Names and ID_Number.
table1 contains columns Names, ID_Number, Price_date
table2 contains columns Names, ID_Number, historical_date, comments
I am trying to do a loop such that it will start from the first value in ID_Number column in table1 and see if it matches with any value in ID_number column in table2.
If there is a match, then compare the 'Names' for the two tables for that particular ID_number. If the names does not matched, then in the comments column, enter the Name from table1 and enter the Price_date from table1 to historical_date in table2.
Don't use loops in SQL, as long as you can avoid them. SQL is a set-based language, that is not optimized for iterative processes.
From your explanation, it seems like you want an update statement with a join. This should do what you want:
update t2
set t2.comments = t1.names, t2.historical_date = t1.price_date
from table2 t2
inner join table1 t1
on t1.id_number = t2.id_number
and t1.names <> t2.names

Is there way to get table name reference before column names in Oracle?

I have two tables that have many same column names so I need to add the table name before the column names when I join the tables. Is there some way to get table name reference before the column name without need to type it before every column in select clause?
In this example there are many columns in both tables and many of them have same names so I need to write TABLE2. before every column in select. Is there easier way to add it?
SELECT TABLE1.column1, TABLE2.column2, TABLE2.column3, TABLE2.column4,...
FROM TABLE1, TABLE2
WHERE TABLE1.column1 = TABLE2.column1;
Use table aliases:
SELECT t1.column1,
t2.column2,
t2.column3,
t2.column4 --, ...
FROM TABLE1 t1
INNER JOIN TABLE2 t2
ON ( t1.column1 = t2.column1 );
(And use ANSI join syntax rather than the legacy Oracle comma join syntax as it makes the relationships much clearer)

Not able to Get data from multiple independent tables that have a common column and yet do not depend on each other

I have 8 tables all with equal number of columns and with a common column. I want to fetch data from all tables in a single query.
My table structure is TABLE1, TABLE2, TABLE3, ..... TABLE 8.
that have columns COLUMNA, COLUMNB... COLUMNE and a COMMON_COLUMN
I need to get data with a where clause where COMMON_COLUMN='X'
I will need all columns from all tables.
I used a query that goes like this..
SELECT TABLE1.*, TABLE2.*, TABLE3.*
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON T1.COMMON_COLUMN = T2.COMMON_COLUMN,
LEFT JOIN TABLE3 T3 ON T1.COMMON_COLUMN = T3.COMMON_COLUMN
WHERE T1.COMMON_COLUMN='X' AND T2.COMMON_COLUMN='X' AND T3.COMMON_COLUMN='X'
The above query is not giving any results even if one of the tables do not have any rows. I do not want to use inner join because although the tables have a common column they do not depend on each other and I need data from all tables with a certain common column.
Also, the tables have unequal number of rows.
What am I doing wrong?
correct me if i am wrong - as you do not attach any sample data and desired result
but i assume that you simply need union all tables. You write in the title that tables are independent
SELECT T1.*
FROM TABLE1 T1
WHERE T1.COMMON_COLUMN='X'
UNION ALL
SELECT T2.*
FROM TABLE2 T2
WHERE T2.COMMON_COLUMN='X'
UNION ALL
SELECT T3.*
FROM TABLE3 T3
WHERE T3.COMMON_COLUMN='X'
...

How to find the common values from two different tables having a common column

I have two tables table1 and table2. Both tables have a common column named city.
How do I find all values under city which are in both the tables ?
You can do an inner join on the city column, to find values that exist in both tables.
select
-- Output the city from either table (since it will be the same)
t1.city
from
-- Join table1 and table2 together, on a matching city column
table1 t1 join table2 t2 on (t1.city=t2.city)
group by
-- Only return a single row per city
t1.city
SELECT tbone.desired_column1
tbone.desired_column2,
--other columns from table one
tbtwo.desired_column1,
tbtwo.desired_column2
--other columns from table two
-- Bellow we're stating what this table could be identified as (tbone and tbtwo), so that you don't have to keep typing table name above and bellow. Can be anything, such as A or B or HORSECORRECTINGBATTERY
FROM table1 tbone,
table2 tbtwo
WHERE tbone.city = tbtwo.city
If you don't want to specify which columns to take, just go with
SELECT * FROM ...