SQL Join tables with empty values - sql

I have 2 tables
Lets say Table1 and Table2
They both have one shared value(id)
What I'm looking for is whether there is any function to combine them both based on that key, however if table2 has more elements, i want columns of table1 to be empty, and if table1 has more elements, table 2 columns to be empty
I tried a lot of different joins, but most of the time I end up with a lot of duplicate values as it tries to fill in both sides.
Tried Full outer join, Full join, etc

You are looking for full join:
select t1.*, t2.*
from t1 full join
t2
on t1.id = t2.id;

The above code from Gordon is right. However, since you have not specified the database and its version, I will post an alternate version for MySQL, which should also work for other databases.
Without duplicates:
SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION
SELECT * FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id
With duplicates:
SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.id = Table2.id
UNION ALL
SELECT * FROM Table1
RIGHT JOIN Table2 ON Table1.id = Table2.id

Related

SQL Select with join query works but delete does not work

I have two tables Table1 and Table2. I compared thes tables using the query below. SELECT
Select Table1.ID
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ID =Table2.ID
WHERE Table2.ID IS NULL
and got 1508 records that exist in Table1 but not in Table2.
Now I wanted to delete these records in Table2. This is the code I used below
DELETE Table1.*
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ID =Table2.ID
WHERE Table2.ID IS NULL
This is the error I am get Could not delete from specified tables. I realize something is wrong with my sql, but where. I thought this DELETE was specifying a table not tables.
Try this:
DELETE FROM Table1
WHERE ID NOT IN
(
SELECT ID
FROM table2
)

query in sql server 2005

there are two tables ...know i need
1st condition:
all the records in table
2 nd condition:
In table2 i need only records which have data
...i want one query for the aove two conditions...
SELECT
*
FROM Table1 t1
INNER JOIN Table2 t2 on t1.PK = t2.FK
This will return all rows in table1 that have at least one corresponding row in table2
But if you want all rows from t1 no matter what then this may be what you want
SELECT
*
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.PK = t2.FK
Finally, As I dont know the structure in place perhaps table1 and table2 have similar structures. If this is true perhaps you may want a union of the two
SELECT
*
FROM Table1 t1
UNION ALL
SELECT
*
FROM Table2 t2

Joining empty table to return all rows

I have a table (Table1) which has a composite primary key(Column1 + Column2). I am using it as a foreign key in another table (Table2).
Now I want to a SELECT statement to select all records from Table1 and Table2.
But its returning me 0 rows, because table2 is Empty. I want all records from table1 and if it does not exist in table2, value of Columns in Table2 should be null.
I know, I only need to Join it. But I am not getting it right.
Thanks
SELECT * FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id = T2.FK
FK is your foreign key on the second table.
A Left Join will return all rows from table1 even if they don't exist in table2.
You need an outer join
SELECT *
FROM table1
LEFT OUTER JOIN table2
ON table1.column1 = table2.column1
AND table1.column2 = table2.column2
Left means preserve all rows from the left (first) table in the query.
You need a LEFT JOIN
SELECT Table1.*, Table2.*
FROM Table1
LEFT JOIN Table2 ON Table1.Column1 = Table2.Column2
Try that out.
Use LEFT JOIN for join you tables. See SQL SERVER JOINS to understand the concept.

Need assistance with Left Outer join?

SELECT ID
(
Select ID from Table1
where Table1.ID=#ID
)T1
Left Outer join
(
Select top 1 Table2.ID from Table2 join Table3 on table3.ID=Table2.ID
order by Table2.ID DESC
)T2 on T2.ID=T1.ID
This is just an example of the actual stored procedure I have with me, the problem which I am facing is that I'm unable to retrieve the values from T2 it just returns as NULL but when I change it Top 5 i am able to retrieve the values. Is this Join correct, is it necessary to have where part inside left outer join in order to retrieve the values?
If you are using TOP you need to decide on all selects (all three) how your data should be ordered so you can have some control on which values you are getting back, and maybe also being more specific on what you filter.
A couple of observations.
There's no ORDER BY clause for your T1 SELECT, so how do you know which TOP 250 is being returned?
If T2 returns NULL, then there is no match between T1 and T2, possibly due to my first point?
What exactly are you trying to accomplish?
You could try something like this:
SELECT
TOP 250 Table1.ID
FROM
Table1
LEFT OUTER JOIN
Table2 ON Table2.ID = Table1.ID
LEFT OUTER JOIN
Table3 ON Table3.ID = Table2.ID
WHERE
Table1.ID = #ID
ORDER BY
Table1.ID DESC

Selecting records based on two other tables

I have a query with inner join to another table, with this I want also include records which are contained in another column.
Example:
select name, address from table1
inner join table2 on table1.id = table2.id
With this, I want to also include rows which are having table1.recno = (1,2,4).
How could I write query for that?
One option I know is to use the IN keyword instead of the first table join. But our client doesn't want to use the IN keyword.
Use a left join and then use the WHERE clause to filter out the rows that you need.
select name, address
from table1
left join table2 on table1.id = table2.id
where
table2.id IS NOT NULL OR table1.ID In (1,2,4)
Or if you want to avoid an innocuous IN for silly reasons, use:
select name, address
from table1
left join table2 on table1.id = table2.id
where
table2.id IS NOT NULL
OR table1.ID = 1
OR table1.ID = 2
OR table1.ID = 4