 Hello everybody, in this lesson we're going to be learning about the select statement in MySQL. The select statement is used to work with columns and specify what columns you want to see in your output. The first thing that we need to do is open up a tab or an editor window. So let's come right up here to the left hand side and we're going to create a new tab and I'm going to zoom in just a little. Now what we need to do is we need to select the actual table that we're going to be querying off of. If you remember from the very first lesson when we set everything up, we came over here and we right clicked and did select rows limit 1000. We're not going to do that. We're going to actually write it out. So what we need to do to select that table, the employee demographics table, is we need to select everything. That's what the star means. The star means everything. All tables, all rows. Now we do have a limit on here. We have a limit to 1000 rows. So if we had a table that had 50,000 rows, this limiter would be an issue. It would still limit it to 1000 rows. We would have to change that to 2000, 5000, probably all the way up to 50,000 if we wanted to view everything. If we had say a million rows, we would need to come up here and say don't limit and it would give us a million rows. The reason they do this is mostly to keep the processing time low. If you have a million rows, it's going to take a long time for the output to actually appear. So let's come right back here. The next thing that we need to do is we need to say select everything and now we need to say where we're selecting it from. So we're going to come right down here and we're going to say from and now we need to specify what table and we're going to say employee underscore demographics and at the end we need a semicolon. Now why do we need a semicolon? This is going to tell my SQL that this is the end of this query. So if you write another one down here, which we will in just a second, it'll be able to distinguish between the two queries. We're going to go ahead and we're going to run this and we'll just use this execute right here instead of this one. And there we have our entire table. So we were able to get our table. Now there is one thing that is potentially wrong depending on what you're using it for, but what we didn't do is we did not specify the actual database before it. We only specified the table. And this works perfectly fine because if you look over here on this left hand side, we have parks and recreation. It's in black. It's bold. That means that we're hitting off of this database. What's going to happen though, if we come down here to the sys database and we double click on it. Now this database is highlighted. So now when we're selecting this table, we're trying to select this table from the sys database. Let's go ahead and try this. If you notice, we have no output. Let's come right down here and pull this up. It's going to say employee sys dot employee underscore demographics doesn't exist. So it's assuming that we're highlighting this sys database. That means we're trying to pull from that database. Now we can still have this highlighted and still select the correct database by saying parks underscore and underscore recreation. Okay, let me spell that right. Dot. So now we're selecting everything from parks and recreation dot employee demographics. If we run this, we do get the correct output. That's just something to consider, especially when you're working with a lot of databases and a lot of tables. It's usually best practice to actually put the database in front of the table name. Although throughout this lesson, we probably won't be doing that every time since we're only going to be using this parks and recreation database. Let's go ahead and double click this. So we have this highlighted again. And let's click all of this. Let's copy all of this. We're going to come down just a little bit right here. Now so far we've only selected everything, but we don't have to do that. We can actually just select one column if we would like to. For example, if we got rid of that star, we say first underscore name, we're selecting the first name column from this table. If we highlight this query, and we hit the execute button with the I, now we are only going to return in our output, all of the first names. And we can add a lot more. Let's actually look at all these. We can separate multiple columns with a comma. So we can do first name comma last underscore name. And then we could do birth underscore date. So now we have three separate columns. Let's go ahead and run this. Now we have first name, last name, and birth date in our output. Now the way we just wrote it is all on one line. And that's perfectly acceptable because MySQL is going to read it the exact same as if we did it in a different format, as long as it's still in this order. But sometimes you'll see it like this, where it's select first name comma last name comma birth date, all on different rows. Now there's a lot of different use cases for this or reasons for this, but it typically can be easier to read. Also if you're doing any type of functions or calculations in the select statement, it's easier to separate those out on its individual row. Now again, we won't always be doing this, but it does help. Sometimes if you're doing that, it just makes it easier to visualize. For example, if we added the age, so let's add age in here, let's run this. Let's say we were doing a calculation where we wanted to add, you know, 10 years to their age. So we'll say age and we'll actually create a new row for this or new column. We'll do age plus 10. So now we can easily see that we're doing plus 10 here. And this is another thing that you can do in the select statement, things like calculations. So if we go up here and we run this, we'll now have an age column, but we'll also have an age plus 10 column where it just adds 10 to the age. And we can at least visualize and really easily see this when we're doing these calculations. Now something really important to know about any type of calculations, any math within my SQL is that it follows the rules of PEMDAS. Now PEMDAS is written like this, it's PMDES. Now what I just did right here with this pound or this hashtag is actually create a comment. So this code isn't going to actually run, but it's just for note taking or seeing things in your actual editor window. I'll come back to comments in just a second, but just wanted to explain what that was. Now what PEMDAS is, is the order of operations for arithmetic or math within my SQL. This stands for parentheses, exponent, multiplication, division, addition and subtraction. So this is the order that these calculations are going to run in the execution engine that my SQL has. So if I do age plus 10, and we'll put that all in parentheses, and then we come over here and we add times 10. So we're doing plus 10 here and then a times 10 here. What's going to actually happen is it's going to say age plus 10. So 44 plus 10 equals 54, then we're multiplying times 10. The parentheses executes first because it comes first in this order, parentheses. Multiplication comes next because it's this one. And then anything else after that, if we did plus 10 could run this. And you'll notice that it still follows the logic, 10 was just added at the very end to all of these outputs. Now let's go right back up here. Let's select everything again from this table. Let's pull up this table so we can see it a little better. And let's go down. Because the last thing that I want to show you is something called distinct. Now this is really, really useful and you use this a lot in my SQL. What distinct is going to do is it's going to select only the unique values within a column. Let's go ahead and copy this employee demographics, bring it right down here. Let's say select, let's do first underscore name. So now we're just selecting the first name. Let's come right down here. There we go. So now we're selecting just the first name from this column. Now these are all unique values. So if we come right here and we say distinct, nothing should happen to this table because these are all unique values. Let's go ahead and run this. As you can see, the output looks exactly the same. But what if we reduce something like gender? So let's come here. Let's do gender. Let's run this. Keeps going down. I don't know why it's doing that. But now we have male and female. Now these are not all unique. We have female, female, female and female. The rest are males. So there's only two unique values here. So if we come right here, we say distinct, gender. Now there should only be two in the output, male and female. Let's go ahead and run this. So now we get male and female in our output. Now this works perfectly in one column, but what happens if we have two columns? So let's do first underscore name comma gender. Let's go ahead and run this. Now the combination of first name and gender are no longer unique. Now Leslie and female are being grouped together, and it's taking the distinct between both of these columns. So when we're only working with gender, it's only looking at this one column for both male and female. So it reduces it down to the only two unique values. But because we added the first name, all of these values are unique. So therefore the name plus the gender combination is always going to be unique. The very last thing that I want to show you in this lesson doesn't actually pertain to the select statement, but I want to save this code. Let's say we wanted to update this or upload this into our GitHub or save this and send it to somebody. We can do that. We can save it by clicking this save button right here. I'm going to go ahead and click this and now we're in our MySQL beginner series folder. I'm just going to save this and I can save this as anything I want. So I'm going to say two dot select statement tutorial. So now when I save this, you'll notice that the name gets changed up here to two dot select statement tutorial. Let's exit out of this. I'm going to open up and now I'm going to come here to the select statement tutorial. I'm going to open it and now I have our code again, exactly as we had it written before. I just wanted to show that to you in case you wanted to save your code as you go throughout this series because that's usually what I do when I'm working with this stuff or learning these things. I like to save my code as I go along. So with that being said, that is the end of the select statement. In the next lesson, we're going to be learning about the where statement where we can actually filter our data.