Focus video player for keyboard shortcuts
Auto
- 720p
- 540p
- 360p
1.00x
cc
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
Here we will start to write SQL statements and queries, starting with basic SELECT statements
Exercise files
Download this lesson’s related exercise files.
Simple SELECT Statements58.7 KB Simple SELECT Statements - Solution
58.9 KB
Quick reference
Simple SELECT Statements
Select statements are the main structure of SQL
When to use
Anytime you want to use SQL, you'll likely use a Select Statement.
Instructions
Select statements select data from our database to display.
- To use T-SQL in SSMS, right click on our table in the object explorer, and choose "Select Top 1000 Rows"
- Type in your SQL. To run your SQL Commands, click the Execute button.
- The select statement is the following:
SELECT [COLUMN NAME 1], [COLUMN NAME 2], [COLUMN NAME 3]
FROM [DATABASE NAME], [DATABASE TABLE]
Hints & tips
- To use T-SQL right click on our table in the object explorer, and choose "Select Top 1000 Rows"
- To run your SQL Commands, click the Execute button.
- 00:05 We've installed SQL Server 2016, we've created our database, we've created our
- 00:09 table, we've put some data in it, we've saved a backup, we've restored it.
- 00:13 Now, we're gonna talk about SQL and SQL statements, and pretty much,
- 00:18 the rest of the course, we're just gonna focus on different SQL items.
- 00:21 When it comes to SQL the main
- 00:24 thing that you're gonna be working with is something called a SELECT statement.
- 00:27 And that's what we're gonna talk about in this video,
- 00:29 the SELECT statement is the structure of all of it.
- 00:33 Everything you do is gonna build on your SELECT statement,
- 00:36 so in this video we're just gonna create a very simple SELECT statement.
- 00:40 And then in the following videos we'll build on that statement to
- 00:43 do more advanced things, more intricate things.
- 00:46 And what we're talking about here is getting data out of our database, basically.
- 00:49 So we've got a database, now how do we pull information out of there?
- 00:54 Well, we can just look at it and we can see we've got these six rows and
- 00:58 I might add some more a little bit later just to flesh this out a little bit more.
- 01:01 But normally, you're gonna have thousands of rows, hundreds of thousands,
- 01:05 tens of thousands, maybe millions of rows, so you can't just eyeball this thing.
- 01:08 You need some way to pull specific data out, and that's what we use SQL for.
- 01:13 So to use SQL in Microsoft SQL Server 2016,
- 01:16 we're just gonna come to our database down here and highlight our table.
- 01:21 And then right-click and I'm gonna click Select Top 1000 Rows.
- 01:27 Now this will allow us to do a T-SQL which is Microsoft's SQL of choice here and
- 01:32 this allows us to write SQL on this little panel right here.
- 01:37 And we have a SQL statement that they've already done for
- 01:40 us just by clicking on that.
- 01:42 Select Top 1000 rows there, and
- 01:44 what it's done is selected the top 1000 rows, here is the output, right?
- 01:50 And we can see we can't change any of this data, this is just for
- 01:54 visual purposes, we could just look at this here.
- 01:56 But before we get into that I'm gonna delete all of these stuff and
- 02:00 we're gonna start from scratch here.
- 02:02 A SELECT statement starts with the word select and
- 02:05 then you have to tell the program what you wanna select.
- 02:08 The format is, list all the columns that you want separated by commas,
- 02:13 each one is put in these straight brackets, right?
- 02:17 So if we wanted to select every first name,
- 02:21 that's one of our columns, first name.
- 02:23 And as we type, you'll notice this fills it in for us if we click that, we might
- 02:28 want the last name and again, we can do that, and let's say we want the age.
- 02:37 Each of these are columns, they're separated by commas,
- 02:40 then the second line here is from.
- 02:43 Where are we pulling this stuff from or
- 02:45 we're pulling it from our customer database, that's this guy right here.
- 02:49 And specifically, this table which is our table, DBO.current_customers.
- 02:55 So here's our statement, we're saying, select the first name, the last name and
- 03:00 the age from our current customers table and our customers database.
- 03:05 To run this, to execute it, you come up here to this little Execute button and
- 03:09 you just click it, and you see down here, it changes to exactly what we want.
- 03:12 So we have John Elder, age 39, first name, last name, and age, which
- 03:16 of course corresponds to first name, last name, and age up there, and that's it.
- 03:21 Very simple. We think of SQL as a programming language,
- 03:25 it's not really a programming language,
- 03:27 it sort of is, but it's really easy, this is it, this is very easy.
- 03:32 Here's another quick tip before we end this video, we designated first name,
- 03:36 last name and age, we can designate everything by using the star.
- 03:39 And if we come over here to execute, then again we get everything in our table,
- 03:43 so that's a neat thing to remember.
- 03:46 So that is the simple SELECT statement, very, very simple.
- 03:49 So in the next video, we're gonna talk about the WHERE clause,
- 03:52 it will allow us to pull data in a more specific manner.
Lesson notes are only available for subscribers.