Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,390 --> 00:00:06,530
Till now, we were trying to join two completely different tables based on a common key.
1
2
00:00:08,490 --> 00:00:15,720
Now we are going to discuss about combining queries so there are certain operators which can help us
2
3
00:00:16,110 --> 00:00:19,170
combine the output of multiple queries.
3
4
00:00:20,590 --> 00:00:29,160
For example, if we have one select query giving us one output and another select query giving us another
4
5
00:00:29,160 --> 00:00:37,200
output, if the structure of the output of the two queries is similar, then we can combine the results
5
6
00:00:37,200 --> 00:00:41,800
of these two select queries using certain operators.
6
7
00:00:42,570 --> 00:00:48,360
Now these operators can be any of these intersect except and union.
7
8
00:00:51,770 --> 00:00:58,240
Let me show you an example so that you understand the difference between Intersect except and union,
8
9
00:00:59,990 --> 00:01:07,460
so suppose from one query I am getting this result, it is giving me a result of 5 customers and their
9
10
00:01:07,460 --> 00:01:08,000
ages.
10
11
00:01:08,990 --> 00:01:14,750
And there is another query which is giving me a list of another five customers and their age.
11
12
00:01:16,580 --> 00:01:25,100
Now you can note that out of the five customers in the first table, these third and fourth customers
12
13
00:01:25,820 --> 00:01:29,120
are also present in the result of the second query.
13
14
00:01:30,320 --> 00:01:31,770
So these two are common.
14
15
00:01:33,020 --> 00:01:38,330
You can also see that the structure of the output from both the queries is same.
15
16
00:01:38,670 --> 00:01:45,710
They both have two columns and the first column contains customer name, which is textual, and the
16
17
00:01:45,710 --> 00:01:49,060
second column contains age, which is numeric.
17
18
00:01:50,000 --> 00:01:53,300
So I can combine the result of these two queries.
18
19
00:01:54,440 --> 00:01:55,940
Again, just like join.
19
20
00:01:56,030 --> 00:02:00,850
I have multiple options of how I want to combine these two results.
20
21
00:02:01,190 --> 00:02:07,830
The first option is I want only those results which are present in both of these query output.
21
22
00:02:08,810 --> 00:02:15,770
So these third and fourth customers, which are present in both the queries, they will come if I use
22
23
00:02:15,980 --> 00:02:16,730
Intersect.
23
24
00:02:17,420 --> 00:02:23,870
So Intersect will give me those rows which are present in both the output.
24
25
00:02:24,990 --> 00:02:26,210
The order does not matter.
25
26
00:02:26,480 --> 00:02:33,290
So both of them do not need to be at the third or fourth position, only that this customer should be
26
27
00:02:33,290 --> 00:02:35,140
present in both the outputs.
27
28
00:02:35,990 --> 00:02:41,820
So Intersect will show me all the customers which were present in both output.
28
29
00:02:43,250 --> 00:02:51,090
Then we have except. Except will remove the common customers from the first output.
29
30
00:02:52,850 --> 00:03:02,000
So if I have the first output, which is this, it will look at the second output and compare each customer
30
31
00:03:02,000 --> 00:03:02,450
name.
31
32
00:03:03,170 --> 00:03:09,380
Whichever customer name is also present in the second output, those will be removed from the first
32
33
00:03:09,380 --> 00:03:14,390
output and then we will get the remaining names from the first output.
33
34
00:03:15,020 --> 00:03:21,510
So in the first output I had these five names, third and fourth were also present in the second output.
34
35
00:03:21,530 --> 00:03:30,650
So those were removed and I am getting remaining three names in my output. Union combines the result
35
36
00:03:30,650 --> 00:03:37,670
of both the outputs and you can see that I am getting all the names from the first table and the names
36
37
00:03:37,680 --> 00:03:38,960
from the second table also.
37
38
00:03:40,640 --> 00:03:48,980
So intersect will give me only those outputs which are present in both the tables, except will remove the
38
39
00:03:48,980 --> 00:03:50,510
output which are present in both
39
40
00:03:50,510 --> 00:03:57,770
the table. Union will show me the combined output of all the select queries.
40
41
00:04:00,790 --> 00:04:07,000
Now, let us discuss each of them, one by one in detail, so Intersect operator, as I have said,
41
42
00:04:07,180 --> 00:04:12,490
will return the common rows from the results of two or more select queries.
42
43
00:04:13,840 --> 00:04:15,190
The syntax goes like this.
43
44
00:04:15,460 --> 00:04:16,720
You have one select query.
44
45
00:04:17,230 --> 00:04:21,280
You can select multiple columns from table one.
45
46
00:04:21,850 --> 00:04:28,930
Then you have another select query where you have to select the same columns from table two and you
46
47
00:04:28,930 --> 00:04:34,150
can find the intersection of these two select queries using this Intersect.
47
48
00:04:34,150 --> 00:04:43,690
Operator, we can have more select queries also and we can add more intersect operators also. So if we
48
49
00:04:43,690 --> 00:04:50,110
have more select queries and more intersect operators, it will find the intersection of all those select
49
50
00:04:50,110 --> 00:04:50,620
queries.
50
51
00:04:54,180 --> 00:05:03,510
So here's an example, we have the sales 2015 table and we have customer 2060 table, there are some
51
52
00:05:03,510 --> 00:05:09,860
customer ideas present in the sales 2015 table, some customer ideas present in the customer 2060 table.
52
53
00:05:10,620 --> 00:05:17,190
If I find intersection, I'll get the customer Id's which are present in both, sales
53
54
00:05:17,190 --> 00:05:20,010
2015 table and customer 2060 table.
54
55
00:05:22,470 --> 00:05:27,930
So let's go to pgAdmin and write this query to see those customer ids which are present in both
55
56
00:05:27,930 --> 00:05:28,500
the tables.
56
57
00:05:30,520 --> 00:05:32,800
So here we will write select
57
58
00:05:35,110 --> 00:05:36,060
customer id.
58
59
00:05:39,540 --> 00:05:44,640
From Sales 2015.
59
60
00:05:48,190 --> 00:05:52,150
Intersect, select.
60
61
00:05:54,900 --> 00:05:56,190
Customer ID.
61
62
00:05:58,200 --> 00:05:58,710
From.
62
63
00:06:00,500 --> 00:06:01,800
Customer
63
64
00:06:03,570 --> 00:06:09,060
underscore 20_60 select this query and run it.
64
65
00:06:11,420 --> 00:06:21,670
So you can see that 436 customer ids are such which are present in both sales 2015 and customer 2060 table.
65
66
00:06:22,310 --> 00:06:24,890
And this is a list of those customer ids.
66
67
00:06:26,680 --> 00:06:34,000
Now, there is a variation of Intersect, which is called intersect all so if you have duplicate values
67
68
00:06:34,810 --> 00:06:39,790
in the sales and customer table, you can use intersect all.
68
69
00:06:40,850 --> 00:06:48,830
And this is available with Union also, So there is Union and union all. Union will give us combined
69
70
00:06:48,830 --> 00:06:54,360
output, but it will remove duplicates, but union all will keep the duplicates
70
71
00:06:54,380 --> 00:06:58,730
also. We will discuss union in detail in the upcoming videos.
71
72
00:06:59,090 --> 00:07:04,950
In the next video, we will discuss about Except Operator and then we will talk about Union Operator.
72
73
00:07:05,210 --> 00:07:06,350
See you in the next video.
7813
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.