All language subtitles for 1. Subqueries

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:06,656 --> 00:00:07,424 Hi everyone 1 2 00:00:08,192 --> 00:00:10,496 In this lecture we will be discussing about subqueries 2 3 00:00:10,752 --> 00:00:14,080 Subqueries are a very powerful tool and you must know this 3 4 00:00:15,104 --> 00:00:16,640 What is a subquery? It is a 4 5 00:00:16,896 --> 00:00:18,432 Query within a query 5 6 00:00:19,200 --> 00:00:24,064 Till now we've been retrieving raw data or very simple processed data 6 7 00:00:24,832 --> 00:00:30,976 If you want to retrieve Complex data we might have to use Complex structured query with a lot of 7 8 00:00:31,232 --> 00:00:32,512 Joins and unions 8 9 00:00:33,024 --> 00:00:35,072 This is where subquery comes in 9 10 00:00:35,584 --> 00:00:38,400 You can use a sub query to reduce your efforts 10 11 00:00:38,656 --> 00:00:40,448 Make the query more readable 11 12 00:00:40,704 --> 00:00:44,800 Reduce the time taken and even save some resources on your database 12 13 00:00:46,080 --> 00:00:48,896 Subqueries can reside within the where clause 13 14 00:00:49,152 --> 00:00:51,968 The from clause or the select clause 14 15 00:00:52,992 --> 00:00:54,528 Syntax I have provided here 15 16 00:00:54,784 --> 00:00:57,856 Is how to use the subquery in the where clause 16 17 00:00:58,624 --> 00:01:03,488 It is like a normal query it is starting with select column names from table name 17 18 00:01:03,744 --> 00:01:06,304 Where you specify the column name 18 19 00:01:06,560 --> 00:01:08,096 Give a comparison operator 19 20 00:01:08,608 --> 00:01:10,656 And after this comes the query 20 21 00:01:11,680 --> 00:01:16,032 This query is part of the main query the outer query 21 22 00:01:16,544 --> 00:01:19,360 And this part is called the sub query 22 23 00:01:19,616 --> 00:01:23,968 Where you write select the column names from table name and give the where condition 23 24 00:01:24,480 --> 00:01:27,552 the output of this subquery will be used 24 25 00:01:27,808 --> 00:01:30,368 as values in the comparison operator 25 26 00:01:30,880 --> 00:01:34,464 Against which the column name 2 the column name 2 written here 26 27 00:01:34,976 --> 00:01:37,280 The values of this column will be compared 27 28 00:01:37,792 --> 00:01:40,352 It will become more clear when we look at the examples 28 29 00:01:43,168 --> 00:01:45,472 Let us say you want to find out 29 30 00:01:45,728 --> 00:01:48,288 the order lines or the sales items 30 31 00:01:48,800 --> 00:01:53,152 where the customer who bought those orders is aged more than 60 31 32 00:01:54,688 --> 00:02:00,832 If we do not know about subqueries how would we do this. We will first find out all the customers 32 33 00:02:01,088 --> 00:02:02,624 Who are aged more than 60 33 34 00:02:03,904 --> 00:02:05,696 Probably store it in a new table 34 35 00:02:05,952 --> 00:02:11,072 and then we'll take an inner join of this sales table and the new table that we have created 35 36 00:02:11,328 --> 00:02:15,168 to find out customers whose age is 60 and who have given orders 36 37 00:02:16,192 --> 00:02:21,312 instead of doing this we'll just use the subquery. first we will find out all the sales 37 38 00:02:22,080 --> 00:02:23,104 From the sales table 38 39 00:02:23,360 --> 00:02:25,152 In the condition we will say 39 40 00:02:25,408 --> 00:02:29,760 Only those customer IDs which belong to this sub query 40 41 00:02:30,016 --> 00:02:33,344 I need this subquery will find out those customer IDs 41 42 00:02:33,600 --> 00:02:35,648 Where customer's age is more than 60 42 43 00:02:36,416 --> 00:02:37,184 so we will get 43 44 00:02:37,696 --> 00:02:38,976 Customer IDs 44 45 00:02:39,232 --> 00:02:41,280 here from this subquery 45 46 00:02:41,536 --> 00:02:45,120 We'll check the customer ID belonging to this set 46 47 00:02:45,632 --> 00:02:49,728 and for only those customer IDs we will get the select result 47 48 00:02:50,496 --> 00:02:52,544 Let's go and write this in the interface 48 49 00:02:55,360 --> 00:02:56,640 So we will select 49 50 00:02:56,896 --> 00:02:59,968 Star from sales 50 51 00:03:01,248 --> 00:03:01,760 Where 51 52 00:03:04,576 --> 00:03:05,600 Customer ID 52 53 00:03:07,392 --> 00:03:11,232 Belongs to 53 54 00:03:11,488 --> 00:03:17,120 where customer ID belongs to a set and we'll define this set by 54 55 00:03:17,376 --> 00:03:18,912 Select 55 56 00:03:20,704 --> 00:03:22,496 customer ID 56 57 00:03:24,800 --> 00:03:27,872 From 57 58 00:03:29,408 --> 00:03:30,944 The customer table 58 59 00:03:33,248 --> 00:03:35,040 Where age is 59 60 00:03:35,296 --> 00:03:36,320 More than 60 60 61 00:03:40,416 --> 00:03:44,256 So if you remember while discussing the IN keyword 61 62 00:03:45,024 --> 00:03:47,584 I told you that I'll be using this IN 62 63 00:03:47,840 --> 00:03:50,400 in the sub queries and there is a lot of 63 64 00:03:50,656 --> 00:03:55,776 Importance of this IN keyword in subqueries. so this is how we use IN 64 65 00:03:56,288 --> 00:03:57,312 during some queries 65 66 00:03:58,080 --> 00:03:59,360 and let us run this query 66 67 00:04:01,152 --> 00:04:02,176 Here you can see 67 68 00:04:02,688 --> 00:04:04,480 I have all the sales 68 69 00:04:04,992 --> 00:04:08,832 Where the customer who has bought is aged more than 60 69 70 00:04:09,344 --> 00:04:12,416 Do note that while selecting in the subquery 70 71 00:04:12,672 --> 00:04:16,000 We had only one column which is customer ID 71 72 00:04:16,256 --> 00:04:20,863 We cannot have the second column because when we are having this IN keyword 72 73 00:04:22,143 --> 00:04:24,447 This IN keyboard wants to compare 73 74 00:04:24,703 --> 00:04:25,983 Customer ID 74 75 00:04:26,239 --> 00:04:27,007 In this 75 76 00:04:27,519 --> 00:04:30,847 range. These are the values that we are giving 76 77 00:04:31,103 --> 00:04:34,431 So it cannot have two columns. It should have only one column 77 78 00:04:34,687 --> 00:04:37,759 Which has the all the values listed in it 78 79 00:04:38,527 --> 00:04:40,063 So this one column 79 80 00:04:40,831 --> 00:04:46,975 Which contains the customer IDs of customers with more than age 60 will be used to compare the customer IDs 80 81 00:04:47,231 --> 00:04:49,791 And give the output from sales table 81 82 00:04:52,351 --> 00:04:56,191 so we just checked how to use the subquery in where 82 83 00:04:56,447 --> 00:05:00,799 Let us now go and learn how to use the subquery within from 83 84 00:05:01,823 --> 00:05:07,967 Now we have a table, product table which list all the products that we have in our inventory 84 85 00:05:08,223 --> 00:05:12,319 Suppose we want to find out how much of each product has been sold 85 86 00:05:13,343 --> 00:05:16,927 I want to find out the quantity of each product sold 86 87 00:05:17,951 --> 00:05:21,279 In the product table I have the product name and categories also 87 88 00:05:22,303 --> 00:05:27,935 So, In future if I have this data I'll be able to find out which category is selling how much 88 89 00:05:28,191 --> 00:05:29,983 So this data may be of use to me 89 90 00:05:30,495 --> 00:05:36,639 If I want to find out this, suppose we are not using the sub queries. I'll first need to find out the sales 90 91 00:05:36,895 --> 00:05:39,199 quantity from the sales table 91 92 00:05:39,455 --> 00:05:40,991 I'll group by 92 93 00:05:41,503 --> 00:05:46,879 the product ID and find out the sum of quantity and save this data in a new table 93 94 00:05:47,903 --> 00:05:51,999 And then I'll do a join of this table with the product table 94 95 00:05:52,255 --> 00:05:54,303 This is how I will get this data 95 96 00:05:55,327 --> 00:06:01,471 What this will lead to is I'll have one additional table which will be taking up resources in my database. Instead of doing this 96 97 00:06:02,495 --> 00:06:07,359 I can do the two things in a single query without saving the data into a table 97 98 00:06:07,615 --> 00:06:10,175 Let us see how subquery helps us in this 98 99 00:06:10,687 --> 00:06:15,807 First the table that we would be saving think of it as a table that you have actually saved 99 100 00:06:16,063 --> 00:06:22,207 That table will be created in the sub query and we'll save that table with some name. you can take out 100 101 00:06:22,463 --> 00:06:23,999 The quantity from the table 101 102 00:06:24,767 --> 00:06:29,119 When we are writing the query we'll select all the columns from the product table 102 103 00:06:29,631 --> 00:06:30,143 And 103 104 00:06:30,399 --> 00:06:31,679 this quantity 104 105 00:06:32,447 --> 00:06:33,215 From the 105 106 00:06:33,471 --> 00:06:36,031 Table that we'll be creating in the subquery 106 107 00:06:37,055 --> 00:06:41,919 After this we'll write the product table and name it as a 107 108 00:06:42,687 --> 00:06:44,479 We will left join this 108 109 00:06:44,735 --> 00:06:47,039 Why we are left joining, you should be knowing 109 110 00:06:47,295 --> 00:06:53,439 All the values of product table will be listed even if it is not part of this second table which will be the subquery 110 111 00:06:53,695 --> 00:06:55,743 So we are using left join 111 112 00:06:56,255 --> 00:06:57,791 This is the 2nd table 112 113 00:06:58,303 --> 00:06:59,839 We'll select the product ID 113 114 00:07:00,863 --> 00:07:03,679 sum the quantity and we group it by the 114 115 00:07:03,935 --> 00:07:04,959 Product ID 115 116 00:07:05,215 --> 00:07:06,239 and we save it as b 116 117 00:07:09,311 --> 00:07:10,847 we are joining these two 117 118 00:07:11,103 --> 00:07:13,407 With the common predicate product ID 118 119 00:07:14,175 --> 00:07:16,479 and we order this with the quantity 119 120 00:07:17,503 --> 00:07:20,063 Let's go and write this and see the result 120 121 00:07:22,367 --> 00:07:24,159 we will select 121 122 00:07:27,487 --> 00:07:29,279 Product ID 122 123 00:07:32,351 --> 00:07:33,375 Product name 123 124 00:07:36,447 --> 00:07:38,495 Add category 124 125 00:07:38,751 --> 00:07:43,871 from the second table 125 126 00:07:44,127 --> 00:07:45,663 That we'll be creating, we need 126 127 00:07:46,431 --> 00:07:49,503 the quantity ok 127 128 00:07:50,271 --> 00:07:53,343 This quantity is actually 128 129 00:07:53,599 --> 00:07:54,623 This quantity 129 130 00:07:55,135 --> 00:07:57,695 Is also an alias so from 130 131 00:07:58,719 --> 00:08:00,511 The table is product 131 132 00:08:05,375 --> 00:08:06,399 which is saved as a 132 133 00:08:06,911 --> 00:08:09,727 After this we will left join 133 134 00:08:14,079 --> 00:08:20,223 this is the subquery part now. select product ID 134 135 00:08:22,015 --> 00:08:23,551 Sum of 135 136 00:08:23,807 --> 00:08:24,831 The quantity 136 137 00:08:28,671 --> 00:08:31,231 As quantity 137 138 00:08:31,487 --> 00:08:32,511 Now we have 138 139 00:08:32,767 --> 00:08:33,791 the Alias 139 140 00:08:35,583 --> 00:08:37,375 From sales 140 141 00:08:37,631 --> 00:08:39,679 Table 141 142 00:08:39,935 --> 00:08:42,751 Grouped by the product ID 142 143 00:08:47,615 --> 00:08:49,663 this whole subquery part 143 144 00:08:50,175 --> 00:08:54,015 Is giving us a table and that table is named as b 144 145 00:08:56,575 --> 00:08:58,111 and we will use the 145 146 00:08:58,367 --> 00:08:59,903 Condition of 146 147 00:09:00,415 --> 00:09:05,023 Product ID of A is same as product ID of B 147 148 00:09:05,279 --> 00:09:09,375 This is the condition that we need to give for the left join 148 149 00:09:09,631 --> 00:09:13,727 Ordered by 149 150 00:09:17,311 --> 00:09:19,615 Quantity in the descending order 150 151 00:09:26,271 --> 00:09:27,039 You can see 151 152 00:09:27,807 --> 00:09:28,319 for 152 153 00:09:30,879 --> 00:09:32,927 We now have all the products 153 154 00:09:33,695 --> 00:09:34,975 from the product table 154 155 00:09:35,487 --> 00:09:38,047 And the quantities of that product sold 155 156 00:09:39,071 --> 00:09:40,863 Mapped against each product 156 157 00:09:41,631 --> 00:09:45,215 you can see that First row has actually null quantity 157 158 00:09:45,471 --> 00:09:48,031 You might be wondering why it is at the top 158 159 00:09:48,799 --> 00:09:52,127 Because null is always considered as the largest number 159 160 00:09:52,639 --> 00:09:58,015 Whenever you order it in descending order Null will always be at top if you order it in ascending order 160 161 00:09:58,271 --> 00:10:00,063 Null will always be at the bottom 161 162 00:10:01,855 --> 00:10:03,391 The same thing you could have done 162 163 00:10:03,647 --> 00:10:04,415 by 163 164 00:10:04,671 --> 00:10:06,463 Creating two different queries 164 165 00:10:06,719 --> 00:10:07,999 Saving the table in between 165 166 00:10:08,767 --> 00:10:11,583 Instead of doing that we just clubbed the two queries 166 167 00:10:12,607 --> 00:10:15,679 One query become a sub query for the other outer query 167 168 00:10:17,471 --> 00:10:20,287 and this saved us some space on the database 168 169 00:10:20,543 --> 00:10:23,359 Now let us look at the third use case of 169 170 00:10:23,615 --> 00:10:24,639 Subquery 170 171 00:10:25,151 --> 00:10:28,735 Here the subquery will be written within the 171 172 00:10:28,991 --> 00:10:30,015 Select part 172 173 00:10:30,783 --> 00:10:36,927 First we wrote it in the where part next we wrote it in the from part now we are writing it in the select 173 174 00:10:37,183 --> 00:10:37,695 part 174 175 00:10:38,719 --> 00:10:41,535 Writing a subquery in the select part is just 175 176 00:10:41,791 --> 00:10:43,327 The same as the left join 176 177 00:10:43,839 --> 00:10:49,727 For example in left join we found out for each order line what is the customer name and age 177 178 00:10:49,983 --> 00:10:52,543 Same thing you can do by using the sub queries also 178 179 00:10:53,055 --> 00:10:53,567 How 179 180 00:10:53,823 --> 00:10:56,639 We'll select customer ID and order line 180 181 00:10:57,151 --> 00:10:59,455 these are directly coming from the sales table 181 182 00:11:00,735 --> 00:11:03,551 but the 3rd column which is the customer name 182 183 00:11:03,807 --> 00:11:06,879 Will come from the customer table and it'll be matched 183 184 00:11:07,135 --> 00:11:08,415 Using the customer ID 184 185 00:11:09,951 --> 00:11:11,743 So customer ID order line 185 186 00:11:13,279 --> 00:11:14,559 And there is a subquery 186 187 00:11:14,815 --> 00:11:16,095 Giving us the customer name 187 188 00:11:16,607 --> 00:11:17,631 from the customer table 188 189 00:11:17,887 --> 00:11:19,423 Let us go and write this in the 189 190 00:11:19,935 --> 00:11:20,447 Interface 190 191 00:11:22,751 --> 00:11:24,543 So we select 191 192 00:11:26,335 --> 00:11:29,663 Customer ID 192 193 00:11:31,455 --> 00:11:32,735 orderline 193 194 00:11:36,575 --> 00:11:39,135 and then we'll write 194 195 00:11:39,647 --> 00:11:45,535 the subquery where we'll find out the customer name 195 196 00:11:45,791 --> 00:11:49,887 Which is matching the customer id from the sales table 196 197 00:11:54,495 --> 00:11:55,519 Customer ID 197 198 00:11:56,799 --> 00:12:01,663 Customer dot customer ID is equal to sales dot customer ID 198 199 00:12:04,991 --> 00:12:07,807 These customer IDs are matched and we get the 199 200 00:12:08,063 --> 00:12:09,343 Customer name 200 201 00:12:09,599 --> 00:12:15,743 from the sales table 201 202 00:12:17,023 --> 00:12:20,351 by customer 202 203 00:12:21,631 --> 00:12:22,399 Select this 203 204 00:12:23,935 --> 00:12:24,703 run it 204 205 00:12:26,495 --> 00:12:27,775 so we get the data 205 206 00:12:28,543 --> 00:12:31,871 These are all the customer IDs in the sales table 206 207 00:12:32,383 --> 00:12:37,247 And wherever customer name is available from the customer table it is mapped here 207 208 00:12:37,503 --> 00:12:39,295 The same thing we did using the 208 209 00:12:39,551 --> 00:12:40,575 Left join 209 210 00:12:42,879 --> 00:12:45,951 However, whenever you have the option of using left join 210 211 00:12:46,207 --> 00:12:47,231 vs subquery 211 212 00:12:47,743 --> 00:12:49,535 I will suggest that we go with the 212 213 00:12:49,791 --> 00:12:52,607 Joins, since they perform better in the database 213 214 00:12:53,375 --> 00:12:59,519 subqueries may take up more resources of the database while performing and it may give result after taking a long time 214 215 00:13:02,335 --> 00:13:03,615 There are few rules 215 216 00:13:03,871 --> 00:13:06,175 I'll mention two or three important ones 216 217 00:13:06,943 --> 00:13:10,015 First is subqueries must be enclosed within parentheses 217 218 00:13:10,527 --> 00:13:13,087 We have been putting brackets around the subqueries 218 219 00:13:13,343 --> 00:13:15,391 So always remember to put brackets 219 220 00:13:16,415 --> 00:13:17,439 and the second point 220 221 00:13:17,695 --> 00:13:20,511 A subquery can have only one column in the select clause 221 222 00:13:21,535 --> 00:13:27,679 Where multiple columns are in the main query for the query subquery to compare date selected column so what this point means is 222 223 00:13:28,191 --> 00:13:29,471 Like we discussed here 223 224 00:13:30,751 --> 00:13:32,031 In this example 224 225 00:13:32,799 --> 00:13:35,359 When we were discussing this IN keyword 225 226 00:13:35,871 --> 00:13:38,175 Here only one column should be present 226 227 00:13:38,431 --> 00:13:40,479 It cannot be be more than one columns 227 228 00:13:40,991 --> 00:13:43,551 That is what this point is signifying 228 229 00:13:45,087 --> 00:13:48,159 So whenever you are comparing values using the subquery 229 230 00:13:48,415 --> 00:13:49,951 It should have only one column 230 231 00:13:52,255 --> 00:13:56,607 Another important point is this last point where between operator 231 232 00:13:56,863 --> 00:13:58,655 It can be used within a subquery 232 233 00:13:58,911 --> 00:14:05,055 It cannot be used with a subquery, so if within a query you are having a subquery you cannot use 233 234 00:14:05,311 --> 00:14:08,639 Between operator like we used IN operator 234 235 00:14:09,151 --> 00:14:10,943 Between cannot be used there 235 236 00:14:11,199 --> 00:14:14,527 But within a subquery you can use between operator 236 237 00:14:14,783 --> 00:14:16,575 These are some important points 237 238 00:14:17,087 --> 00:14:18,879 We will not be discussing other points 238 239 00:14:19,135 --> 00:14:23,743 Just read them. this presentation will be part of the resources of this lecture 239 240 00:14:25,023 --> 00:14:27,071 And that is all for subqueries 240 241 00:14:29,375 --> 00:14:33,983 In the next video we will learn about views and how they are useful 19874

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.