SQL STATEMENT QUERY - sql

I have table1Name with data populated and table2 with no data populated.
select * from [database1Name].dbo.table1Name
join [database1Name].dbo.table2Name
on [database1Name].dbo.table1Name.fieldName like value;
After running the above sql statement it joins the tables but does not show any populated data from the table 'table1Name'.
Why does this happen?

Using JOIN which is an INNER JOIN means that it will get you only data where the condition matches. So if the second table has not data, then the condition is never met, so you get no data in return.
In your case you need a LEFT JOIN. This will get all the rows from the left table (table1Name in your case) and the corresponding values from the right table when the condition is met.
SELECT *
FROM [database1Name].dbo.table1Name
LEFT JOIN [database1Name].dbo.table2Name
ON [database1Name].dbo.table1Name.fieldName like [database1Name].dbo.table2Name.fieldName;
Just to mention that using joins mean that you might get multiple times a single row from a specific table. For instance since you have a LIKE condition, if fieldName of Table 1 matches fieldName in 2 rows from Table 2 then you will get two rows containing the same row from Table 1 and the two rows from Table 2:
Example:
Table1
FieldName
1
2
Table2
FieldName OtherField
1 1
1 2
Result of LEFT JOIN
T1FieldName T2FieldName T2OtherField
1 1 1
1 1 2
2 NULL NULL

Related

Upon doing Table t1 inner join table t2, all Boolean values are retuned as TRUE. How do I fix it?

SELECT e.*
FROM Table1 m
INNER JOIN Table2 e ON m.table_id = e.table_id
WHERE m.format_type_id = 3
ORDER BY e.rank desc;
The above Query is a Join between 2 tables.
Expected ResultSet : All fields of Table 2(hence e.*)
Expected:
Table 2 ID
(boolean) header
1
0
2
1
I get the results when I:
remove order by ,the values are coming in properly
or I mention all column names like e.column1, e.column2...
The problem is that all Boolean column values are only coming in as true. The type of the column is BIT in the DB
Output:
Table 2 ID
(boolean) header
1
1
2
1
Have tried this as well: How map a bit type in Mysql to hibernate?

Duplicate rows in left join

I have 2 tables. There are about 100000 of null in one column, other values are integer, total values are about 200000. Another table has only the integer value. When I use the left join on this column, it gave me a lot of duplicates rows. Is it ok to use left join here?
Table 1:
Column 1
2
3
5
null
null
Table 2:
Column 1
1
2
3
so on
Your example is really odd. Why would anyone have null values in an ID field? But anyway.
If you need fields from table 2 in the resultset as you say above then you must use an INNER JOIN not a LEFT JOIN
Something like:
SELECT DISTINCT a.id, a.name, b.someOtherField
FROM Table1 a
INNER JOIN Table2 b ON a.id = b.id
Please note: Since only the ID field of table 1 has null values there will be no records selected from table 1 with id IS NULL because they have no equivalent in table 2. Adding the DISTINCT keyword helps in case this query would still produce duplicates.

how to fetch distinct records from 3 tables which should contain all the values from 1 table

I have query which should fetch all the matching records from 3 tables. other records which are not common in three tables, it should union the records from first table..
EX:
select a.x,a.y,a.z
from table1 a,table2 b,table3 c
where a.x=b.x
and b.x=c.x;
above query will fetch common records among all 3 tables.
I need to add the records to my result set which are not present in table2 or table3.
Records should come like below:
1 abc acd
2 xyz xzy
3 pqr prq
4 null null -- incase 4 is not present in either table2 or table3
You need to use an outer join instead:
select a.x,a.y,a.z
from table1 a
left join table2 b on a.x=b.x
left join table3 c on b.x=c.x;
A Visual Explanation of SQL Joins
You state
other records which are not common in three tables, it should union
the records from first table.
Then state
I need to add the records to my result set which are not present in
table2 or table3.
For the first case use a full outer join which will return all rows from all tables. For the second case use left joins which will leave out rows that have no match to table1.

SQL Join query to return matching and non-matching record

I am working on a SQL Syntax to write a Join query.
I couldnt get the expected output, i request expert to help me.
Table: Table1
ScriptNumber Date Filled RefillsLeft
100 01/02/2014 1
200 01/03/2014 0
300 01/22/2014 3
Table : Table 2
ScriptNumber Date Filled RefillsLeft
100 02/02/2014 0
Expected output
ScriptNumber Date Filled RefillsLeft
100 02/02/2014 0
300 null null
SQL Statement
SELECT Table_2.ScriptNumber
,Table_2.DateFilled
,Table_2.RefillsLeft
FROM Table_1
LEFT JOIN Table_2
ON Table_1.ScriptNumber = Table_2.ScriptNumber
Your problem is coming from including columns in SELECT statement from table_2 that do not have values for rows that exists in table_1. You need to change SELECT Table_2.ScriptNumber to SELECT Table_1.ScriptNumber
As future reference make sure you always select all relevant columns from LEFT tables and only columns you need from RIGHT table. Otherwise you end up with less NULL rows instead of having data that is present in LEFT table.
A left join could be useful here to get the records you want in Table_1 and any relevant details that may exist in Table_2
select Table_1.ScriptNumber
, Table_2.DateFilled
, Table_2.RefillsLeft
from Table_1
left join Table_2 on Table_1.ScriptNumber = Table_2.ScriptNumber
where Table_1.RefillsLeft > 0
Such a description is helpful in your questions, too.

left join on MS SQL 2008 R2

I'm trying to left join two tables. Table A contains unique 100 records with field_a_1, field_a_2, field_a_3. The combination of field_a_1 and field_a_2 is unique.
Table B has multi-million records with multiple fields. field_b_1 is same as field_a_1 and field_b_2 is same as field_a_2.
I join the two tables together like this:
select a.*, b.*
from a
left join b
on field_a_1 = field_b_1
and field_a_2 = field_b_2
Instead of getting 100 records, I get multi-million records. Why is this?
Because table B has multiple rows for each table A entry.
For example:
TableA (ID)
1
2
3
TableB (ID, data)
1 hello
1 world
1 foo
1 bar
2 data
2 words
2 more
3 words
3 boring
If you left join from TableA to TableB, you will get a row for every TableB record that matches a TableA record - ie. all of them.
Can you explain what results you are looking for?
Because a left join returns all of the rows from the first table + all of the matching rows from the second table. Which of the millions of matching rows did you expect to get?
Left join or inner join don't really make a difference. A JOIN will return all rows that match the join condition. So if table b has millions of rows that match the JOIN criteria, then all the rows will be returned.
Depending on what you wish to accomplish you should consider using the DISTINCT keyword or GROUP BY to perform aggregate functions.