SQL query to append values not contained in second table - sql

I have table A and table B with different number of columns but both containing a column with IDs. Table A contains more complete list of IDs and table B contains some of the IDs from the table A.
I would like to return resulting table B with original information plus appended IDs that are missing in B but contained in A. For these appended rows, other columns should be blank while column with IDs in B should just contain missing ID values.

Simple solution UNION ALL, with NOT EXISTS:
select b.id, b.c1, ..., b.cn
from b
UNION ALL
select distinct a.id, null, ..., null -- should be same number of columns as in the above select
from a
where not exists (select 1 from b where b.id = a.id)

I think you described left join:
select *
from b left join
a
using (id)

Related

SQL Request for joining a table and printing its column twice in the result table

There are two tables (A, B) in my database and I'm trying to join them and print the same column of table B twice in the result table. The problem is that I need to put in compliance with two different columns of table A, so to join these tables also twice. I tried to join them and put in the select expression required column two times, but in this case result table shows the same relation in every column.
Here is the result I want, where the id column of table B includes id_1 and id_2 from table A and the output names correspond to ids from table A.
Table A: (integer id_1, integer id_2)
Table B: (integer id, varchar name)
Result Table: (id_1, name, id_2, name)
You need inner join with tableB twice as follows:
select a.id_1, b1.name, a.id_2, b2.name
from tableA a join tableB b1 on a.id_1 = b1.id
join tableB b2 on a.id_2 = b2.id

Postgres SQL SELECT data from the table the entry exists with ID

I have the following scenario.I have 3 tables with the following structure.
TABLE A
-entry_id (PRIMARY KEY, INTEGER)
TABLE B
-entry_id (FOREIGN_KEY -> TABLE A)
-content (TEXT)
TABLE C
-entry_id (FOREIGN_KEY -> TABLE A)
-content (INTEGER)
I want to retrive the content cell value from either table B or table C. The value can be in just one of the table. So it is either table B or C witch have an entry with a given entry_id.
PS. Sorry if duplicate did not manage to find anything to match what i need.
If I correctly understand, you need something like:
select entry_id, content::text from TABLEB where entry_id = ?
union all
select entry_id, content::text from TABLEC where entry_id = ?
union all
select entry_id, content::text from TABLED where entry_id = ?
If it can only exist in one table at a time, use a union
select a1.entry_id, b2.content
from TableA a1
inner join TableB b2
on a1.entry_id = b2.entry_id
union -- This removes any duplicates. Use UNION ALL to show duplicates
select a1.entry_id, c3.content::text
from TableA a1
inner join TableC c3
on a1.entry_id = c3.entry_id

Issues with SQL Select utilizing Except and UNION All

Select *
From (
Select a
Except
Select b
) x
UNION ALL
Select *
From (
Select b
Except
Select a
) y
This sql statement returns an extremely wrong amount of data. If Select a returns a million, how does this entire statement return 100,000? In this instance, Select b contains mutually exclusive data, so there should be no elimination due to the except.
As already stated in the comment, EXCEPT does an implicit DISTINCT, according to this and the ALL in your UNION ALL cannot re-create the duplicates. Hence you cannot use your approach if you want to keep duplicates.
As you want to get the data that is contained in exactly one of the tables a and b, but not in both, a more efficient way to achieve that would be the following (I am just assuming the tables have columns id and c where id is the primary key, as you did not state any column names):
SELECT CASE WHEN a.id IS NULL THEN 'from b' ELSE 'from a' END as source_table
,coalesce(a.id, b.id) as id
,coalesce(a.c, b.c) as c
FROM a
FULL OUTER JOIN b ON a.id = b.id AND a.c = b.c -- use all columns of both tables here!
WHERE a.id IS NULL OR b.id IS NULL
This makes use of a FULL OUTER JOIN, excluding the matching records via the WHERE conditions, as the primary key cannot be null except if it comes from the OUTER side.
If your tables do not have primary keys - which is bad practice anyway - you would have to check across all columns for NULL, not just the one primary key column.
And if you have records completely consisting of NULLs, this method would not work.
Then you could use an approach similar to your original one, just using
SELECT ...
FROM a
WHERE NOT EXISTS (SELECT 1 FROM b WHERE <join by all columns>)
UNION ALL
SELECT ...
FROM b
WHERE NOT EXISTS (SELECT 1 FROM a WHERE <join by all columns>)
If you're trying to get any data that is in one table and not in the other regardless of which table, I would try something like the following:
select id, 'table a data not in b' from a where id not in (select id from b)
union
select id, 'table b data not in a' from b where id not in (select id from a)

Singe SQL Query to retrieve fields from 2 different table.

Trying to explain my Question:
Say, Table A contains ID,Number,Location.
Table B contains Option 1,Option 2.
I want to write a single query that would select ID, Number & Option 1 from the Table A and Table B.
(At present I am doing something like:
SELECT ID FROM [A]
SELECT Option 1 FROM [B]
)
You do this:
SELECT A.ID, A.Number, B.Option1
FROM TableA as A, TableB as B
WHERE A.id = B.id;
This Part sets an alias for the table so that you don't have type in the full table name all the time:
TableA as A
TableB as B
This part is the relationship between table A and B.
WHERE A.id = B.id;
Consider reading SQL table relationships http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/

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]