SELECT Command

23 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
€67.35
List Price:  €96.21
You save:  €28.86
£55.83
List Price:  £79.77
You save:  £23.93
CA$100.76
List Price:  CA$143.95
You save:  CA$43.19
A$112.23
List Price:  A$160.33
You save:  A$48.10
S$95.14
List Price:  S$135.93
You save:  S$40.78
HK$543.62
List Price:  HK$776.63
You save:  HK$233.01
CHF 63.04
List Price:  CHF 90.06
You save:  CHF 27.02
NOK kr797.48
List Price:  NOK kr1,139.30
You save:  NOK kr341.82
DKK kr502.49
List Price:  DKK kr717.87
You save:  DKK kr215.38
NZ$124.10
List Price:  NZ$177.29
You save:  NZ$53.19
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,361.55
List Price:  ৳11,945.58
You save:  ৳3,584.03
₹5,962.79
List Price:  ₹8,518.63
You save:  ₹2,555.84
RM314.04
List Price:  RM448.65
You save:  RM134.61
₦108,335.42
List Price:  ₦154,771.52
You save:  ₦46,436.10
₨19,495.71
List Price:  ₨27,852.21
You save:  ₨8,356.49
฿2,390.81
List Price:  ฿3,415.60
You save:  ฿1,024.78
₺2,467.44
List Price:  ₺3,525.07
You save:  ₺1,057.62
B$434.14
List Price:  B$620.23
You save:  B$186.09
R1,296.68
List Price:  R1,852.48
You save:  R555.80
Лв131.68
List Price:  Лв188.12
You save:  Лв56.44
₩102,198.93
List Price:  ₩146,004.73
You save:  ₩43,805.79
₪256.56
List Price:  ₪366.53
You save:  ₪109.97
₱4,090.81
List Price:  ₱5,844.26
You save:  ₱1,753.45
¥10,994.99
List Price:  ¥15,707.80
You save:  ¥4,712.81
MX$1,408.98
List Price:  MX$2,012.91
You save:  MX$603.93
QR255.09
List Price:  QR364.43
You save:  QR109.34
P967.82
List Price:  P1,382.66
You save:  P414.84
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,562.49
List Price:  E£5,089.49
You save:  E£1,527
ብር8,907.19
List Price:  ብር12,725.10
You save:  ብር3,817.91
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,323.69
List Price:  CLP$99,038.09
You save:  CLP$29,714.40
CN¥510.80
List Price:  CN¥729.75
You save:  CN¥218.94
RD$4,245.36
List Price:  RD$6,065.06
You save:  RD$1,819.70
DA9,465.07
List Price:  DA13,522.10
You save:  DA4,057.03
FJ$162.39
List Price:  FJ$232
You save:  FJ$69.60
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,638.77
List Price:  GY$20,913.42
You save:  GY$6,274.65
ISK kr9,772.70
List Price:  ISK kr13,961.60
You save:  ISK kr4,188.90
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,291.33
List Price:  L1,844.84
You save:  L553.50
ден4,143.97
List Price:  ден5,920.21
You save:  ден1,776.24
MOP$559.97
List Price:  MOP$800
You save:  MOP$240.02
N$1,289.45
List Price:  N$1,842.15
You save:  N$552.70
C$2,574.80
List Price:  C$3,678.44
You save:  C$1,103.64
रु9,534.71
List Price:  रु13,621.60
You save:  रु4,086.89
S/260.99
List Price:  S/372.86
You save:  S/111.86
K283.85
List Price:  K405.52
You save:  K121.66
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,936.45
List Price:  ZK2,766.47
You save:  ZK830.02
L335.12
List Price:  L478.77
You save:  L143.64
Kč1,694.03
List Price:  Kč2,420.15
You save:  Kč726.12
Ft27,771.44
List Price:  Ft39,675.18
You save:  Ft11,903.74
SEK kr774.38
List Price:  SEK kr1,106.30
You save:  SEK kr331.92
ARS$71,792.58
List Price:  ARS$102,565.23
You save:  ARS$30,772.64
Bs484.55
List Price:  Bs692.24
You save:  Bs207.69
COP$307,165.41
List Price:  COP$438,826.54
You save:  COP$131,661.12
₡35,530.54
List Price:  ₡50,760.09
You save:  ₡15,229.55
L1,777.33
List Price:  L2,539.15
You save:  L761.82
₲546,250
List Price:  ₲780,390.60
You save:  ₲234,140.59
$U3,115.91
List Price:  $U4,451.49
You save:  $U1,335.58
zł287.82
List Price:  zł411.19
You save:  zł123.37
Already have an account? Log In

Transcript

Hi. In this lesson, we understand what is SQL? And how do we use it. SQL is the standard language to access relational database. It's also standardized by NC, which stands for American National Standards Institute. It's a non procedural language.

That means you don't have to talk about the procedural details, you just need to give the command emphasizing on what you want, not how it is to be done, because SQL takes care of how to get it done. Now, the first thing you need to know is these commands are divided into different categories. Data Definition Language commands known as DDL are meant to create tables alter the structure of the table. And dropping the tables and other objects. DML is about manipulating the data, they update existing data, delete the data and so on. dcl is about controlling access to data.

We talk about data in the context of security, transaction management commands, like commit and rollback or use it to manage transactions again a topic we take up later. But today's discussion is about how to query the data, how to retrieve the data from database using select command, the most important command in SQL and these are some rules and if you are using SQL commands in SQL plus a tool which you can use if you like, then in addition, Today's the first To three you have to follow the last point which is you must terminate the command with semicolon. But otherwise that is not needed. And then the best part of it is they are not case sensitive, you can give them an uppercase or lowercase and you can enter statements in any number of lines and keywords are not to be displayed Otherwise, there is no other restriction.

For example, you can't put a part of the word in one line and another part in another line and there are no abbreviations. You have to give the words completely there are no abbreviated versions. So those are the rules very simple to follow. So not case sensitive. You can give commands in multiple lines need not be terminated with semicolon unless you're using SQL plus as we use SQL developer, you don't have to worry about semicolon. Now, these are the tables we use throughout this course, these tables are coming along with your HR schema.

And these tables represent the data about employees, departments jobs, and the history of jobs done by employees. And this is a diagram which clearly indicates the relationship between the tables and we call it as ER diagram entity relationship diagram. And if you see department's table and locations table, they have one to many relationship and this is what you see. This indicates that we have one to many relationship. And then similarly, you see a lot of one is too many rules. Relationships between departments and employees, and also between countries and locations.

There are cases where you have one is to many relationship between departments and employees and also employees is having relationship back with the departments. So we will see more about that little later. But it's enough to know that these tables are the ones we use for all our demos, because these tables were created by Oracle. And the aim is to give you a simple but a database which represents many common scenarios and coming to the blue parts, they are all primary case. So whatever is put in blue, that primary key, and then in job history, you see what is called as composite primary key. So we have two columns.

Use it to uniquely identify rows. So it's called composite primary key. And we also have a lot of foreign keys. For example, in employee table, your manager ID is a kind of foreign key. And then department ID is a foreign key. If you see this department ID is referencing this department ID.

And similarly here, the location ID is referencing that. And then in job history, job ID is referencing this. This is the jobs table. department ID is referencing that employee ID is referencing this. So we have as many as three foreign keys in job history. So this is what we are going to use all through.

Do you remember these tables and it's very important for you to understand What data we have and the relationships between the tables, as we go deeper, you will understand more about this. Now, it's time to use a command to understand the structure of the table. So, describe is the command we can use to find out what is the structure of the table like what columns we have, whether the columns can accommodate null value means a missing value, because sometimes you don't have anything to store then you store what is called as null value and then the data type of the column. So if you take this job stable job ID is of type where character to have 10 it means we can store up to 10 characters and it cannot have null value. Whereas minimum salary can store up to six desserts and it can have null value So, if you do not know what is the minimum salary for a job, you can just leave it empty that is called null and more about null values later.

So use describe and then you give the name of the table to know more about the table structure. You can abbreviate this command because Strictly speaking, this is not a SQL command. This is called SQL plus command. So, some commands are coming from Oracle specifically, and they are not part of the standard SQL language so, we can abbreviate them so you can say describe or de se both are fine. Then this is another command to get the list of all the tables that are available to you. So, select star from tab is going to give you what are the tables that are present.

You get the table name and feel other details which are not important for now. As you can see, one of them is not a table, it's called a view. And that is a topic for some other day. But this is the command Select star from tab and this is going to tell you what tables are available. Now, the command, the most important command select, it is going to retrieve the data from the table. You can also select a few columns from the table because sometimes tables contain too many columns.

You might be interested in only a few columns not all then we call it as projection. So selecting a few columns from available columns is known as projection. Similarly, you may want to take a few rows from available rows, then we call it as selection. So we will see both Have them. And some of the clauses if you see, like a distinct is saying it'll pick up only the unique values. And then you can have either the column or even an expression.

And expression can have an alias if you like, so that a meaningful name is given to expression. And we can select a few rows using the condition and WHERE clause. And you can sort the data in any order using order by and here are some commands. So let's start by looking at the first one. This is going to bring all the columns that are present in employees table. Star here means all you're picking up every column present in employees.

As we are not talking about where class we take all the rows present in employee table. This is what we call As projection, we are selecting only a few columns from the available columns. But you got to know the names of the columns. And relational model is where you refer to columns not by the position but by the name. So this is how you select two columns from available columns. And this is also the same, but the new thing is this.

This is where I'm giving an alias to this column. So in the output, you're going to see name not first name, this is going to be the column headings. And then the next one is to get only unique job IDs. Not all because multiple employers might be doing the same job, but we get only unique because we are giving distinct and then these two are to be concatenated. This is the symbol for concatenation to pipe symbols, so I want to put first name, then concatenate that with the space, and then concatenate that with the last name. So you're going to get first name, a space and last name.

This is how you can concatenate. So those are some of the examples. And these are the operators you can use. If you're planning to use any artomatic calculations, then you need to use these operators. So star slash plus minus, you might be aware of that. The only thing that is missing is a person taste which is typically found in many languages, but it's not available, so you can't use this.

And these are the standard relational operators single equal to check whether two values are equal. And this is about not equal to, and the rest are pretty self explanatory. These are called logical operators, they are used to combine conditions. So let's look at some examples. To start with. Here we are.

The first one is about an expression. I'm calculating 50% of salary, that becomes the bonus. So first name and bonus will be the headings. And this is going to be the expression from where the value is derived. And the second one is where we subtract the minimum salary from maximum salary in job stable, and we call that as the difference. This is again the alias and this is about selection.

We started selecting a few rows from available rows. So I say take only those employees where salary is greater than 5000. So, it will select only a few rows and also it is displaying only first name the same here but we are just extending gate by saying department ID should be hundred and salary should be less than 10,000. So, we get only those employees where this condition is true as well as this is true because we are using end to join the conditions. So, those are the commands to understand how to use where and then you have some more special operators that are called SQL operators. And these operators are very widely used.

So let's look at them between and a straightforward. It is use it to create Compare whether a value is in between the given range. So I want to get employees where the salary is between 10,015 thousand, inclusive of 10,015 thousand. And the next one is where I'm using in. So, I want to know whether the Department of the employee is 1020 or 30. If department ID is in any of these values, the condition is true.

And then the last one here is about now these two are related to null. And we will discuss little more about null. But to understand this, all that you need to remember is null indicates a value that is not present not available not known. So if for number is containing null value, then I'm interested and just a slight Different one is when phone number is not null. So, if there is something in phone number then we are interested. So, those are the operators and then the like operator is another very useful operator.

This is where we put it like this, I want all the employees where the first name of the employ is matching this pattern. So, like followed by the pattern, the pattern can use two special characters percentage means anything, underscore means only one character. So, this says take out all the implies where the first name is starting with S and the be aware it's always case sensitive. So it is uppercase s and in this case, we are taking all the names where S is present In the name, we are looking for employers where the name the first name contains s. And here we are looking for the names where the second letter is a is, first letter can be anything. And underscore indicates that position can be taken by any character, second character must be as that two small s, and the rest can be anything like that we need to form the pattern based on our requirement.

So this is called as, like operator. And then you can sort the data in any order you like. This is how you can sort the data. The first example is where we are sorting the data by minimum salary. The second example is where we sort the data by different which points to the alias which represents the difference between minimum and maximum salary. And then we can also use the position of the column.

So order by two means you're saying take the second column selected and sorted by that. So we are going to take everything and then sorted by the second column. Remember, second column of the selected columns. So these are the selected columns, not the columns in the table. You might have 10 columns in the table. It doesn't consider them, it counts.

There's only what is selected second column among the columns you selected. And this example is where you can sort by more than one column. That means first everything is sorted by job ID Within the job ID, because you may have two or more employees with the same job ID. And within that there will be further sorting by salary. So first sort by job ID, then within that sought by salary. Of course, this makes sense provided job ID is repeated.

Only within those multiple rows. Further sorting is done by salary. And this is about descending order. default is ascending, but you can sort even in descending order. So those are some examples of select command, which is, of course, the most important. And now before we conclude this, let me demo a few things.

Let's go to our SQL Developer. So this is where we give those commands so if I want to get the details of jobs, Just type that Select star from jobs and go run it. And it's going to display all the jobs like this. And then if you say you want only those jobs where the minimum salary is greater than 10,000 rupees, then give that and then click on this file, it could be 10,000 in the currency of your country or whatever. So minimum salary is greater than 10,000, only two rows are selected. If I'm not interested in all I want only the job titles to be selected, I can go for job title.

And I can also call it as title if I like, go ahead and run. And you can see the heading of the column is title. And we are selecting only the job title. So this is an example for what we call as per section plus selection. you're selecting few rows You are selecting a few columns. If you want to sort the data by an order, you can say sort by one, and then the data will be sorted by job title.

And I'll just change this to 5000 to include more rows go ahead run and you can see all these job titles are arranged in the ascending order. And for any reason, if you want to sort them in descending order, you can say d c, same thing will be in the descending order, a comes last s comes first. And these are the simple things but if you say for example, you want all the titles where the title contains manager, you can put it like this job title, like percent days manage percenters. This is going to get you all the jobs where the title is. containing manager, because before that we can have anything after that you can have anything. Yes, go ahead.

And this is what you get. And also you can display all the jobs where the minimum salary is not known to us. So we can say minimum salary is null. Well, if you don't get anything, that's fine, that means there is no row where minimum salary is null. So these are the commands and we discussed about more commands here. So in this lesson, we got started with SQL.

So we understood SQL commands are divided into categories, like DDL DML dcl, queries and transaction control commands. HR schema is very important. Please focus on that. It's simple to understand and we Use this all through this code. So I want you to get comfortable with that describe command is going to display the structure of the table. And you can list all the tables using Select star from tab.

Tab is a special entity, and it's called a data dictionary. We'll see more about that later. Select command is obviously the most frequently used command. And we will see a lot of it there's so much involved in select it is just tip of the iceberg at this point, then we have operators we looked at them some special operators, and then we understood how we can sort the data by using order by class. So those are the things I want you to understand at this point. So this lesson is all about getting you started with SQL and more about SQL will be discussed all through So enjoy your lending get started and keep going

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.