Select Data with Filters

MySQL Database Development Mastery Manipulating Tables and Data - CRUD Operations
19 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
€67.28
List Price:  €96.12
You save:  €28.84
£55.84
List Price:  £79.78
You save:  £23.93
CA$100.66
List Price:  CA$143.81
You save:  CA$43.15
A$112.30
List Price:  A$160.44
You save:  A$48.13
S$95.06
List Price:  S$135.81
You save:  S$40.74
HK$543.65
List Price:  HK$776.68
You save:  HK$233.03
CHF 62.95
List Price:  CHF 89.94
You save:  CHF 26.98
NOK kr796.08
List Price:  NOK kr1,137.30
You save:  NOK kr341.22
DKK kr501.96
List Price:  DKK kr717.11
You save:  DKK kr215.15
NZ$124.09
List Price:  NZ$177.29
You save:  NZ$53.19
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,350.53
List Price:  ৳11,929.84
You save:  ৳3,579.31
₹5,966.48
List Price:  ₹8,523.91
You save:  ₹2,557.43
RM312.78
List Price:  RM446.85
You save:  RM134.07
₦108,176.54
List Price:  ₦154,544.54
You save:  ₦46,368
₨19,454.09
List Price:  ₨27,792.76
You save:  ₨8,338.66
฿2,392.25
List Price:  ฿3,417.65
You save:  ฿1,025.40
₺2,467.97
List Price:  ₺3,525.82
You save:  ₺1,057.85
B$472.19
List Price:  B$674.59
You save:  B$202.39
R1,303.83
List Price:  R1,862.70
You save:  R558.86
Лв131.65
List Price:  Лв188.08
You save:  Лв56.43
₩102,536.89
List Price:  ₩146,487.56
You save:  ₩43,950.66
₪256.46
List Price:  ₪366.39
You save:  ₪109.93
₱4,057
List Price:  ₱5,795.97
You save:  ₱1,738.96
¥11,019.26
List Price:  ¥15,742.47
You save:  ¥4,723.21
MX$1,411.83
List Price:  MX$2,016.98
You save:  MX$605.15
QR254.14
List Price:  QR363.08
You save:  QR108.93
P970.50
List Price:  P1,386.50
You save:  P415.99
KSh9,031.50
List Price:  KSh12,902.70
You save:  KSh3,871.20
E£3,562.42
List Price:  E£5,089.40
You save:  E£1,526.97
ብር8,897.26
List Price:  ብር12,710.92
You save:  ብር3,813.65
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,297.02
List Price:  CLP$99,000
You save:  CLP$29,702.97
CN¥510.85
List Price:  CN¥729.81
You save:  CN¥218.96
RD$4,256.60
List Price:  RD$6,081.12
You save:  RD$1,824.52
DA9,464.46
List Price:  DA13,521.24
You save:  DA4,056.78
FJ$162.28
List Price:  FJ$231.84
You save:  FJ$69.55
Q538.26
List Price:  Q768.97
You save:  Q230.71
GY$14,619.81
List Price:  GY$20,886.35
You save:  GY$6,266.53
ISK kr9,762.20
List Price:  ISK kr13,946.60
You save:  ISK kr4,184.40
DH704.68
List Price:  DH1,006.73
You save:  DH302.05
L1,286.35
List Price:  L1,837.72
You save:  L551.37
ден4,141.74
List Price:  ден5,917.03
You save:  ден1,775.28
MOP$559.01
List Price:  MOP$798.63
You save:  MOP$239.61
N$1,299.34
List Price:  N$1,856.28
You save:  N$556.93
C$2,571.30
List Price:  C$3,673.45
You save:  C$1,102.14
रु9,517.06
List Price:  रु13,596.38
You save:  रु4,079.32
S/260.20
List Price:  S/371.74
You save:  S/111.53
K283.61
List Price:  K405.18
You save:  K121.56
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,933.89
List Price:  ZK2,762.82
You save:  ZK828.92
L334.80
List Price:  L478.31
You save:  L143.50
Kč1,689.59
List Price:  Kč2,413.80
You save:  Kč724.21
Ft27,569.95
List Price:  Ft39,387.34
You save:  Ft11,817.38
SEK kr774.42
List Price:  SEK kr1,106.36
You save:  SEK kr331.94
ARS$71,763.27
List Price:  ARS$102,523.35
You save:  ARS$30,760.08
Bs482.86
List Price:  Bs689.84
You save:  Bs206.97
COP$309,145.61
List Price:  COP$441,655.52
You save:  COP$132,509.90
₡35,480.70
List Price:  ₡50,688.88
You save:  ₡15,208.18
L1,775.44
List Price:  L2,536.46
You save:  L761.01
₲544,980.94
List Price:  ₲778,577.57
You save:  ₲233,596.63
$U3,110.44
List Price:  $U4,443.67
You save:  $U1,333.23
zł287
List Price:  zł410.02
You save:  zł123.01
Already have an account? Log In

Transcript

Hey guys, in our previous video, we looked at selecting records using SQL statements that we wrote. In this video, we will be looking at how workbench generates some of these things for us. And also we'll be looking at more advanced queries in terms of custom columns and filters. So the first thing that we want to do when we want to see data from the table and expedite the process is just find the table over here in the schema listing, and then we just click this grid with a lightning bolt that is in the same row once the row is highlighted, and then that will actually generate for us a select star from statements which is a good place to start from, and we will get the queries that the results associated with that query. Now if you notice, instead of using the use statement like we would have done in our previous script This generated query actually says it uses dot notation to say, database dot table.

And that's just a standard thing that you'll see in most database management systems where it is seen database name, dot table name. So in essence, if we forgot to use school dB, and we had two school DVDs or two sets of databases with a table with a table called students, then what we could do is just say we want the students table from this specific database, and it will know that that is the context within which you should run that query. Alright, so I'm just going to comment that so it doesn't cause any errors. And that is the way that this generates it for us. So if you want to write it this way, then that's fine. And as we get into some more advanced spring, you'll see the value in writing it down.

Because then you may end up writing for cross databases. And this is a good way to know exactly which database. The table that is being referenced applies to know to explore some of the filtering exercises are requirements. I have included some activities here in the script file. And if we look at them, we see that we want to retrieve only records that meet a certain standard. And so we will have to amend our select query because I know the Select queries are scaring back every single thing that is in the table.

I also failed to mention that you can actually limit the rules so you can actually tell workbench that you don't want to limit or you want to limit to just 1000 or to 5000 so that when you click this button here, the subgrade with the lightning bolts, it will Actually limit according to what you want here. Now if we were to actually write that limit constraints on our query, then we would append it to the end of our table clause. So it's Select star from, and then we say database dot table, and then we would say limit and the number so I say two, then it would just give me the top two rows in the table. All right? If I say limit 10, then it will just give me the top 10. Okay, so that is the power of that limit keyword right there.

Now back to our task at hand, we want to retrieve only the students with the last name William, so I'm just going to remove the limit. And we take a look at our entire data set once again, and we see that we only have one students named Williams but Williams is a pretty prominence name. So you would probably have multiple students with the name Williams and for whatever reason, the Dean of discipline is asking you to give him all of the persons with the last name Williams, who are currently in the school. So no, you need to modify your SELECT statement to add a filter to this request for data. So to do this, what you would write is select and then remember that star really means all the columns or all the data points. So if it is that you want everything about the student, they can see star or you can only select the colors that you want.

So I'm just going to say last name on this and comma first, underscore name. And I only want the first and last name from the students table. So I could just write students but I didn't have the use statement up top so I just conform to the school DB dot notation syntax. And I'm going to say, dot students. And then a new keyword that we will look at is we're so we're seeing select these columns from this table in this database, where and then after Where is a condition or condition based on our requirements would be that the last name must be Williams. So I can see where the column last name has the value Williams and the easiest way to say has value Williams would be equals, and then I type whatever it should be equal to, and it should be equal to Williams.

Alright, so that is a new parts to our selection statement. So essentially, we see we're once again and then we specify our column And then what we expect so he can have different operators Siri can have equals, he can have less than we can have less than or greater than or greater than equals less than n equals, we can also have not equal to. All right. So, of course, the less than, greater than, and so on would apply to numbers as opposed to words. But then when we want to match a specific word, we could just use the column equals, and then some static text inside of quotation marks, which will be used to evaluate every last name and only bring back the records that meet this condition. So we can just go ahead and execute this as selected and executed by itself.

And there we go. So we're only bringing about the first name and last name of all students with the last name was And if we look at the entire data sets, then we can determine or deduce that that is actually accurate. All right. So what if I wanted the students with the last name test? So we see that we have one with test, but we have many variations of tests. And I just want to ask you to ask yourself right now, how many rules would come back if I specified that the last name should be equal to test.

So if I execute this, I only get back one rule. Now you may have said more than one rules, or you may have said as many rules as there is a word test in the last name, and you would have been wrong because test one is not the same as test this is saying give me We're all of them are equal to test. So any variation of the word test will not be brought back. So that brings us to our next activity. We're We're going to retrieve only students with the word test in their last name. Now, we just attempted something like that.

I'm just going to copy this query and paste it down here, and we'll just modify it accordingly. No, let's take a look at the entire data set once again, and we see that all of these from one through nine, IDs one through nine have the word test in their name. So that means whatever query we write for this requirement should bring back IDs one through nine, not bringing back 20 through 22. So then we need to modify or conditioned capture exactly what our requirement is. And to do that, we will use our keyword called like, no like allows us to specify what we call a wild card, and much a pattern as opposed to an A how and accept much. So in more simple terms, when we say equal, it will look for an exact match.

So let me just put back Williams. If the name, if the column doesn't exactly match this value, then this condition fails. So that is what the equal sign does. However, we want a partial match, we want anything that has the word test in it. And so we need to change from using any sign to using the keyword like and then when we have the keyword like inside of the string, we can specify what we call a wild card, which is represented by a modulus or percentage sign. All right.

So this is how modulus works. It will see give me anything that is before this or anything that is after this. And I'm going to start small before we actually fulfill this requirement. So let's say I wanted a name with the letter E, any record with the letter E in it, that means I should bring back every test. And also this rule 20 with read, which also has an E. So I can see, give me these columns from this table where this column, which is last name has something like an E. And to do that I'm going to say modulus, which means I don't care what comes before an E. and I were to say modular, so again, because I don't care what comes after the E. So that's what the model is really does. It says I don't really care what comes before this pattern.

And I don't really care what comes after this pattern, as long as this pattern is met. In this case, the pattern is just one letter, which is he. So if I execute this, then it will sift through and find out The last names in it. And that's why read came back. So let's try that with the letter I, if I said give me about all the last names of the letter I in it, then only Williams and read combat because there's an iron read and there's there's at least one I mean, William. So let's move away from letters What if I wanted anything that had the pattern l in it so any column that has two L's in it, I don't care what comes before, ELLs, I don't care what comes after the L so give me anything with L's in it.

And of course, you should only get back all small, I forgot that smallest there. So we're getting back small because small has something before the L but it has an L and just the same for Williams. It doesn't care what's in front of the LR after it as long as there's an L present. In the same way that we can actually match patterns inside of the string. We can actually use this to specify if the word starts or ends with a letter. So let's see, I wanted any name, last name that ends with the letter.

It's alright, so I will just say models, it's so that means I don't care what comes before. This give me anything that ends with an S. All right. And then if I execute that, I should only get back, Williams. And then if I see I want anything that begins with an S, I can see s and then Model S. And I hope you notice by know that it is not case sensitive, because I'm searching using common letters, and actually getting back values with capital letters that meet their criteria. So the evening executing the query and searching with a static string is not very case sensitive in my SQL, but when we're selecting here, and we just want to see any name that starts With s, then we see that letter or that sequence, if I wanted s m, or SNP B, whatever it is, what this is saying is that give me any sequence that matches this, regardless of what comes afterwards.

All right, so I want any pattern, any last name that has an S, regardless of what comes after the s, ri, but we're not putting the wildcard before because we want it to start with an S. And when I do that, we get back small. If I did that with t, then we get back all of the test student records in the database. All right. So actually, this would have kind of fulfilled this requirement to retrieve all the students with the word test, but then this would not still not work because then if I had somebody with the last name, field or something like that, with Tea, Thomas pharmacy, then that person would come back and that would void the results. So once again, I can wrap the expression inside of wildcards. And I can just write out the whole world, the whole word test to see, I don't care what comes before the word test.

And I don't care what ops comes after the word test. I just want back all the records with the word test in it. So I do that and then execute and the results it stays fairly the same. But if there was some other record coming back before, no, it would be sifted out. And that is essentially how wildcards work. Now, our next requirement tells us that we want about the full name and the enrollment date for all the students.

Now you'd be wondering, why am I specifying full name, because I'm already bringing back the full name here. But then notice that last name, and first Those are two entirely different columns, we want back one column with just the full name. Some, we'll just start off with the basic Select star from and just copy this code. And when I get these queries, I like to do what I call a process of elimination, where I start off with all of the data, all of the potential columns, and then one by one I sift through and take out what I don't need. So I know I need the last underscore name and first underscore name to make full name. And I also want the enrollment.

It's alright. And so when I execute this, I'm getting about the three columns that I want. But then once again, I'm not getting back a full name here, and I really want to rename those columns. Now to accomplish sprinting up first Name and Last Name in one column, we have to employ what we call one of the string manipulation functions or knee to string functions of my SQL. And that comes in the form of Han cat concat is short for concatenate, which is the process by which we join two words are blocks of text together in general programming terms. so in this situation, what I'm going to do is write the expression on contracts and they're auto completed for me, but then inside of the parentheses, I need to specify the strings to be concatenated.

So I can see I want to concatenate first name Alright, and then I can see last name. Alright, and then as you as we discussed before, one size, specify the column, I can always rename it. So I can just say full name. So that we know that this is the full name. And then when I select this, and this is not going to come out very cleanly, so we'll just refine it as we go along. So when I execute this, then you see that full name is being printed, but then you look at it and you see that it's chopped up.

Alright, so we need a space in between the Beverly and the small and the student is anticipated. So with the Concord, you actually have to specify every single space, in addition to the values, so because we have this column here, and this column here, it's just crunching them together so we can just crunch another space in between. So I can see space, open quotation mark, spacebar, and then comma. So what that's going to do is join the first name to us face to the last name, and then our end result will be A full name that is far more user friendly user readable students space tests traverse space Williams, and that is how our data would look. And once again, our column is named appropriately so we can just go ahead and rename enrollment date to ensure that everything looks uniform. The next two activities are based on the courses table.

So by now you should have inserted some records into the courses table. And, you know, we'll be tasked with selecting those records with some filters. Alright, so you can always just put in some test data that would kind of help you to fulfill these requirements set up here for you and leave you two hints as to why You can use to fulfill these. So when you're writing the filter, you'd want to see where credits column. And I'm not, I'm being very vague here, because I want you to actually give that practice is greater than three. And in the same way, where credits column is less than or equal to three, I'm sorry, this would be two, based on the question being asked, right?

So it's where it's greater than two, as outlined here, and where it is less than or equal to three. So you can go ahead and complete that I will upload this file with the solution, but I implore you to practice it on your own.

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.