1 00:00:00,710 --> 00:00:07,400 Data can make more sense and may reveal patterns more readily when arranged in order rather than jumbled 2 00:00:07,400 --> 00:00:07,880 randomly. 3 00:00:07,880 --> 00:00:08,330 Right. 4 00:00:08,360 --> 00:00:15,530 So in SQL, we order the results of a query using the mouse containing the keywords order by followed 5 00:00:15,530 --> 00:00:18,260 by the name of the column or columns to sort. 6 00:00:18,290 --> 00:00:22,990 So applying this class doesn't change the original table, only the result of the query here. 7 00:00:23,000 --> 00:00:26,630 So now we firstly select the lastname firstname. 8 00:00:26,630 --> 00:00:30,890 And after that we also had the hire date and salary. 9 00:00:30,890 --> 00:00:31,400 Right? 10 00:00:31,400 --> 00:00:39,320 And we selected this from teachers and after teachers before the semicolon here. 11 00:00:39,350 --> 00:00:47,930 Or we can also delete semicolon, we can add later, later we will use order by and let's delete salary 12 00:00:47,930 --> 00:00:54,530 because we will use that salary here, order by salary and the scanning, the scanning here. 13 00:00:54,680 --> 00:00:59,390 So by default order by source value in a scanning order. 14 00:00:59,390 --> 00:01:07,200 But here I sort in the scanning order by adding the desk keyword and the optional keyword specifies 15 00:01:07,200 --> 00:01:09,030 sorting and scanning order. 16 00:01:09,030 --> 00:01:16,710 So now by ordering the salary column from highest to lowest, I can determine which teachers earn the 17 00:01:16,710 --> 00:01:17,430 most. 18 00:01:18,970 --> 00:01:21,850 And as you can see here, Reynolds earns the most. 19 00:01:21,850 --> 00:01:27,670 So if we if you are if you want also to print the values here, you see the values. 20 00:01:27,670 --> 00:01:32,740 You can also add the salary to select after hire date. 21 00:01:32,740 --> 00:01:33,190 Right? 22 00:01:33,230 --> 00:01:36,010 Actually, instead of adding. 23 00:01:36,880 --> 00:01:38,890 After the air date. 24 00:01:38,920 --> 00:01:41,320 We can also add before the last name. 25 00:01:41,320 --> 00:01:47,770 So with these numbers, we can identify the top earners of our. 26 00:01:48,570 --> 00:01:49,440 Teachers, right? 27 00:01:49,440 --> 00:01:52,650 So we will add the salary before the last name. 28 00:01:52,650 --> 00:01:55,140 And here that's it. 29 00:01:55,140 --> 00:02:06,960 The first earner, 65,000, 43,000 543,000 538,000 536,000 30 00:02:07,560 --> 00:02:10,680 230,200. 31 00:02:10,680 --> 00:02:17,130 And we can also do, for example, instead of salary, we can also do remember we have had an ID here, 32 00:02:17,130 --> 00:02:23,280 so ID and here, let's actually see the ID here or after. 33 00:02:23,280 --> 00:02:26,190 Let's add ID after the date. 34 00:02:27,140 --> 00:02:27,920 And. 35 00:02:52,390 --> 00:02:52,630 Here. 36 00:02:52,630 --> 00:02:59,260 If you are using keyword as a name, which is it's not a best practice, you will use double quotes 37 00:03:00,040 --> 00:03:01,390 outside the ID here. 38 00:03:01,390 --> 00:03:09,550 So in this case, if I write ID just regularly, it will the SQL will identify it as a query and that's 39 00:03:09,550 --> 00:03:13,570 why we will use the ID with it in the double quotes. 40 00:03:13,570 --> 00:03:19,660 And here and as you can see here, six, five, four, three, two, one. 41 00:03:19,660 --> 00:03:28,930 So that's because we use that in a scanning order of the ID So firstly six, five, four, three, two, 42 00:03:28,930 --> 00:03:29,560 one. 43 00:03:29,560 --> 00:03:31,210 So if we delete that. 44 00:03:36,370 --> 00:03:37,900 And as you can see here. 45 00:03:38,780 --> 00:03:41,450 One, two, three, four, five, six. 46 00:03:42,040 --> 00:03:46,670 Okay, so the order by clause also accepts numbers instead of columns names. 47 00:03:46,670 --> 00:03:53,060 So with the number identifying the SORT columns according to its position in the select number. 48 00:03:53,060 --> 00:03:58,160 So this for example, order by three and desc here. 49 00:03:58,160 --> 00:04:00,410 So you will see something different here. 50 00:04:00,410 --> 00:04:07,670 And instead of this delete ID, we don't need that because here, as you can see here with this candid 51 00:04:08,120 --> 00:04:08,840 here. 52 00:04:08,840 --> 00:04:16,360 So the ability to sort in our queries gives us a great flexibility in how we retrieve and present the 53 00:04:16,370 --> 00:04:16,760 data. 54 00:04:16,760 --> 00:04:19,010 For example, we are not limited to sorting. 55 00:04:20,370 --> 00:04:21,360 Just one column. 56 00:04:21,750 --> 00:04:24,180 So we can also do this, for example. 57 00:04:24,180 --> 00:04:27,000 Order by order by. 58 00:04:28,620 --> 00:04:31,740 Ordered by school standing. 59 00:04:32,460 --> 00:04:35,970 And higher date the scanning. 60 00:04:37,350 --> 00:04:37,950 That's it. 61 00:04:38,160 --> 00:04:39,600 And last name. 62 00:04:39,600 --> 00:04:40,530 First name. 63 00:04:40,920 --> 00:04:43,230 Let's actually instead of using salary now. 64 00:04:44,320 --> 00:04:44,980 Uh, we don't. 65 00:04:44,980 --> 00:04:46,750 We just want to print the. 66 00:04:49,200 --> 00:04:53,370 Uh, I want the last name and hire date. 67 00:04:54,140 --> 00:04:55,200 Yeah, that's it. 68 00:04:55,220 --> 00:05:01,130 So now, as you can see here, we have last name and hire date here. 69 00:05:01,160 --> 00:05:02,990 So that's it. 70 00:05:05,000 --> 00:05:10,230 As kdb rzj c p. 71 00:05:10,400 --> 00:05:12,080 So this is the ending. 72 00:05:12,080 --> 00:05:15,350 And here, as you can see, here, we have this. 73 00:05:16,710 --> 00:05:24,360 So in this case, we are retrieving the last names of teachers, their school and the date they were 74 00:05:24,540 --> 00:05:25,140 hired. 75 00:05:25,170 --> 00:05:28,770 So here we also need to school. 76 00:05:29,680 --> 00:05:30,040 Here. 77 00:05:31,700 --> 00:05:32,420 That's it. 78 00:05:33,680 --> 00:05:34,310 So. 79 00:05:34,310 --> 00:05:36,710 And the and here. 80 00:05:38,840 --> 00:05:44,090 By sorting this school column in ascending order and hiredate. 81 00:05:44,090 --> 00:05:52,730 In descending order, we create a listing of teacher grouped by a school with the most recently hired 82 00:05:52,730 --> 00:05:54,110 teachers listed first. 83 00:05:56,400 --> 00:06:01,410 So this shows us who the new teachers are at each school. 84 00:06:01,440 --> 00:06:02,160 Right. 85 00:06:02,430 --> 00:06:03,690 As you can see here. 86 00:06:04,920 --> 00:06:08,460 Myers Middle School 2005 eight one. 87 00:06:08,760 --> 00:06:11,670 This is the oldest teacher in Myers Middle School. 88 00:06:11,700 --> 00:06:12,860 2011. 89 00:06:13,470 --> 00:06:16,320 This is the newest teacher on the Myers Middle School. 90 00:06:16,380 --> 00:06:18,040 2011 1013. 91 00:06:18,070 --> 00:06:25,080 And here Reynolds has a Reynolds is the let's actually print the names right first. 92 00:06:25,080 --> 00:06:26,550 So we will meet them. 93 00:06:26,550 --> 00:06:27,030 Right. 94 00:06:27,540 --> 00:06:28,260 I'm sorry. 95 00:06:28,380 --> 00:06:28,830 Okay. 96 00:06:28,840 --> 00:06:30,780 First name. 97 00:06:31,940 --> 00:06:32,620 And that's it. 98 00:06:33,490 --> 00:06:33,900 Yes. 99 00:06:33,910 --> 00:06:44,770 So here Lee Reynolds is the oldest teacher of the Roosevelt School. 100 00:06:45,730 --> 00:06:46,270 High school. 101 00:06:46,750 --> 00:06:54,070 And Jane Smith is the newest teacher of Roosevelt High School. 102 00:06:55,110 --> 00:07:02,220 So you can use order by on more than two columns, but you will soon reach a point of diminishing returns 103 00:07:02,220 --> 00:07:05,100 where the effect will be hardly noticed. 104 00:07:05,400 --> 00:07:12,180 Imagine if you added columns about teachers, how big diggers attained and the grade leveled out, and 105 00:07:12,180 --> 00:07:14,200 the birth date to order by class. 106 00:07:14,220 --> 00:07:18,600 It will be difficult to understand the various sort of directions. 107 00:07:19,400 --> 00:07:23,360 Output once, much less communicate to that of others. 108 00:07:23,540 --> 00:07:27,260 Digesting the data happens more easily when not output. 109 00:07:28,380 --> 00:07:30,770 Answering specific question. 110 00:07:30,780 --> 00:07:38,460 Therefore, a better strategy is to limit the number of columns in your query to only the most important 111 00:07:38,460 --> 00:07:41,910 and run several queries to each question. 112 00:07:43,310 --> 00:07:43,970 So. 113 00:07:45,240 --> 00:07:46,860 And that's it with our lecture. 114 00:07:46,860 --> 00:07:50,310 In the next lecture, we will also learn about the distinct. 115 00:07:50,310 --> 00:07:52,830 My name is Stefan and I'm waiting. 116 00:07:53,950 --> 00:07:56,230 Next lecture of Oxley Academy.