More Advanced Select Queries Part 1

Microsoft SQL Server 2017 for Everyone Manipulating Tables and Data
17 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.39
List Price:  €91.99
You save:  €27.60
£55.09
List Price:  £78.70
You save:  £23.61
CA$95.28
List Price:  CA$136.12
You save:  CA$40.84
A$104.54
List Price:  A$149.36
You save:  A$44.81
S$94.19
List Price:  S$134.57
You save:  S$40.37
HK$545.77
List Price:  HK$779.70
You save:  HK$233.93
CHF 63.61
List Price:  CHF 90.88
You save:  CHF 27.26
NOK kr746.97
List Price:  NOK kr1,067.14
You save:  NOK kr320.17
DKK kr480.44
List Price:  DKK kr686.38
You save:  DKK kr205.93
NZ$114.32
List Price:  NZ$163.32
You save:  NZ$49
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,168.69
List Price:  ৳11,670.06
You save:  ৳3,501.36
₹5,828.58
List Price:  ₹8,326.90
You save:  ₹2,498.32
RM327.90
List Price:  RM468.45
You save:  RM140.55
₦105,229.96
List Price:  ₦150,334.96
You save:  ₦45,105
₨19,431.47
List Price:  ₨27,760.43
You save:  ₨8,328.96
฿2,521.66
List Price:  ฿3,602.53
You save:  ฿1,080.87
₺2,253.99
List Price:  ₺3,220.12
You save:  ₺966.13
B$357.19
List Price:  B$510.29
You save:  B$153.10
R1,273.09
List Price:  R1,818.79
You save:  R545.69
Лв125.75
List Price:  Лв179.65
You save:  Лв53.90
₩95,095.26
List Price:  ₩135,856.19
You save:  ₩40,760.93
₪258.97
List Price:  ₪369.98
You save:  ₪111
₱4,052.01
List Price:  ₱5,788.84
You save:  ₱1,736.82
¥10,898.49
List Price:  ¥15,569.94
You save:  ¥4,671.45
MX$1,161.50
List Price:  MX$1,659.36
You save:  MX$497.86
QR254.39
List Price:  QR363.44
You save:  QR109.04
P945.24
List Price:  P1,350.41
You save:  P405.16
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,276.23
List Price:  E£4,680.53
You save:  E£1,404.29
ብር4,006.99
List Price:  ብር5,724.51
You save:  ብር1,717.52
Kz59,384.57
List Price:  Kz84,838.74
You save:  Kz25,454.16
CLP$62,491.07
List Price:  CLP$89,276.78
You save:  CLP$26,785.71
CN¥496.64
List Price:  CN¥709.51
You save:  CN¥212.87
RD$4,086.46
List Price:  RD$5,838.06
You save:  RD$1,751.59
DA9,413.48
List Price:  DA13,448.41
You save:  DA4,034.92
FJ$155.95
List Price:  FJ$222.79
You save:  FJ$66.84
Q541.96
List Price:  Q774.26
You save:  Q232.30
GY$14,594.23
List Price:  GY$20,849.79
You save:  GY$6,255.56
ISK kr9,678.91
List Price:  ISK kr13,827.61
You save:  ISK kr4,148.70
DH694.53
List Price:  DH992.23
You save:  DH297.69
L1,236.03
List Price:  L1,765.83
You save:  L529.80
ден3,963.41
List Price:  ден5,662.25
You save:  ден1,698.84
MOP$560.62
List Price:  MOP$800.92
You save:  MOP$240.30
N$1,276.29
List Price:  N$1,823.35
You save:  N$547.06
C$2,568.19
List Price:  C$3,669
You save:  C$1,100.81
रु9,300.39
List Price:  रु13,286.84
You save:  रु3,986.45
S/260.50
List Price:  S/372.17
You save:  S/111.66
K270.91
List Price:  K387.03
You save:  K116.12
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,780.59
List Price:  ZK2,543.81
You save:  ZK763.22
L320.34
List Price:  L457.65
You save:  L137.31
Kč1,591.01
List Price:  Kč2,272.97
You save:  Kč681.96
Ft24,857.67
List Price:  Ft35,512.48
You save:  Ft10,654.81
SEK kr747.87
List Price:  SEK kr1,068.43
You save:  SEK kr320.56
ARS$62,063.45
List Price:  ARS$88,665.87
You save:  ARS$26,602.42
Bs481.99
List Price:  Bs688.59
You save:  Bs206.59
COP$267,109.86
List Price:  COP$381,601.87
You save:  COP$114,492.01
₡35,703.30
List Price:  ₡51,006.90
You save:  ₡15,303.60
L1,727.27
List Price:  L2,467.63
You save:  L740.36
₲522,695.49
List Price:  ₲746,739.85
You save:  ₲224,044.36
$U2,715.34
List Price:  $U3,879.22
You save:  $U1,163.88
zł274.22
List Price:  zł391.77
You save:  zł117.54
Already have an account? Log In

Transcript

Hey guys, in this video we will be looking at more advanced SELECT statements and we will start looking at how our editor helps us with creating these statements. So firstly, I am going to instead of creating a script to extract the records and read records from our school database and students table, I'm going to use the editors function by drilling down into the database into the tables and finding the table that I would like to select rows from and then I'm going to right click and go to Select top 1000 rules. Once that is done, it will actually generate a select statement and bring back all of the results accordingly. Now let us take some time and dissect the select statement that is generated for us. We see here we know our keywords select and from now we know that in between the selects and the Fromme, we really have a list of columns then we Have an additional keyword being introduced here in the form of top.

And what this really does is to say give me the first X number of records. So the default option gives us 1000. Of course, we only have 11 Records in our database. So 1000 is more than sufficient to get back all the records in our database. However, if we had a database of 10,000 Records, then this would really just give us the first 1000 Records. So if I wanted only five records, then I would change this value to five and say, give me the top five.

And there we go. Another thing I want to point out is the fully qualified name for our table. Now, if we dissect this slide, we see that we have the database name, dots, the context or schema name, which if it's in default settings would be DB O, and another dot, and then the table object. So this is actually the fully qualified table name, which means that if this database script is not within the context of the specific database, you can actually just write all this entire line here. And it will know to contextualize this table to this database. So I'm going to change school, up top here to school v2.

And then if I execute this again, recall that we have no data in our school v two students table. So I'm just going to quickly bring up that table and show you that that has no records. And we can see here that we would have school v2 dot students dot dbo dot students. So if I run this query, even though I have contextualized this scripts to school underscore v two. If I execute this query right now, it will know that I want the students table that is found in the school database. And that is the advantage of using that fully qualified database table name.

So this would actually allow us to be able to query different tables from different databases all in one script file. Now, let's expand our scope a bit and actually start looking at filtering. Now there are times when you want only certain records, I'm just going to remove this constraint and execute for all columns once again, and do recall that if you wanted all columns without having to type them out, you could replace all of that with Star over the auto generated code gave us every column. So if we wanted to filter and find let's see, only the students whose last name is Williams are we wanted to see all of the Williams is in our database. That is a form of filtering because now we're narrowing down the results at a specific what we call condition. So SQL gives us a keyword in the form of we're, and then it allows us to specify a condition afterwards, a condition could be like a column being equal to a certain value, or being greater than less than or looking like it.

Those are all conditions. so in this situation, once again, the scenario is that we want only the students whose last names are Williams, we see we only have one, so we should only expect one record to come back. So I'm just going to read a new query down here and with a comment this one out, so I can just highlight all of this and use this button up here or once again, hold on key control and press key. And see. So once again, our objective is to retrieve only the students with the last name Williams. So I'm going to add a new keyword where and then I go about adding the condition, the condition is hinged on the column last name, so I say last name, and then my condition stipulates that the last name column should be equal to a specific value.

And I'm just going to go ahead and produce this value here. So because we're comparing a string column or an N var char column, it's a word, we have to make sure that we're comparing it with also a word our string. So if I go ahead and execute this, then we will see that we have filtered out all of the data to only retrieve the students with the last name Williams. In this case, we only have one, but then let's try a more popular one. So we had a bunch of test students and I want it Any student with the last name, student, someone to execute, we only got about one. Now that's kind of strange, but it is very accurate.

Because if we once again, and I'm just going to uncomment this. And by highlighting this and clicking execute, I can specify that I only want to run this query. And I see that I had quite a few students with the last name students, except only one had students, everybody else had a number attached to the word students. Which then brings us to another type of filtering, where we can filter on what we call a wild card. A wild card is essentially posts looking for a portion of text that meets our requirements and bringing back anything that meets that pattern. So I'm just going to go down here and create another query.

So in this scenario, we want all the students With the word students in their last name, which means we need to say where last name once again, the column that's in contention for the condition what then instead of seeing equal we're going to see like, though like is grayed out, but it's also a key word after our like keyword, we're going to employ the use of what we call a wild card. Now our wildcard symbol is in the form of a modulus or percentage sign. And this allows us to actually specify corrector sequence that we are looking for in any block of text, which means if I want every last name that has the letter I in it, I can see I so that's open quotation mark percentage sign or modulus I and then modulus. And then this is really going to say I don't care what comes before.

I don't care what comes after, as well. As an AI is somewhere in a blob of text that is found in the last name column, then I should bring it back. And we can identify this by looking at the results that that at least three records should be returned. So let's just test that theory. And there we go. We got Macintosh, Williamson, and Williams.

So it just looked to see it didn't care if MC came before the eye. And you can notice that it is not case sensitive, as this is a common eye that I search for. And this is a capital I in the block of text. And it really didn't care what came after the AI as long as the block of text contained and I On the flip side, you can also say you want anything that begins with an AI, meaning it's going to see I am looking for an AI and I don't care what comes afterwards. Now based on the results that I don't think we'll get back any good We don't have any names that begin with AI. And in the same vein, we can actually see we want anything ending in AI by putting the wildcard character before the letter.

So I can say wildcard, but the last character should be an AI. And then we also should not get any results here as you don't have anything ending in I know this is not limited to a letter, we can actually once again, look for any character sequence. So our objective, according to this comment above here is to look for any name that contains the word students. So if we write wildcard, we don't care what mi come before this character sequence which spells the word students and we don't care what comes after it's. So if I execute this, then we should get back every students with the last name containing the word students. If I look for the car to sequence that spells the word Williams, then I should get back at least two results because we have one student with Williams, and one with Williamson.

And that is how you go about using that wildcard character. Now our next task involves us retrieving the full names of the students in our database and their respective enrollment dates. And I already drafted up the query, where we said select specify the columns that we wanted from and the table that we want them from. We discussed in a previous video, how we can go about making our column names a bit more presentable. What we're going to be discussing no is actually creating our own columns to represent our own data. Now, we didn't say full names.

Yes, the first name and the last name comprise a full name, but then they're still in two entirely different columns. I want one column that stores the full name. So I'm going to go about creating a column. And I do so I can actually, no SQL actually allows us to input any value that we want to be coming back with each record. So for instance, if I wanted to money whether print the number one, or I wanted to print the words, hello, class, or rather Hello students, as long as i comma separate, it's a one, so the last column, then I can actually do this. And then you see that we have the first name, the last name, random character that I put in just ones with no column name.

And this text that says Hello students, no column name and our enrollment dates. We discussed that if we wanted to give the column a specific name, we could just say as or just right on with square brackets, the name, so random number, and then I can execute this again. And we see that no, we actually get that column with that column name, random number. So this can actually be useful in certain situations. But based on our objective for this activity, we want a column that is storing full name of the students. So I'm going to what we call concatenate the string or join the strings together, or join the values in these two columns together to give us a full name.

So I can actually say something like first name, and then in SQL, we use the plus sign to say this columns value clause, that columns value. And because it's in between two commas, SQL is actually going to see whatever comes by here as one value, or one column for the results if so I'm just going to execute that. And we can see we get first name, last name and our custom column that we just put in that we did not give a name so no column name But then if we look at the values that are coming back, we see that we don't have any space, it's still it looks more chemical cased than than human readable. So we actually need to put in manually the space that we would expect. So if I say first name, and then concatenate an empty string, so I just opened a quotation mark, press the space bar, and close the quotation mark.

And then I'm concatenating. Once again, the last name, then we will see something that looks more reader friendly. So we have the first name to the left and the last name to the right. And once again, we still have no column name, so we can just add the column name, full name. And then because we have the full name, we can actually do away with the first name column and the last name column. So now we will have the full name and I'm going to give enrollment dates Pretty name, so enrollment dates and full name and then we execute.

And here we have full name and enrollment date. And you can always become as descriptive as you want. So you can see a student full name so that any reader looking at this report can know exactly who is listed to the left and what the dates are relating to them to the right is. So I'm just showing you how you can go about using certain SELECT statements to customize how you present your data. Okay, so up until now I have been focusing on the students now let us run some queries with our courses table. I didn't create any video creating content in the courses table.

I did however, give you a script that populates some records in your courses table and I hope that's in your experimentation you would have populated your courses table with some data so you have enough to play around with for these few exercises. Now, up until now, we have been focusing On our students table, I hope you would have populated your courses table foil you are experimenting. Either way, a strip file is attached to this video, which gives you some data to populate your courses table with so that at least these activities you can go through with some test data. So, we've been focusing on manipulating strings and n var char values. And now I want to just look at how we can go about filtering on number values. With number of values, we actually can carry out a numerical comparison was greater than less than we can look for equality.

And these two examples will bring us through some of those. So we want all the courses in our courses table with the number of credits being greater than two. And if you take a peek at the design of our courses table You'll recall that we are tied to number of credits which is an integer or numerical value and core score. So everything except number of credits, and the ID are invar chars. So we'll just quickly say select, and we want star from, and we have to fully qualify our table. And then we see where number of credits, which is our column is greater than two.

So I want all of the courses where the number of credits is greater than two. So if I execute this, I see that I get about four courses. And they all have values that are greater than two, I could easily have said equals two to find out if there is even a two credit course and if there were multiple, then we'll get back multiple. So I know that multiple three credit courses so I can just change it to three and execute Now we see we have all of the three credit courses. In contrast, I am going to select and I just copied this query down here. And I'm going to just modify it to say select courses with the number of credits three, and let's so we want anything with a number of credits, maximum three, but anything less than three is acceptable.

So what we did here was to just say less than or equal to three, the number of credits should be less than or equal to three. And then if we execute this, we should see our three credit courses as well as our two credit course. And if we had a one credit course it would also show up but nothing above three is appearing.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.