avoid null tables in left join - sql

If I join two tables together with left join and one of the tables is completely empty, I get a bunch of empty columns in the joined table.
Here is a fiddle demonstrating what I mean.
I would like the resulting joined table to not contain all those null columns

The number of columns that a query returns is fixed. It cannot change depending on whether a table is empty or not. So the answer is nope.

Related

Why i am not getting data after applying join query in table that doesnt have data?

I have applied a join query on two tables.
One table has data but the other one does not.
When i try to join table that does not have data, it is not returning anything.
I expected data to come at least from the table which has data.
$allTours = DB::table("virtual_tours")
->join('destinations','virtual_tours.destination_id','=','destinations.id')
->join('virtual_tour_comments','virtual_tours.id','=','virtual_tour_comments.virtual_tour_id')
->get();
Virtual tour comments doensn't have data
You're using a regular join, which becomes an inner join. Meaning data without a match on your conditions gets filtered out. Inner joins are meant to be used to find matches.
Join explanations
What you want is a left join, where data gets always joined but columns simply become NULL if they're not present and you'll get all results.
Since you're using laravel, here's the reference for it:
Laravel Joins

2 SQL Queries from 2 Different Databases, Create 1 Table merging Columns

Suppose that I have two queries from two databases as below.
LIST 1 from [Alphabet] table
ABCD
A
B
C
D
LIST 2 from [Integers] table
Numbers
101
201
301
401
I would like to merge them so that in Excel sheet, I want to see that alphabet table is in columns A and integers table is in column B.
Here my suggestion:
Create Table merged ( Select [ABCD] from [Alphabet] join with [Numbers] from [Integers])
How can I improve the quality to work?
And should the row numbers be equal in both tables? Say, 27 letters and 27 integers, or would work with 27 letters and integers [1,20]?
This Is My First Answer and i hope it would help ...Back to your question
How can I improve the quality to work?
You Must use constraints to enforce rules for the data in your tables.
You need two columns of the same type, one on each table [Alphabet table] and [Integers table], to JOIN on.
Whether they're primary and foreign keys or not doesn't matter.
And should the row numbers be equal in both tables?
the answer is No
Depending on what joins you will Use !
SQL joins are used to query data from two or more tables, based on a relationship between the corresponding rows in these tables.
• INNER JOIN: Returns all corresponding rows from multiple tables.
• LEFT OUTER JOIN: Returns all rows from the left table, corresponding rows in the right table & if there are no matches, returns NULL.
• RIGHT OUTER JOIN: Returns all rows from the right table, corresponding rows in the left table, & if there are no matches, returns NULL.

SQL join fails on temp tables

I'm having trouble joining two temp tables where all columns are varchar(MAX).
I'm trying to join on columns that both contains value 'SV-001', when I change value to 'SV-0' there are no problems, but when I add 1 more '0' it fails?
Values on both tables are collected from different standard tables and I have tested the results before I join them - even excel can compare values, so I'm sure values are identical.
I'm joining them like this:
SELECT *
FROM #Speedwell_setup
JOIN #Speedwell_data ON #Speedwell_setup.Productcode = #Speedwell_data.Product1
All I get is a empty result, no errors or anything, I hope that you can help me out here.
Thanks in advance

Why am I loosing table data from adding additional join statement in SQL Server?

So I have table X with let's say 50 rows of data in it and it is being joined to another table with only 2 rows in it. When I "normal" JOIN them together only the overlapping data will show, i.e. the two rows of data that are found in the larger collection of 50. In my case I want all 50 rows to persist so I use a LEFT JOIN and I am returned all 50 rows like planned.
Now I want to start adding and joining in other tables to get additional data about these rows and have them display togehter, whenever there is no data, I am fine with getting null. Now I'm adding a new JOIN and it's only going to find data for those two rows and not for the other 48, which is perfectly fine and I would like NULL to be displayed where no match is found. My problem is that rather than doing that, displaying null, it is instead removing the 48 rows will partial null data in their columns entirely and only showing the two that match, WHY?
I can provide code if needed, I thought this may be easier to understand.
If you inner join the third table, there are only 2 rows that can match (I assume you don't match on nulls). Only 2 rows from the second table have non-null values in them, so only those 2 can produce a result if you do an inner join with the third table.
You will need another left join in this case. This will return all the rows from the second table, including those that are all null (as the result of the first left join).

Compare rows in different table having same columns

I have 2 tables tbl_A and tbl_A_temp. Both the table have the same schema. their primary key differ since they are identity columns. Is there a way i can compare the two rows in these two table and get to know if they differ.I will be inserting data from tbl_A_temp to tbl_A, i need this compare just to make sure that I am not inserting any duplicate data in the main tables.
Regards,
Amit
I think this should work for you. Basically, since you don't have a primary key to join on, you'll need to perform a LEFT JOIN on all your other fields. If any are different, then the NULL check will be true:
SELECT t.*
FROM tbl_A_temp t
LEFT JOIN tbl_A a ON
t.field1=a.field1 AND t.field2=a.field2 AND ...
WHERE a.field1 IS NULL
I've also seen others use CHECKSUM, but have run into issues myself with it returning false positives.