Let's begin by going through some simple SQL to read or retrieve information from our database. I am going to open up SQL Server Management Studio and connect to my instance here. After it's open, the button that I am going to click is this one, the New Query button on the toolbar. A query, well, that's the Q in SQL. It's Structured Query Language. We write queries and this one will use the most common piece of SQL, the SELECT statement. We are selecting or choosing information from one of the tables in one of our databases.
So I am going to write this phrase to begin with SELECT *, using the asterisk, From. I want to get everything from somewhere. Well, there's the question. Which database? Which table? Because in SQL Server Management Studio I can see that I have got quite a few databases, even the system databases. And I could be writing a select statement that goes against any of them. So I am first going to have to say which database I'm coming from. So I'm going to say I want to use AdventureWorksLT. So I will type A and I immediately get this IntelliSense, this auto complete that pops up which is great.
I am going to come down with my cursor key to AdventureWorksLT and that's the one that I am going to use, the LT version, then do a dot, and then instead of getting directly to the tables, I am getting to the schemas and roles here. I will explain that in just a second. But for the moment, I will select SalesLT., come down to Customer. This is actually a complete SQL query. I don't need to finish it with a semicolon or anything like that. I can actually go ahead and run or execute this either by clicking this button that says Execute or I could hit the F5 key on my keyboard.
It would do the same thing. Down here it says Query executed successfully. It took 847 rows of data out of the database and it's now showing them to me. It is just a select, so you're not going to find these are editable. We are just reading this information. By convention, you'll see that the SQL keywords such as Select and Insert and Update and From, and we will see quite a few of them, are written in uppercase. It actually doesn't matter. SQL Server Management Studio and even SQL Server is not going to care.
I could just as easily write select in all lowercase and it would work as well. But by convention of many, many years you tend to see SQL statements written with the keywords in uppercase. If we were going to write a lot of SQL statements, it would get a little tedious. You'd have to write the name of the database again and again and again, because typically you are going against the same database all the time. But I do have to say which database I'm interested in. There's a couple of ways to do it. I can put it in the From part to say explicitly what table and what scheme and what database.
Another way that I could do it if I was going to write many SQL statements is before the first one, I just have the phrase Use AdventureWorksLT. This would do exactly the same thing. If I hit Execute again, I will see the same results. Another way that I could do it, if you notice that this Use AdventureWorksLT, we've also got it popping up here. This is the equivalent. It's actually selecting the database that we are interested in right now. So, I can also get rid of that and just make sure that the correct database is showing up in SQL Server Management Studio.
Execute and there we go. It's a common problem for beginners that you're writing a piece of SQL and you think it's correct, but without you noticing it, the wrong database is highlighted. Say master is the very common default database and when you execute, it says "I have no idea what this SalesLT.Customer is." So I will make sure I'm on the right one. Now I am using the SELECT * here. SELECT * just means select everything.
Give me every column in this particular table. While that's nice and convenient when you're in SQL Server Management Studio, you probably don't want to end up doing that. Because when you're writing say an application, of course the database may change. So you want to be very specific about the columns you are going to retrieve. All I can see that I have things like FirstName, MiddleName, LastName, SalesPerson, his EmailAddress is here. So I can instead of using the asterisks, I can say I would like FirstName. And you notice I have IntelliSense there. Comma, LastName, comma, EmailAddress From SalesLT Customer.
I can break this up onto another line as well. This will work just fine. If I hit Execute now, then I get a filtered amount of data. This is what you want to do as a general rule unless you are writing really casual SQL statements. Be specific about the columns that you're interested in. Now, if we were writing this SQL because, for example, we were going to be printing this information on a report, we might even want to combine the FirstName and LastName. Even though they are stored separately in the database, which is a really good way of filtering and sorting things, combining them there is a way we are going to write them out in a report or an email.
Well, we can combine them on the fly within SQL. I can actually instead of using the comma here, I could use the plus sign to concatenate or join these two together. Now, if I just use the plus sign, we are going to have a problem. If I hit Execute, what I am going to see is we are combining or concatenating these two together into this and it says, "Well, there's no column name." It is no FirstName or LastName. It's kind of something in-between. Not only that, but we have the FirstName and LastName squished up against each other.
Well, we'll fix that in two ways. One is I am going to say that rather than combine FirstName and LastName directly together, I'm going to combine them with a space in the middle. Now, when we are doing a space or a string, we use the single quotes to separate it. So I am doing single quote, space, single quote. Add FirstName, then a space, add LastName. We will execute that and that looks a little better in the values that we are getting back, but it still says No column name. It probably be useful to have a kind of madeup or constructed column name here.
The way we do it is this. We are saying I want to select FirstName and space and LastName AS FullName. There isn't a FullName column in that table. So not a problem there. But we can make it seem like there is. So I execute that and now it seems like we are retrieving the FullName column and the EmailAddress. So what we've seen very quickly is just by using a few little key phrases, we can start to get very flexible about the information that we're retrieving. Now, right now I'm bringing back 847 rows. That might not be what I want.
We are going to go ahead and start adding on to this simple SELECT statement where we are saying select the data that we want from the table that we are interested in. But we need to move on and make sure that we are only retrieving the data in the right circumstance or in the right order or when certain conditions are true. So we are going to build on that next.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 73572 Viewers
80 Video lessons · 129279 Viewers
52 Video lessons · 63653 Viewers
59 Video lessons · 49415 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.