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.98
List Price:  €92.84
You save:  €27.85
£55.64
List Price:  £79.48
You save:  £23.84
CA$95.72
List Price:  CA$136.74
You save:  CA$41.02
A$105.61
List Price:  A$150.88
You save:  A$45.27
S$94.51
List Price:  S$135.02
You save:  S$40.51
HK$546.96
List Price:  HK$781.40
You save:  HK$234.44
CHF 63.39
List Price:  CHF 90.57
You save:  CHF 27.17
NOK kr759.29
List Price:  NOK kr1,084.75
You save:  NOK kr325.45
DKK kr484.72
List Price:  DKK kr692.49
You save:  DKK kr207.76
NZ$116.27
List Price:  NZ$166.11
You save:  NZ$49.83
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,668.91
List Price:  ৳10,956.06
You save:  ৳3,287.14
₹5,843.17
List Price:  ₹8,347.75
You save:  ₹2,504.57
RM331.68
List Price:  RM473.85
You save:  RM142.17
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,439.77
List Price:  ₨27,772.29
You save:  ₨8,332.52
฿2,571.29
List Price:  ฿3,673.43
You save:  ฿1,102.14
₺2,259.44
List Price:  ₺3,227.91
You save:  ₺968.47
B$355.04
List Price:  B$507.22
You save:  B$152.18
R1,290.39
List Price:  R1,843.50
You save:  R553.10
Лв127.12
List Price:  Лв181.60
You save:  Лв54.48
₩94,866.43
List Price:  ₩135,529.28
You save:  ₩40,662.85
₪261.89
List Price:  ₪374.14
You save:  ₪112.25
₱3,999.98
List Price:  ₱5,714.50
You save:  ₱1,714.52
¥10,762.93
List Price:  ¥15,376.28
You save:  ¥4,613.34
MX$1,185.03
List Price:  MX$1,692.97
You save:  MX$507.94
QR254.87
List Price:  QR364.12
You save:  QR109.24
P951.97
List Price:  P1,360.02
You save:  P408.04
KSh9,396.15
List Price:  KSh13,423.65
You save:  KSh4,027.50
E£3,355.96
List Price:  E£4,794.44
You save:  E£1,438.47
ብር4,011.08
List Price:  ብር5,730.37
You save:  ብር1,719.28
Kz58,466.37
List Price:  Kz83,526.97
You save:  Kz25,060.59
CLP$65,872.48
List Price:  CLP$94,107.58
You save:  CLP$28,235.10
CN¥495.49
List Price:  CN¥707.87
You save:  CN¥212.38
RD$4,060.47
List Price:  RD$5,800.92
You save:  RD$1,740.45
DA9,414.39
List Price:  DA13,449.71
You save:  DA4,035.31
FJ$157.07
List Price:  FJ$224.39
You save:  FJ$67.32
Q543.15
List Price:  Q775.96
You save:  Q232.81
GY$14,618.48
List Price:  GY$20,884.44
You save:  GY$6,265.96
ISK kr9,767.80
List Price:  ISK kr13,954.60
You save:  ISK kr4,186.80
DH704.80
List Price:  DH1,006.90
You save:  DH302.10
L1,237.78
List Price:  L1,768.33
You save:  L530.55
ден4,001.09
List Price:  ден5,716.08
You save:  ден1,714.99
MOP$562.43
List Price:  MOP$803.50
You save:  MOP$241.07
N$1,293.49
List Price:  N$1,847.92
You save:  N$554.43
C$2,572.34
List Price:  C$3,674.94
You save:  C$1,102.59
रु9,329.91
List Price:  रु13,329.02
You save:  रु3,999.10
S/260.85
List Price:  S/372.66
You save:  S/111.80
K270.11
List Price:  K385.89
You save:  K115.77
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,884.87
List Price:  ZK2,692.78
You save:  ZK807.91
L323.24
List Price:  L461.79
You save:  L138.55
Kč1,626.08
List Price:  Kč2,323.07
You save:  Kč696.99
Ft25,316.43
List Price:  Ft36,167.88
You save:  Ft10,851.45
SEK kr757.73
List Price:  SEK kr1,082.52
You save:  SEK kr324.79
ARS$61,503.14
List Price:  ARS$87,865.40
You save:  ARS$26,362.25
Bs483.90
List Price:  Bs691.31
You save:  Bs207.41
COP$271,944.94
List Price:  COP$388,509.43
You save:  COP$116,564.48
₡35,713.67
List Price:  ₡51,021.71
You save:  ₡15,308.04
L1,726.17
List Price:  L2,466.06
You save:  L739.89
₲523,116.65
List Price:  ₲747,341.53
You save:  ₲224,224.88
$U2,673.86
List Price:  $U3,819.97
You save:  $U1,146.10
zł281
List Price:  zł401.45
You save:  zł120.44
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.