Getting common fields of two tables in PL/SQL - sql

Suppose Table A and Table B have various fields. What is an easy way to get the common fields among Table A and Table B ? I want to do an inner join on these tables but I don't know what the common fields are.
Note that this is in PL/SQL. When I table A. or B. I get the list of fields names of each table in a drop down menu. But I would like to get the common fields.

It depends on what do you mean by "common fields".
If you want to get all colums which names are the same in both tables, then you can use this query:
SELECT t1.column_name
FROM user_tab_columns t1
JOIN user_tab_columns t2
ON t1.COLUMN_NAME = t2.COLUMN_NAME
/*
AND t1.DATA_TYPE = t2.DATA_TYPE
AND t1.DATA_LENGTH = t2.DATA_LENGTH
*/
WHERE t1.table_name = 'A'
AND t2.table_name = 'B'
;
Demo: http://sqlfiddle.com/#!4/2b662/1
But if you look at tables in the above demo, you will see that table A has a column named X with datatype VARCHAR2, and table B has also a column named X but of different type INT.
If you want to get all columns that have the same names, the same datatypes and the same length, then uncomment respective conditions in the above query.

Do mean something like this:
TableA has 2 columns: Id and Name.
TableB has 2 columns: Name and PhoneNumber
Query:
SELECT A.Id, A.Name, B.PhoneNumber
FROM TableA A, TableB B
WHERE A.Name = B.Name;
Edit:
If you want know what Columns names there a re from your table I believe you can use DESC TableA. Then you get a list a column names. You can use those to compaire against another list, for example from TableB.

Related

How to distinguish the same field names of two Oracle tables?

I have two different table.
One table has 70 Columes, the other has 80.
I want to display all the Columes of the two tables.
But there are some Columes with the same Columes-name.
EX:
SELECT *
FROM TABLE1 A INNER JOIN
TABLE2 B ON A.ID = B.ID
enter image description here
I want to distinguish which table does the Columes comes from.
I know have to list your column list explicitly and provide aliases to them in the SELECT list.
How can I modify my program?
Is there any other easier way.
Because there are too many field names
You'll have to list your column list explicitly and provide aliases to them in the SELECT list.
SELECT
A.ID AS A_ID,
B.ID AS B_ID
FROM TABLE1 A INNER JOIN
TABLE2 B ON A.ID = B.ID
As a best practice
Never use SELECT * in production queries, always list the required columns explicitly. Why is SELECT * considered harmful?
When you have more than one table referenced in the query (e.g you join two tables), always give an alias to all tables and use that alias when you are referencing any columns of the tables.
You can build a query and let it to compose the query for you. It's not that hard as it seems
I've created tables TEST1 and TEST2 with identical column names and managed database to list me all the columns with prefixes.
select 'select ' txt from dual
union all
select listagg('t1.' || atc.COLUMN_NAME, ', ') within group (order by atc.COLUMN_NAME) || ', '
from all_tab_cols atc
where table_name = 'TEST1'
union all
select listagg('t2.' || atc.COLUMN_NAME, ', ') within group (order by atc.COLUMN_NAME)
from all_tab_cols atc
where table_name = 'TEST2';
The output is
TXT
----------
select
t1.NUM_COL, t1.TEXT_COL,
t2.NUM_COL, t2.TEXT_COL
So you may run the query, copy the output and then add the FROM and WHERE and other parts you need
If the only column names in common are used a JOIN keys, then you can phrase this as:
SELECT *
FROM TABLE1 A INNER JOIN
TABLE2 B
USING (ID);
The ID column only appears once in the result set.
If other columns are common, then you need to use column aliases. Sometimes, it is convenient to use something like this:
SELECT A.*, B.col1 as b_col1, B.col2 as b_col2, . . .
FROM TABLE1 A INNER JOIN
TABLE2 B
USING (ID);
To make this simpler, you can use the metadata tables.

How to Select all columns of a table except one

My code looks like:
CREATE TABLE tableC AS
(SELECT tableA.*,
ST_Intersection (B.geom, A.geom) as geom2 -- generate geom
FROM tableB, tableA
JOIN tableB
ON ST_Intersects (A.geom, b.geom)
WHERE test.id = 2);
Now It is working but I have two columns geom and geom2!
Inside geom column I will have the new geometry based on the intersection. So how can I select tableA except the geom column?
Create the table with all the columns and after that drop the geom column and rename the new one:
CREATE TABLE tableC AS
SELECT
tableA.*,
ST_Intersection (B.geom, A.geom) as geom2 -- generate geom
FROM
tableA inner JOIN tableB ON ST_Intersects (A.geom, b.geom)
WHERE test.id = 2
;
alter table tableC drop column geom;
alter table tableC rename column geom2 to geom;
The only way you would be able to do this would be to generate a dynamic SQL statement based on the columns within the table that excludes those you don't want. Obviously this will be a lot more effort than simply adding in all the column names.
There are also a lot of very good reasons to never include a select * in a production environment, given how picky SQL often is on the number and format of columns that are returned. By using select * you open yourself up to a changing query result in the future that could potentially break things.
If you have a LOT of columns and you simply don't want to manually type them all out, run the query below for your table and then format the result so you can copy/paste into your script:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'

How to print all columns when columns are dynamic and one common column is present?

Lets say i have 2 table A,B .In both the tables (id) is the common column ,and rest columns dynamic.so write a query to print id of "A" and rest all columns .
A(id,name,city),B(id,phone,phone_num). Here I only know "id" column ,rest columns(name,city,phone) are coming dynamically,So i can not use A.name,A.city,B.phone etc .In
select * from A FULL OUTER JOIN B ON A.id = B.id;
is printing id column twice.
If you want to display the id column only once, use the using clause for the join :
SELECT *
FROM tableA a
JOIN tableB b using (id)
The using clause as the effect that the join column(s) are only included once with select *.
If I'm not wrong, you want to display all the column of both the tables and the common column i.e id should come only once.If that's the case use the below query and replace the column name with the column names you want to display.
SELECT a.id,a.column1,a.column2,b.column1,b.column2
FROM tableA a
INNER JOIN tableB b ON a.id=b.id
Demo
create table my_table_1 (id int,c1 int,c2 int,c3 int);
create table my_table_2 (id int,c4 int,c5 int);
select array_to_string(array_agg (table_name || '.' || column_name::text),',')
from information_schema.columns
where table_name in ('my_table_1','my_table_2')
and not (table_name,column_name) = ('my_table_2','id')
my_table_1.id,my_table_1.c1,my_table_1.c2,my_table_1.c3,my_table_2.c4,my_table_2.c5

Include table name in column from select wildcard sql

Is it possible to include table name in the returned column if I use wildcard to select all columns from tables?
To explain it further. Suppose I want to join two tables and both tables have the column name “name” and many other columns. I want to use wildcard to select all columns and not explicitly specifying each column name in the select.
Select *
From
TableA a,
TableB b
Where
a.id = b.id
Instead of seeing two column with same name "name", could I write a sql to return one column name as "a.name" (or TableA.name) and one as "b.name"(or TableB.name) without explicitly putting the column name in select?
I would prefer a solution for mssql but other database could be a reference too.
Thanks!
You can use select a.*, ' ', b.* from T1 a, T2 b to make it more visible where columns from T1 end and columns from T2 begin.
You are basically joining two tables on the ID field, so you will only see one column labeled "ID", not two, because you are asking to see only those records where the ID is the same in table a and table b: they share the same id.
Try ...
SELECT 'TableA' AS 'Table', A.* FROM TableA A
WHERE A.id IN (SELECT id FROM TableB)
UNION
SELECT 'TableB' AS 'Table', B.* FROM TableB B
WHERE B.id IN (SELECT id FROM TableA)
ORDER BY id, [Table]

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?