Select Data from the Table with SQL Script

Microsoft SQL Server 2017 for Everyone Manipulating Tables and Data
9 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.52
List Price:  €92.18
You save:  €27.65
£54.91
List Price:  £78.45
You save:  £23.53
CA$95.63
List Price:  CA$136.63
You save:  CA$40.99
A$105.46
List Price:  A$150.66
You save:  A$45.20
S$94.47
List Price:  S$134.97
You save:  S$40.49
HK$546.75
List Price:  HK$781.10
You save:  HK$234.35
CHF 63.92
List Price:  CHF 91.33
You save:  CHF 27.40
NOK kr736.32
List Price:  NOK kr1,051.93
You save:  NOK kr315.61
DKK kr481.39
List Price:  DKK kr687.74
You save:  DKK kr206.34
NZ$114.12
List Price:  NZ$163.04
You save:  NZ$48.91
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,234.07
List Price:  ৳11,763.47
You save:  ৳3,529.39
₹5,834.74
List Price:  ₹8,335.70
You save:  ₹2,500.96
RM329.19
List Price:  RM470.30
You save:  RM141.10
₦99,907.92
List Price:  ₦142,731.72
You save:  ₦42,823.80
₨19,527.07
List Price:  ₨27,897.02
You save:  ₨8,369.94
฿2,572.84
List Price:  ฿3,675.65
You save:  ฿1,102.80
₺2,256.45
List Price:  ₺3,223.64
You save:  ₺967.19
B$361.19
List Price:  B$516.01
You save:  B$154.82
R1,279.07
List Price:  R1,827.32
You save:  R548.25
Лв126.17
List Price:  Лв180.25
You save:  Лв54.08
₩95,644.21
List Price:  ₩136,640.44
You save:  ₩40,996.23
₪258.47
List Price:  ₪369.26
You save:  ₪110.79
₱4,093.36
List Price:  ₱5,847.91
You save:  ₱1,754.54
¥11,004.79
List Price:  ¥15,721.81
You save:  ¥4,717.01
MX$1,172.36
List Price:  MX$1,674.88
You save:  MX$502.51
QR255.99
List Price:  QR365.72
You save:  QR109.72
P952.66
List Price:  P1,361
You save:  P408.34
KSh9,238.68
List Price:  KSh13,198.68
You save:  KSh3,960
E£3,310.57
List Price:  E£4,729.59
You save:  E£1,419.01
ብር4,031.68
List Price:  ብር5,759.79
You save:  ብር1,728.10
Kz59,697.73
List Price:  Kz85,286.13
You save:  Kz25,588.40
CLP$62,737.63
List Price:  CLP$89,629.03
You save:  CLP$26,891.40
CN¥497.79
List Price:  CN¥711.16
You save:  CN¥213.37
RD$4,135.10
List Price:  RD$5,907.53
You save:  RD$1,772.43
DA9,419.68
List Price:  DA13,457.26
You save:  DA4,037.58
FJ$158.10
List Price:  FJ$225.87
You save:  FJ$67.76
Q544.93
List Price:  Q778.50
You save:  Q233.57
GY$14,687.95
List Price:  GY$20,983.69
You save:  GY$6,295.73
ISK kr9,608.22
List Price:  ISK kr13,726.62
You save:  ISK kr4,118.40
DH697.14
List Price:  DH995.96
You save:  DH298.82
L1,237.78
List Price:  L1,768.33
You save:  L530.55
ден3,973.37
List Price:  ден5,676.49
You save:  ден1,703.11
MOP$564.20
List Price:  MOP$806.04
You save:  MOP$241.83
N$1,290.11
List Price:  N$1,843.10
You save:  N$552.98
C$2,582.58
List Price:  C$3,689.56
You save:  C$1,106.97
रु9,338.57
List Price:  रु13,341.39
You save:  रु4,002.81
S/263.01
List Price:  S/375.75
You save:  S/112.73
K272.83
List Price:  K389.78
You save:  K116.94
SAR262.51
List Price:  SAR375.03
You save:  SAR112.52
ZK1,889.24
List Price:  ZK2,699.03
You save:  ZK809.79
L321.07
List Price:  L458.70
You save:  L137.62
Kč1,594.02
List Price:  Kč2,277.26
You save:  Kč683.24
Ft24,941.11
List Price:  Ft35,631.69
You save:  Ft10,690.57
SEK kr742.57
List Price:  SEK kr1,060.86
You save:  SEK kr318.29
ARS$62,535.78
List Price:  ARS$89,340.66
You save:  ARS$26,804.88
Bs484.70
List Price:  Bs692.46
You save:  Bs207.76
COP$271,654.26
List Price:  COP$388,094.15
You save:  COP$116,439.89
₡36,081.33
List Price:  ₡51,546.97
You save:  ₡15,465.64
L1,733.89
List Price:  L2,477.09
You save:  L743.20
₲528,334.84
List Price:  ₲754,796.40
You save:  ₲226,461.56
$U2,699.12
List Price:  $U3,856.05
You save:  $U1,156.93
zł274.62
List Price:  zł392.34
You save:  zł117.71
Already have an account? Log In

Transcript

Is In this video, we will start looking at how we can go about extracting data from our tables. In the previous video, we were inserting records and we'll be inserted up to probably about 10 or so records. And now we want to actually start pulling them back and manipulating the data. So we have our scripts page open. And we know that the first thing that we do is to specify which database it is that we're about to run the script against. I want to know how to show you that you have the use keyword where you say use and then you give it the database name.

And since we're using the editor, I'm going to show you that this drop down up here that says monster, and it will say monster because monster, which is a system database, is the default database that is going to be selected for any script. We can actually use this drop down box and we will see all the databases listed there. So if we click school, then it changes the context of our query without us having to write us school. So you can use either one. Personally, I prefer to write use school because if it goes on another machine, and then by default, it goes to the master, then it knows when the script executes, it uses school, as opposed to leaving it up to memory to change this when you're about to execute the script, and then you may end up executing a possibly destructive script against the wrong database.

So be mindful of that. But of course, for this course, I want you to be very versatile. So you can use either method somewhere just stick to the evil I know and use school and to get the IntelliSense you can press control and space, but you probably know that already since you've gone through all the keyboard shortcuts, and you will see that it actually gives you all this additions and helps you to complete certain statements faster. No today will be Looking at the SELECT statements, so we've gone through create, we've gone through insert into and know we're looking at select and select is doing exactly what the name suggests it's selecting the data from a table. And that is exactly what the query line looks like, select. And then what data do you want?

Do you want the first name? Do you want the last name? Or do you want everything in the table. So if you want specific columns, you can write the modes. In this situation, we want all columns, so they give us a shortcut for all in the form of an asterisk, which allows us to see select all so the asterisk is synonymous with all and that means all columns from and then we specify the table and we want students that is how we're going to give all of the student records in our students table after execution. In the query, we see here that our results have come back.

And everything is, as we saw in our previous video. However, in this view, we're not able to click in and edit any of these records. So the Select is really just to read the beta. So when you run a select command, you're just reading the data for a presentation made before reports, it's just giving you in a grid format. And you can actually change that the options are here to change the results the file or to change the results and things so I can just pick that and then re execute the query and then just show you the difference in the results. So you actually get it in text form instead of a table format, and then file would actually export it to an RPT file, so we can just leave it on grid as the most that's the most common and that's the default one and we execute and we see it in grid format.

Once again, it is not editable. Now let's start looking at some variations. of this SELECT statements. So I'm just going to remove this one. And then what if I wanted all the students, I only wanted first names. And last names, I didn't want to see the ID that's auto generated, I didn't want to see their date of birth or the enrollment date.

I just wanted to see first name and last name. So I would still use select. And in this occasion, I don't want to see all the columns, I just want two columns. So I want first name. And then i comma separates all of the columns that I'm specifying. So first name, comma, last name, and then we see from students, and then all of those errors go away because now it knows that first name and last name from from students to execute once again, and we see that our results set not only reflects those two columns, first name, and last name, though, if we're actually pulling these data points for reports, and I'm just Going to add enrollment beats to this.

So the enrollment dates and we want to see all of the students and their enrollment beats. But then we want to export this to excel. And we want sensible looking column names. First Name is not, you know, reader friendly. It has a common F and come on and it's one word first name is not one word last name is not one word. But because we're discouraged from using spaces.

In creating our column names, we didn't write first space name, but when we print it out in our grid, we actually want the column names to look more presentable. So what we can do is say, as and then we can write the string that we would like to be printed for the column name. You actually can write it without the as and using the square bracket, just see last name, so I'm doing it both ways so you can see all of your options. So the as keyword is actually optional. You can Put it here. All right, and then enrollment dates like we discussed could look kind of convoluted and confusing.

So I'm just going to bring that to the next line. And I'll say and roll meant eight. And then we execute this. And then if we look at the headings will see first space name, last space name, and enrollment date all looking more presentable and human readable. Once again, we can always break our lines because SQL will know where statement starts on end. So if you need to do this, just to see everything more clearly, you have a keyword column and it's alias.

This is what we'll call an alias comma column alias called column alias. And then that's one of the advantages of that square bracket because if it is a case where you inadvertently created a column with a space in the name, then you could actually use the square bracket to still operate, but then that's not good practice. I don't like doing it and I'm not recommending it. So just treat your your column names like this either use one word like this in all lowercase, use your underscore to separate the names, the words or camelcase them. But then ultimately for presentation, you will want to use the aliases so that your columns come out looking better. So let's say you have to run this report your administrators saying hey, I need this report, you print it, you put on all the aliases, and then you need it in Excel, you can actually just right click here in the corner.

So by clicking in the corner, it actually selects all the data and then you can just right click, and you can copy and it will copy it in tabular format and you can paste it in Excel or you can actually see the results as and the most common thing is CSV, which can be opened in Excel anyway. So that's how you would go about it. exporting an instruction from the database to a file for reporting purposes. So let's try another thing. And you can actually run multiple selects in one script, what it will do is separates the results. So I want to say select star from courses.

I didn't enter any courses. Not sure if you did, but when I execute this, I'm expecting to see something saying there's no record in the database. So you see here, when if you and you have more multiple selects, you will see the different sections for each grid results. So we see up here, that's our first select. And then down here, you see the results from our second selects, which we have nothing and also take note of the fact that all of the column names are coming back as they were created. So by using this star, you're you're not afforded the opportunity to set some aliases, so you probably want to spill over Your columns and set is for when you're going to be exporting the data for another human being to read.

So when we get back we'll start looking at more advanced extraction techniques and how we can go about filtering and selecting only some records and excluding others.

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.