Compare a column in two different tables - sql

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?

Related

Comparing two tables with different values

I have two tables each in different DB on the same server ...with multiple columns in SQL.
I need to fetch the records where the value of one of the column I use is present in TableA and not in TableB. This column is alphanumeric (BIGINT) in TableA while it is different in TableB it is ShortDescription(varchar(100))
TableB column's value is ONLY numbers same as Table A column but without characters (for example 123) while TableA column value can contain for example "ab123"
Now if value "abc123" in TableA is present in Table A and value "123" is not in Table B, then I should get that record from table A. If 123 is present in Table B for that column, then I shouldn't fetch.
How to do?
You need PATINDEX to find the numbers from TableA.Column1 and then match it with TableB.Column2. Below is the query for you:
Select Column1 FROM TableA WHERE EXISTS (SELECT 1 FROM TableB WHERE SUBSTRING(CAST(Column1 AS VARCHAR), PATINDEX('%[0-9]%', CAST(Column1 AS VARCHAR)), LEN(Column1))=Column2)
I am assuming you do not have a common column between two tables. Otherwise you can use the common column to join and put the PATINDEX statement in WHERE clause.
Try this query. It makes things simple by joining the two tables, then utilizing the WHERE clause. I'll just call the common column between the two tables "Id" for the join:
SELECT * FROM TableA
LEFT JOIN TableB ON TableA.Id = TableB.Id
WHERE TableA.Column1 = "ab123" AND TableB.ShortDescription != "123";

select all columns from 2 tables having duplicated columns

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);

Getting common fields of two tables in PL/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.

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]

Join SQL query to get data from two tables

I'm a newbie, just learning SQL and have this question: I have two tables with the same columns. Some registers are in the two tables but others only are in one of the tables. To illustrate, suppose table A = (1,2,3,4), table B=(3,4,5,6), numbers are registers. I need to select all registers in table B if they are not in table A, that is result=(5,6). What query should I use? Maybe a join. Thanks.
You can either use a NOT IN query like this:
SELECT col from A where col not in (select col from B)
or use an outer join:
select A.col
from A LEFT OUTER JOIN B on A.col=B.col
where B.col is NULL
The first is easier to understand, but the second is easier to use with more tables in the query.
Select register from TABLE_B b
Where not exists (Select register from TABLE_A a where a.register = b.register)
I assumed you have a column named register in TABLE_A and TABLE_B