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.99
List Price:  €92.84
You save:  €27.85
£55.80
List Price:  £79.72
You save:  £23.91
CA$95.74
List Price:  CA$136.78
You save:  CA$41.04
A$105.97
List Price:  A$151.39
You save:  A$45.42
S$94.71
List Price:  S$135.31
You save:  S$40.59
HK$547.38
List Price:  HK$782
You save:  HK$234.62
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr760.18
List Price:  NOK kr1,086.02
You save:  NOK kr325.83
DKK kr484.74
List Price:  DKK kr692.51
You save:  DKK kr207.77
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.15
List Price:  ৳10,970.69
You save:  ৳3,291.53
₹5,844.24
List Price:  ₹8,349.28
You save:  ₹2,505.03
RM331.61
List Price:  RM473.75
You save:  RM142.14
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,466.20
List Price:  ₨27,810.05
You save:  ₨8,343.85
฿2,579.91
List Price:  ฿3,685.75
You save:  ฿1,105.83
₺2,258.19
List Price:  ₺3,226.13
You save:  ₺967.93
B$355.28
List Price:  B$507.56
You save:  B$152.28
R1,291.06
List Price:  R1,844.45
You save:  R553.39
Лв127.20
List Price:  Лв181.73
You save:  Лв54.52
₩95,163.27
List Price:  ₩135,953.36
You save:  ₩40,790.08
₪260.34
List Price:  ₪371.93
You save:  ₪111.59
₱4,006.12
List Price:  ₱5,723.27
You save:  ₱1,717.15
¥10,811.89
List Price:  ¥15,446.23
You save:  ¥4,634.33
MX$1,180.56
List Price:  MX$1,686.59
You save:  MX$506.03
QR255.22
List Price:  QR364.61
You save:  QR109.39
P950.05
List Price:  P1,357.27
You save:  P407.22
KSh9,308.67
List Price:  KSh13,298.67
You save:  KSh3,990
E£3,339.92
List Price:  E£4,771.52
You save:  E£1,431.60
ብር4,017.22
List Price:  ብር5,739.13
You save:  ብር1,721.91
Kz58,559.69
List Price:  Kz83,660.29
You save:  Kz25,100.60
CLP$65,083
List Price:  CLP$92,979.70
You save:  CLP$27,896.70
CN¥496.09
List Price:  CN¥708.73
You save:  CN¥212.64
RD$4,059.13
List Price:  RD$5,799
You save:  RD$1,739.87
DA9,404.13
List Price:  DA13,435.05
You save:  DA4,030.92
FJ$157.14
List Price:  FJ$224.49
You save:  FJ$67.35
Q543.86
List Price:  Q776.98
You save:  Q233.11
GY$14,638.84
List Price:  GY$20,913.53
You save:  GY$6,274.68
ISK kr9,768.50
List Price:  ISK kr13,955.60
You save:  ISK kr4,187.10
DH701.39
List Price:  DH1,002.03
You save:  DH300.64
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,006.46
List Price:  ден5,723.76
You save:  ден1,717.29
MOP$563.24
List Price:  MOP$804.66
You save:  MOP$241.42
N$1,288.69
List Price:  N$1,841.06
You save:  N$552.37
C$2,575.52
List Price:  C$3,679.48
You save:  C$1,103.95
रु9,351.66
List Price:  रु13,360.08
You save:  रु4,008.42
S/260.79
List Price:  S/372.58
You save:  S/111.78
K270.67
List Price:  K386.69
You save:  K116.01
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,901.47
List Price:  ZK2,716.50
You save:  ZK815.03
L323.43
List Price:  L462.07
You save:  L138.63
Kč1,625.98
List Price:  Kč2,322.93
You save:  Kč696.95
Ft25,280.20
List Price:  Ft36,116.11
You save:  Ft10,835.91
SEK kr759.20
List Price:  SEK kr1,084.61
You save:  SEK kr325.41
ARS$61,608.98
List Price:  ARS$88,016.60
You save:  ARS$26,407.62
Bs483.53
List Price:  Bs690.78
You save:  Bs207.25
COP$273,394.26
List Price:  COP$390,579.97
You save:  COP$117,185.71
₡35,797.53
List Price:  ₡51,141.53
You save:  ₡15,343.99
L1,728.55
List Price:  L2,469.47
You save:  L740.91
₲523,227.64
List Price:  ₲747,500.10
You save:  ₲224,272.45
$U2,673.53
List Price:  $U3,819.50
You save:  $U1,145.96
zł280.44
List Price:  zł400.65
You save:  zł120.20
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.