S
S
sorry_i_noob2018-02-09 02:47:44
SQL
sorry_i_noob, 2018-02-09 02:47:44

I can not understand what was displayed as a result of the SQL query (FULL JOIN). Why are there so many entries and nowhere NULL?

Hello.
There is such a task:
Assuming that the receipt and expenditure of money at each reception point is recorded no more than once a day [i.e. primary key (point, date)], write a query with output data (point, date, income, expense). Use tables Income_o and Outcome_o.
Database schema:
income.gif
I was able to solve the problem (this is a problem with sql-ex.ru), there is no need to write a solution. But during the solution, I sketched the following code:

SELECT Income_o.point, Income_o.date, inc, out
FROM Income_o
FULL JOIN Outcome_o ON Income_o.point = Outcome_o.point AND Income_o.date = Income_o.date

As a result, he displayed twice as many records as needed. And there was no NULL anywhere. I can't understand why this happened? Help me understand (I need this to better understand how JOIN works).
The result of my code
1		2001-03-22 00:00:00.000		15000.0000	1221.0000
1		2001-03-22 00:00:00.000		15000.0000	15348.0000
1		2001-03-22 00:00:00.000		15000.0000	2004.0000
1		2001-03-22 00:00:00.000		15000.0000	2075.0000
1		2001-03-22 00:00:00.000		15000.0000	2530.0000
1		2001-03-22 00:00:00.000		15000.0000	3110.0000
1		2001-03-22 00:00:00.000		15000.0000	3195.0400
1		2001-03-22 00:00:00.000		15000.0000	3663.0000
1		2001-03-22 00:00:00.000		15000.0000	4490.0000
1		2001-03-23 00:00:00.000		15000.0000	1221.0000
1		2001-03-23 00:00:00.000		15000.0000	15348.0000
1		2001-03-23 00:00:00.000		15000.0000	2004.0000
1		2001-03-23 00:00:00.000		15000.0000	2075.0000
1		2001-03-23 00:00:00.000		15000.0000	2530.0000
1		2001-03-23 00:00:00.000		15000.0000	3110.0000
1		2001-03-23 00:00:00.000		15000.0000	3195.0400
1		2001-03-23 00:00:00.000		15000.0000	3663.0000
1		2001-03-23 00:00:00.000		15000.0000	4490.0000
1		2001-03-24 00:00:00.000		3400.0000	1221.0000
1		2001-03-24 00:00:00.000		3400.0000	15348.0000
1		2001-03-24 00:00:00.000		3400.0000	2004.0000
1		2001-03-24 00:00:00.000		3400.0000	2075.0000
1		2001-03-24 00:00:00.000		3400.0000	2530.0000
1		2001-03-24 00:00:00.000		3400.0000	3110.0000
1		2001-03-24 00:00:00.000		3400.0000	3195.0400
1		2001-03-24 00:00:00.000		3400.0000	3663.0000
1		2001-03-24 00:00:00.000		3400.0000	4490.0000
1		2001-04-13 00:00:00.000		5000.0000	1221.0000
1		2001-04-13 00:00:00.000		5000.0000	15348.0000
1		2001-04-13 00:00:00.000		5000.0000	2004.0000
1		2001-04-13 00:00:00.000		5000.0000	2075.0000
1		2001-04-13 00:00:00.000		5000.0000	2530.0000
1		2001-04-13 00:00:00.000		5000.0000	3110.0000
1		2001-04-13 00:00:00.000		5000.0000	3195.0400
1		2001-04-13 00:00:00.000		5000.0000	3663.0000
1		2001-04-13 00:00:00.000		5000.0000	4490.0000
1		2001-05-11 00:00:00.000		4500.0000	1221.0000
1		2001-05-11 00:00:00.000		4500.0000	15348.0000
1		2001-05-11 00:00:00.000		4500.0000	2004.0000
1		2001-05-11 00:00:00.000		4500.0000	2075.0000
1		2001-05-11 00:00:00.000		4500.0000	2530.0000
1		2001-05-11 00:00:00.000		4500.0000	3110.0000
1		2001-05-11 00:00:00.000		4500.0000	3195.0400
1		2001-05-11 00:00:00.000		4500.0000	3663.0000
1		2001-05-11 00:00:00.000		4500.0000	4490.0000
2		2001-03-22 00:00:00.000		10000.0000	1440.0000
2		2001-03-22 00:00:00.000		10000.0000	2040.0000
2		2001-03-22 00:00:00.000		10000.0000	7848.0000
2		2001-03-24 00:00:00.000		1500.0000	1440.0000
2		2001-03-24 00:00:00.000		1500.0000	2040.0000
2		2001-03-24 00:00:00.000		1500.0000	7848.0000
3		2001-09-13 00:00:00.000		11500.0000	1500.0000
3		2001-09-13 00:00:00.000		11500.0000	2150.0000
3		2001-09-13 00:00:00.000		11500.0000	2300.0000
3		2001-10-02 00:00:00.000		18000.0000	1500.0000
3		2001-10-02 00:00:00.000		18000.0000	2150.0000
3		2001-10-02 00:00:00.000		18000.0000	2300.0000

If so, here's the correct solution:
spoiler
SELECT Outcome_o.point, Outcome_o.date, inc, out
FROM Outcome_o
LEFT JOIN Income_o ON Income_o.point = Outcome_o.point AND Income_o.date = Outcome_o.date
UNION
SELECT Income_o.point, Income_o.date, inc, out
FROM Income_o
LEFT JOIN Outcome_o ON Outcome_o.point = Income_o.point AND Outcome_o.date = Income_o.date
The result of the correct solution
1		2001-03-14 00:00:00.000		NULL	        15348.0000
1		2001-03-22 00:00:00.000		15000.0000	NULL
1		2001-03-23 00:00:00.000		15000.0000	NULL
1		2001-03-24 00:00:00.000		3400.0000	3663.0000
1		2001-03-26 00:00:00.000		NULL	        1221.0000
1		2001-03-28 00:00:00.000		NULL	        2075.0000
1		2001-03-29 00:00:00.000		NULL	        2004.0000
1		2001-04-11 00:00:00.000		NULL	        3195.0400
1		2001-04-13 00:00:00.000		5000.0000	4490.0000
1		2001-04-27 00:00:00.000		NULL	        3110.0000
1		2001-05-11 00:00:00.000		4500.0000	2530.0000
2		2001-03-22 00:00:00.000		10000.0000	1440.0000
2		2001-03-24 00:00:00.000		1500.0000	NULL
2		2001-03-29 00:00:00.000		NULL	        7848.0000
2		2001-04-02 00:00:00.000		NULL	        2040.0000
3		2001-09-13 00:00:00.000		11500.0000	1500.0000
3		2001-09-14 00:00:00.000		NULL	        2300.0000
3		2001-10-02 00:00:00.000		18000.0000	NULL
3		2002-09-16 00:00:00.000		NULL	        2150.0000

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-02-09
@Stalker_RED

Visual_SQL_JOINS_orig.jpg
Your case is bottom-left.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question