MySQL JOIN Complexity - mysql

I am using MySQL 5.5 and have two tables T1(ID, NAME) and T2(ID, MARKS), following is the data in the table.
T1's data
ID NAME
1 A
2 B
3 C
T2's data
ID MARKS
1 40
5 60
AND I want following resultset
ID NAME MARKS
1 A 40
2 B 0
3 C 0
What query we can write to accomplish above resultset?

LEFT JOIN the two tables:
SELECT
t1.id,
t1.name,
IFNULL(t2.Marks, 0) AS Marks
FROM T1
LEFT JOIN T2 ON t1.ID = t2.ID;
SQL Fiddle Demo

You should be using a LEFT JOIN clause for this kind of result :-)
Your query will look like this :
SELECT
ID,
NAME,
COALESCE(MARKS, 0) AS MARKS
FROM T1
LEFT JOIN T2 USING(ID)
Note that I'm using the USING clause for a more semantic and compact syntax than the ON clause.

In this case, you need to use LEFT JOIN to join both tables since it is not guaranteed that ID on table T1 will exist on table T2. LEFT JOIN retrieves all rows that are found on the lefthand side table whether it has a matching record or not on the righthand side table.
The use of COALESCE in the statement is to convert NULL values into 0. The reason why NULL exist on the values of the column is because some records on table T1 doesn't have a matching record on table T2 causing the column T2.Marks to be null.
SELECT a.ID, a.Name, COALESCE(b.Marks, 0) Marks
FROM T1 a
LEFT JOIN T2 b
ON a.ID = b.ID
SQLFiddle Demo
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins

Related

I have two tables,first table T1 contains id and name and the name contains 5 values

I have two tables,first table T1 contains id and name and the name contains 5 values.And the second table T2 contains id and amonut_paid and the amount paid column contains 3 values.I want to display the all names from the table T1 and the amount_paid in sql
Do a LEFT JOIN and then use IFNULL for replacing the NULL with 0
SELECT T1.name,IFNULL(T2.amount_paid,0)
FROM T1
LEFT JOIN T2 ON T1.id=T2.id
Note: Instead of IFNULL you can also use COALESCE as strawberry suggested.
Use left join here, Because your t1 table contains 5 names and you want to display all name from t1 table. And your t2 table contains only three entry(amount_paid).
Select t1.name, t2.amount_paid
from t1
left join t2 on t1.id = t2.id
Use an LEFT JOIN on both these tables T1 and T2, so that it shows only the rows where a NAME against AMOUNT_PAID exists (INNER JOIN provides the rows available in both these tables)
SELECT T1.name, T2.amount_paid
FROM T1
LEFT JOIN T2 ON T1.id = T2.id
--RETURNS 3 rows as per your example
Use an OUTER JOIN on both these tables T1 and T2, so that it shows all the rows irrespective of the values being present in these tables.
RETURNS 5 rows as per your example

SQL Inner Joins Giving Incorrect Count Values in MS ACCESS

I am trying to use join to connect multiple tables in MS Access to get count values. But I don;t know it gives wrong count values. If I try to join them individually, then it gives me correct count values.
I have 3 Tables. Table 2 and Table 3 are independent and are connected to Table 1. Test 2 and test 3 are basically text values and I want to count the rows .
Table1(ID1(Primary Key),Name)
Table2(ID2(Primary Key), ID1(Foreign Key), Test2)
Table3 (ID3(Orimary Key), ID1(Foreign Key), Test3)
The Query that I get from MS Access is given below:
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2, Count(Table3.Test3) AS CountOfTest3
FROM (Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID2)
INNER JOIN Table3 ON Table1. ID1 = Table3.ID3
GROUP BY Table1.ID1;
But this gives me wrong Count Values.
Can someone please help me.
Thanks.
When I use it individually, it gives me correct count value:
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2
FROM Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID1
GROUP BY Table1.ID1;
SELECT Table1. ID1, Count(Table3.Test3) AS CountOfTest3
FROM Table1 INNER JOIN Table3 ON Table1.ID1 = Table3.ID1
GROUP BY Table1.ID1;
But when I try to join Table1, Table2 and Table 3 in MS Acces, it gives me incorrect count values.
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2, Count(Table3.Test3) AS CountOfTest3 FROM (Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID1) INNER JOIN Table3 ON Table1. ID1 = Table3.ID1 GROUP BY Table1.ID1
As per my understanding it is taking the count value of 1st query in the parenthesis and multiplying it with the count values of the other inner join.
I have tried many things but don't know what to do. Access adds parenthesis for some reason.
If I can assume test2 and test 3 are unique to each record (perhaps it would be better to count the PK?)
SELECT Table1.ID1
, Count(distinct Table2.Test2) AS CountOfTest2
, Count(distinct Table3.Test3) AS CountOfTest3
FROM Table1
INNER JOIN Table2
ON Table1.ID1 = Table2.ID2
INNER JOIN Table3
ON Table1.ID1 = Table3.ID3
GROUP BY Table1.ID1;
Or you may have to get the counts before the joins though the use of inline views. You could use window functions if MSSQL SERVER but Access needs the inline views.
SELECT A.ID1
, B.CountOfTest2
, C.CountOfTest3
FROM Table1 A
INNER JOIN (SELECT Table2.ID2, count(table2.test) as CountOfTest2
FROM Table2
GROUP BY Table2.id) B
ON Table1.ID1 = B.ID2
INNER JOIN (SELECT Table3.id, count(table3.test3) as CountOfTest3
FROM Table3
GROUP BY Table3.id) C
ON B.ID1 = C.ID3
GROUP BY A.ID1;
Yup i had the same problem when i was trying to do this. just use the double sql function to counteract the html and you should be good. once the query has been doubled it will react like a C++ quota statement. If this fails you can always just quantify the source fields to adhear to the table restrictions. its actually a piece of cake i hope this helped.

Oracle Select Query based on Column value

I have two tables lets say
Table A
columns id , name address
Table B
columns id , age, import_date
The Table B id is a reference key of Table A.
Now I want to return results from A & B but if the record is not in B I still want to see the record so for this I use left outer join
Select * from A a left join B b
on a.id = b.id
Now even I don't have record in B I still get the record.
Table B may contain duplicate ids but unique import_date.
Now I want to results in a way that if there is duplicate id in table B then I want to get the records only where import_date is as of today.
I still want to get the records for ids which are not there but if the ID is there in table B then I want to apply above condition.
I hope someone can help me with this.
Sample data
Table A
01|John|London
02|Matt|Glasgow
03|Rodger|Paris
Table B
02|22|31-AUG-2015
02|21|30-AUG-2015
02|23|29-AUG-2015
The query will return
01|John|London|null|null|null
02|Matt|Glasgow|22|31-Aug-2015
03|Rodger|Paris|null|null
You almost got the solution. Just add one more condition like below
Select a.id,a.name,a.address,b.age,b.import_date
from tablea a left join tableb b
on a.id=b.id and b.import_date=trunc(sysdate)
order by a.id;---This line optional
Check the DEMO HERE
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id UNION
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id
GROUP BY t2.import_date
HAVING t2.import_date=CURDATE();

SQL Count + Left join + Group by ... Missing rows

Trying to list all what's in table 1 and records under it in table 2
Table one each row has an id , and each row in table 2 has idontable1
select table1.*, count(table2.idintable1)as total
from table1
left join table2 on table1.id=table2.idintable1
WHERE table1.deleted='0' AND table2.deleted=0
group by
table2.idintable1
My current problem is rows from table1 with 0 records in table2 are not displayed
I want them to be displayed
The query that you want is:
select t1.*, count(t2.idintable1) as total
from table1 t1 left join
table2 t2
on t1.id = t1.idintable1 and t2.deleted = 0
where t1.deleted = 0
group by t1.id;
Here are the changes:
The condition on t2.deleted was moved to the on clause. Otherwise, this turns the outer join into an inner join.
The condition on t1.deleted remains in the where clause, because presumably you really do want this as a filter condition.
The group by clause is based on t1.id, because t2.idintable1 will be NULL when there are no matches. Just using t1.id is fine, assuming that id is unique (or a primary key) in table1.
The table aliases are not strictly necessary, but they make queries easier to write and to read.
You should GROUP BY table1.id.
The LEFT JOIN ensures all the rows from table1 appear in the result set. Those that do not have a pair in table2 will appear with NULL in field table2.idintable1. Because of that your original GROUP BY clause produces a single row for all the rows from table1 that do not appear in table2 (instead of one row for each row of table1).
You have fallen into mysql's non-standard group by support trap.
Change your group by to list all columns of table 1:
group by table1.id, table1.name, etc
or list the column positions of all table1 columns in the select:
group by 1, 2, 3, 4, etc
Or use a subquery to get the count vs the id, and join table1 to that.

Left Join in Mysql?

This is my first Table.
Then the second one is
Now I am trying to do Left join like this
SELECT t1.StackID FROM t1 LEFT JOIN t2 on t1.StackID=t2.StackID
Output
I am confused here, Is this the correct output? Is not this supposed to return only the 5 rows which is present in left side table.
It's correct output. You are doing LEFT JOIN, so for every record in LEFT table DBMS will 'concatenate' the corresponding RIGHT table record(-s) (and NULL, if there's no corresponding record for the JOIN condition; also remember, that if there are more that 1 corresponding record - all will be joined - this issue is why you're getting not only 5 records from 1-st table).
The thing you're trying to achieve should be done by either DISTINCT modifier, i.e.
SELECT DISTINCT t1.StackID FROM t1 LEFT JOIN t2 ON t1.StackID=t2.StackID;
More about JOIN in SQL you can read here.
There is a simple example of left join:-
SELECT * FROM a JOIN b LEFT JOIN c ON (c.key=a.key) LEFT JOIN d ON (d.key=a.key)
WHERE b.key=d.key;
These extra repeated values of StackId are coming from the join, just use DISTINCT to get those only 5 values:
SELECT DISTINCT t1.StackID
FROM t1
LEFT JOIN t2 on t1.StackID=t2.StackID;
SQL Fiddle Demo
Yes, this is the expected output.
To get the distinct IDs, use DISTINCT -
SELECT DISTINCT t1.StackID
FROM t1
LEFT JOIN t2
ON t1.StackID=t2.StackID
When you left joined your t2 table with t1 based on stackID, then the database will join every row of t2 with the every row of t1 which has the same stackID value. Since 1 appears in six rows in the t2 table as a stackID value, all of them are joined with the first row of t1. Similar thing is true for all other values.
To give yourself a little more understanding of how a left join works try this:
SELECT t1.StackID, t2.StackID FROM t1 LEFT JOIN t2 on t1.StackID=t2.StackID
basically a left join returns all the rows from the left table plus matching ones from the right.