Video Transcript
Everybody should go to this website.
That's where I'll do the examples.
Your SQL, it's really easy.
Just need a web browser and go to that website.
You should be able to click Run SQL and you just get the results.
So talk about SQL today, structured query language.
Basically, it's database.
When you go today to any website like Facebook,
Instagram, TikTok, whatever you use today,
or even desktop applications,
it's very likely it is using a database to
store all the information that you see.
For example, when you fill your profile,
you have your name, right?
You have some links,
you have your description.
Where does that information go?
Well, ultimately, it goes behind the scenes to a database,
and that's where it is stored so that next time you access
the website, it remembers all of that.
So the most popular way of storing data is via SQL.
It's like a database.
We write in this language called SQL or SQL,
and a structured query language.
So you can think of this as Excel.
Let me share Excel.
So it's like, if you ever use Microsoft Excel or Google Sheets
or whatever spreadsheet software,
you have rows and columns, right?
So in SQL, basically, we're going to have what we call tables.
And each table has columns and rows.
So the columns, they denote what kind of information.
For example, I want to store the first name of somebody.
I can say here first name, right?
So I have a lot of people.
I can have John, and I have Maria, et cetera, right?
So the heading here would be what we call column
for this table.
And then we have each row.
The first row is for a specific person.
The second row for another person, and the cell goes on.
So every row represents a different object
or a person in this case.
When we go back to Programmys,
what we're going to see is on the right-hand side,
you see all your available tables for this exercise.
So we have a table of customers,
and as you can see, we have some columns.
And then each row represents a person
or a customer in this case.
So we got information about the first name, last name,
age, and then the person's country.
Now, what we typically do with SQL
is we always have ID column to identify each row.
And it typically is sequential, starting from one,
and then a new one would be two, and so on.
So it's very common.
It's a way of uniquely identify that record.
So we have a table of orders that's just for orders.
Okay, somebody ordered a keyboard, paid 400 bucks,
and it tells you which customer.
In this case, it goes by ID.
So if you say customer ID four,
it means you've got to go to the customer's table
and see, okay, the person who has ID four
is the one that bought this keyboard.
And then we got shipping.
So this is all about shipping.
You can see shipping of ID one, status is still pending,
and the customer is number two.
So number two means, okay, I've got to go to the customer's
table and look up who is customer number two.
That's Robert Luna.
So as you can see, each table kind of represents
a specific piece of information.
You cannot have everything together that are not related.
So we separate the customers,
we separate the orders and shippings,
and if you need to find information
across all of those tables, you've got to look them up.
Like they have it here, like, okay, the customer four.
So you got to always look up in the customer's table
who that is.
Okay, so we got tables, and tables have rows,
and they have columns.
So in SQL, we make what's called a query
to ask about information.
So in this editor here, you can write a query,
and then you can click run SQL,
and that will run the query
and do whatever command you issue that.
Okay, so today we have different kinds
of flavors of SQL.
SQL is a general term to the structured query language,
but many companies have implemented their own version.
For example, you might have heard of MySQL,
or Microsoft SQL Server, or Oracle SQL,
so IBM SQL and so on.
Every company kind of make their own version,
but it's all ultimately the same standard language.
If you learn how to write SQL standard,
you will understand how to write a query
for any of those flavors.
Okay, so let's do a first query.
I'm gonna delete everything here,
and I'm just gonna go and say,
select space star space rom space customers.
So this is my first query, and it's very simple.
And if you notice, I use different kinds of cases,
uppercase, all caps for select, all caps for rom.
So this is not really necessary,
but it's like a naming convention.
When we have keywords from the SQL language,
we usually capitalize everything.
So what does it do is allows you to get information
for a specific table.
In this case, the syntax is always to say select,
and then space in star means, okay,
I want to see all the columns, space from, from,
the right hand side of from, you have to say the table name.
In this case, I have a table called customers.
So make sure to use the same casing here.
I know there might be some SQL implementations
that don't care about upper lower case,
but I suggest you always try to pay attention to the casing.
Okay, I'm gonna click run SQL.
As you can see in the output here at the bottom,
I see the output for the table customers,
and it tells me, okay, got several rows.
First row being John Doe from the USA, age 31,
and then the second being Robert Luna, 22 from the USA,
so on.
Okay.
So the star here means I want to get this column
and this column and this column and that column,
basically all the columns in the table.
If you notice in the right hand side,
the left hand side of this page,
we kind of have a enumeration of all the columns
for each table.
A table of customers has these columns here.
So when you say star, you're saying,
I want to see the value for all of these columns.
Now, if you want just a specific column
information from a column,
maybe I only care about somebody's last name.
I can go here instead of star,
I can say last under score name.
That's the exact name for the column here,
and you can see here as well.
And I click run.
What I'm gonna get is the last name for each row.
So you can see the first is there,
the second is moon and so on.
Now, what if I want first and last name?
Well, that's easy, just separate them by a comma, okay?
So I can either put before or after.
Let me put at before.
So I'm gonna say first under score name,
now add a comma there.
And I click run SQL.
You're gonna see now I get the first name and the last name.
Okay, I usually like to add a space after punctuation
like comma, but that's not required.
It's just for me to better visualize things.
Does anybody have any question?
You can always post to comments there.
Let me see if I question you and keep putting underscore.
Is that needed?
Yes.
So you have to pay attention.
Every character matters.
The case you matters.
So if you see this column name here,
the column is literally first name, underscore name.
So you have to use the underscore there.
So different SQL implementations kind of have
their own name in convention.
In this case, they are using of underscore case.
So meaning every word that you have,
you're gonna separate it with underscore.
If you had like without the underscore
for the programming languages,
it's kind of difficult to have a space
because it would mean like two different things.
So that would be really confusing.
So that's why we always put something to separate the words
or you don't have to put anything.
There are some enaming conventions where all you have
to do is capitalize the first word,
the first letter of each word,
like Pascal, Casey, Microsoft, SQL server.
Okay, but you gotta have the underscore.
All right, more questions.
Pretty much a data repository.
Planned company started data.
What's the purpose of semi-colon?
Semi-colon here is always needed
at the end of every statement in SQL.
So I forgot to let you know about that.
So in SQL, all the commands have to end in a semi-colon.
That's just for the syntax of the language, okay?
So make sure to add that semi-colon at the end.
Implementation the database
will determine how you query information it.
I'm not sure your question, Maggie,
but every database has their own implementation of SQL
that might not necessarily be the same.
I do not know the internals
of the database implementation here.
So that's something you don't need to know
how the database is built run internally.
You just need to know the language called SQL
in order to interface with that
and be able to manipulate or get information out of it.
Can we use like first name plus last name?
That's called concatenation and it is possible.
There's a function that there are functions
that you can use to concatenate things.
But the way I did things right now is separate columns, okay?
But if you're interested in that,
I think it's called concat and you would call it here
like this and pass the arguments
and finish with like this.
Something like that.
Just look it up and you eventually find something.
So I'm gonna continue.
So let's talk about country.
Okay, where are people, where are the countries?
So if I wanna select only the countries
from these customers,
I would write here to let country from customers.
So I know where my customers are from.
So I can see I have customer from USA,
another one from USA, another UK, then UK, then UA.
As you can see it repeats, right?
Because I have one person per row,
even though you don't see their first, last name.
But this is kind of such,
but if I have like a lot of people, one million people,
there certainly isn't a million countries, right?
So I wanna be able to see a list of all the countries
my customers come from without necessarily having to go
through all the one million rows.
So in order to do that,
you should do distinct before the column name here.
So what this does is it groups every duplicate
into a single row.
So if I click run SQL,
I only see unique country names.
That way, if I had a million rows,
I don't have to go through every one of them
to see which, who is from what.
If I do distinct, I can consolidate
into probably at most 190 something countries, right?
There's no more than 200.
Somebody asked the question I wanna know,
the way prayer information is SQL standard
regardless of how it was implemented.
Yes, like I said in the beginning,
standard SQL as a standard language,
if you'll know how to write SQL,
this way we're writing,
you can do pretty much any SQL database
because they all follow the same patterns.
It's a standard.
They just implement, when I say implement,
it just means they construct the thing you're gonna use.
It's like a blueprint.
You have a blueprint from a car,
everybody's gonna follow that same rules.
But the blueprint by itself,
don't necessarily let you drive, right?
Because you need somebody to build a car.
So SQL is like the blueprint
and the companies have built a car,
meaning their own cars, right?
We have different kind of makes
so they each have their own variation,
but they all follow the same pattern.
So we can all drive over wheel,
we can brake and throttle and so on, all right?
So moving forward.
Okay, let's talk about looking somebody up
because so far we're always querying all the rows.
And that's not really what we do.
Let's say I wanna find information
about somebody who might have the last name dough.
So let me put star back.
So I want all the columns from customers.
And I'm gonna teach you about the where clause.
So we can add after the from, we can add where,
if you want to narrow it down to specific rows.
Like I wanna say where the last name is equal to dough.
So what this means, okay,
I'm gonna select the rows from the table customers,
all the columns,
but I only want the ones that have last name
equal to dough here.
Click run.
And you can see there's two people,
two customers, okay?
You can see their last name is dough.
So where is very common?
Don't forget, you can just say the column name here
and equal sign means to compare, okay?
Some programming languages, this is like assignment,
but this is not the case here.
This is actual comparison.
Okay, but what if I wanna look up by first name as well?
Okay, I found two customers.
Actually, I just wanted to find out about John.
So how would I narrow down it even more
to just a single person?
Well, I have to say and another comparison.
In this case, first name equals to John.
Now I wanna do something here real quick.
I'm gonna put lowercase j.
Let's see what happens.
Well, I got nobody, no rows.
Why?
Because I typed lowercase j here
and that's different from uppercase j.
So be careful, the case matters in this case.
Think about that, okay?
So you can basically follow the same pattern
and keep saying and column name equals something
and column names equals something.
Now I might have noticed I put quotes around the name.
Why did I put quotes?
This is called the string in programming language, okay?
Because I want to literally say the word
or the string of characters, D-O-E, okay?
If I put it without, it might think it's something else.
Like usually programming languages,
we have the reserved keywords like the select here, right?
And the from and where, there's special keywords.
And if I put it without quotes,
it's gonna try to think it's special keyword
or some kind of variable or something.
So I'll just make sure if you have the literal text,
you have to add the single quotes around it.
In the beginning and the end.
Somebody asked, can you compare by ID?
Yes, you can compare anything.
If you want to say by ID,
you just say customer ID equals some number.
Now in the case of number,
you probably don't need to say the quotes around it.
For example, let me just do it real quick.
Where the customer ID is, say I want to find the customer ID
three, let me see.
As you can see on the left hand side,
it tells me what's the kind or type of the column.
Customer ID in this case is int.
Int is short for integer.
So in that case, I don't have to have quotes.
I had to have quotes for the other one
because it's a vercare, meaning character varying string.
Okay, of 100.
And there you go.
In this case, because that's an int, a number integer,
I don't have to have the quotes.
That as you can see, I can find by ID.
Yes, it's called case sensitive.
Somebody said, yeah, the case matters.
Somebody commented, what if you wanted John and John?
So if you want these two,
so first you would have to say first name, John,
to get both of them, right?
Now that could be a third, John.
Do you care if there's a third one?
If you care about the third one,
and you specifically just want these two,
even though they're mean by the other John's,
then you got it now or down even more.
But you can say like this,
instead of saying, and, you can add the,
let me show you real quick.
That's a little bit nested things.
So, and the last name
is either dough or last name is run.
Let's see if that works the same way.
So in this case, I have to say, okay,
first name is John and something else.
And the something else is, I have to use a R here,
which is like the opposite of N in a way.
The last name is either dough or last name is that one.
So that one, you just capture those two,
assuming there are more John's.
Okay, so the value,
so that's a little bit of a bit of a nest in there.
I didn't want to go that far.
Might confuse some people.
Nice, we've talked a lot about querying data,
like finding out information,
but can we manipulate the data?
I mean, can we add more records?
Can we update them?
Can we delete them?
Let's talk about inserting new records.
So when you talk about adding a new row,
we talk about insert.
So we're going to say insert into customers values.
And then we're going to put some parentheses,
Stanley Cohen here.
Now the syntax is all like this,
insert space into an end table name,
space values and some parentheses here.
And within the parentheses,
you got to specify the value for each column in order.
Okay, in this case, you have to have the order.
So the first, you look up in the left-hand side,
you can see the customer ID is the first one.
So as you can see, there's already a five,
so I'm going to add six.
And then if I want to separate these column values,
I need a column.
I like to add a space of a column,
but that's not necessary.
Okay, now I need, what's the next column here?
After customer ID, there's first name, right?
So I got to write the first name within quotes.
Let's say, I don't know, let's say somebody Anna.
And then column, now I need the third,
that's the last name, so last name I'm say long.
And then the fourth column here is age,
I'm going to say 21, Alma.
Now the next column is country, let's say Canada.
Now let's click run, see what happened there.
If you notice, we now have a new row
in the column customers.
So typically when you have databases, the ID column,
usually auto increments for you,
so you don't have to specify it.
But here it's not really,
I don't think it works that way in this environment.
So I have to specify the ID there.
You might be asking, okay,
I have to always follow the same order.
What if I don't know, for some reason,
I don't know the order of the columns.
Obviously if you have access to database,
you're going to know the order,
but let's suppose you don't know.
You can always go here before values,
you can specify the column names here.
Let's say I say customer ID is the first,
but then let's, for some reason,
use last name before the first name.
An Asian country.
So make sure it's specified all the column names
separated by a comma.
Now you might notice there's no quotes around them
because they're a special thing, they're column names,
so no need.
Now if you notice I put last name before first,
now if I do this, I gotta swap these two
because this one is for the first one here,
so they're all like corresponding in order.
The last name in this case would be Anna, which is wrong,
so I would have to swap this one with that one there.
Like make sure the syntax is correct.
And I put in a new row so you can see better.
The line break doesn't matter, okay?
I can break it into a new line
and that won't change your thing.
I just made it so you can see it better.
You might have noticed I'm typing everything
in a single line, but you don't have to.
Usually the SQL queries will be very long
so you can break them down multiple lines
and there's usually a name, a convention for that
if you want to follow.
Okay, what happens if I do that?
Oh no, if you read the error,
unique constraint failed on the customer ID.
So it says stable name dot column name.
You're gonna see this a lot.
So unique constraint.
So the column customer ID has a constraint
called unique meaning I cannot have a same row
of different rows with the same customer ID.
So that's important because it has to be something unique
so that every row has something unique you can query for.
So since I cannot have that there,
I'm gonna put seven here.
Now if you notice, there's no protection
about first name and last name.
It can't be the same.
You know, probably in the world,
there are people with the same name, right?
So I wouldn't add a unique constraint to the names.
And probably they could have the same age and same country.
That's totally possible to have these kind of things.
They could be the same different people
by the same name.
Okay, and if you notice, the last name is the same,
even though the order here was changed.
So if you need to order any way you want,
just specify the column names here
and order any way you want,
but make sure that the values right hand side here
is corresponding in order to the names that you wrote.
Anybody have any questions?
Is ID always the primary key without specifying it?
No, you have to specify that a column is a primary key
when you make the SQL statement to create the table.
Because we are in this environment,
the tables were created for us automatically,
but in the real world, when you have to make your own table,
you're gonna write a statement called create table,
and then you're gonna specify each column,
and there you're gonna say,
is this column primary key or not?
So you have to do it.
But if you're using any kind of framework
for programming like a web server,
what we call ORM, object relational mapper,
they conveniently have all these things
like built in for you,
so you probably wouldn't have to do it manually,
but in practice, it's behind the scenes,
it's there, somebody had to add it.
What is BearCare 100?
So every column has a type in SQL.
Like you can store numbers, right?
You can store text.
You can even store binary files,
like I don't know, images or whatever.
So if you look at the left hand side here,
customer ID is an int, int is like integer,
which stands for just a number, that's an integer, right?
One, two, three, and so on.
And then first name is BearCare 100.
BearCare is a character varying string,
meaning it's a string of characters that vary in length.
So I can have one character, I can have two,
I can have three and so on,
but it has to be a limit, right?
How many characters can you have?
That's what you put in the, usually what the 100 there is for.
It's just by kind of a limit.
Okay, I want this varying character,
but how far can you go?
Can I have more than three characters and so on?
Yeah, look up the docs, BearCare,
and it will tell you there what number.
If you have more information,
some of them are that different kind of things,
depending on the type,
how to sort data in a specific order,
or sending or descending.
We didn't talk about that, but there is a way.
I can show you right now,
even though I wasn't planning on talking about that.
So let's say, you can see now the columns start
from customer ID one to seven, right?
What if I want to reverse that?
So I can say select start from customers,
and this will give me the table as is.
Now if I want to order by some column,
I add order by here.
Literally the word order space by,
and then customer ID is the column I want to order by.
But then I got to say, is it ascending or descending?
By default, it's ascending,
but if you want to send it,
D E S C, which stands for descending.
So as you can see, now I have the rows descending
by customer ID column.
Okay, so that's the order by.
Let's talk about deletion.
I added two records here,
and I don't want to like them,
I want to delete them, six and seven.
Now before I talk about delete,
I want to remind you, be careful of delete,
because you might delete everything,
so I'll show you how to not delete everything.
So we're gonna say delete from,
and then you see the table name, the state of customers.
Now if I say like this, delete from customers,
everything will be deleted, all the rows.
So be careful using this.
So you always want to put a where there,
to narrow it down to one thing usually.
Okay, let's say I want to delete from customers,
where the customer ID is six,
because I don't like six, I don't want seven.
So I can do here customer ID is six.
This is the most common way of deleting, right, by the ID.
And the safest thing to do, right,
is just delete one record, if it's this.
So if I do that, it's gone.
The customer, the row with the customer ID six is gone.
Now I could also delete by other columns.
For example, I don't want seven,
but I could say last dang long,
but I have to be careful, this is not good.
Okay, I'm telling you about this just for practice,
but usually this is not a good thing,
because there could be other people named long, right?
So if I meant to just delete Anna here,
this very unique row,
there were other people's name long,
they would also be deleted.
So be very careful, you just usually just want to delete
by the ID, that's the safest way,
because you just delete one thing,
because the ID is unique, right?
Primary key, there's only one row,
only one row of that ID.
Okay, so that's pretty much how to delete,
delete from table name and add a where there,
and specify usually the ID is equals.
So what we're going to talk about next
is how to update records.
Okay, so to do that, we're going to use update.
So you say table name, and then set,
and then whatever you want to update.
For example, country equals Canada.
Now, you gotta be careful here too,
if I say update, the table name,
customer set country to Canada,
that means every single row will be set to country Canada.
So it's the same thing to delete,
make sure to add a where there,
to be specific about which row you want to change.
So let's say, for some reason,
Robert Luna won't move to Canada.
So customer ID is too, is that Robert Luna,
you remember here?
And if I do that, he's now in Canada.
So make sure to add a where there,
otherwise you're going to update everything.
And that's pretty much how you update anything.
Just after the set, you specify what columns
you want to update, and then here,
some narrow down, some filtering,
to make sure you don't change everybody's rows
by accident.
So that's pretty much update everybody.
I don't have anything more to say about updates.
If you have any questions, let me know,
otherwise we just keep going.
You want to try what happens when I see,
without a where, click run.
Everybody's now from Canada, to see what happened.
So that's really bad.
So I'm going to refresh, so I get a reset everything.
So this country is a reset.
Obviously in the real world,
you don't get this reset like I just did.
So be careful.
Is there an undo feature,
or do you have to redo the entire database?
Yeah, so you have to be careful about database.
Now usually people keep backups,
that is every few moments in time,
it backs up the database to a certain point in time.
So you could technically try to bring it back
to a certain point in time.
You know how people might do really bad things
in database, so that's how you recover.
Now there's an undo feature, I don't know.
There might be a way to,
like I know database you can log everything you do.
So there might be a way of kind of going back
to a previous snapshot.
Now if you're working programming languages,
typically what you do is what's called a transaction.
You try to do something, but if anything fails,
it will just revert back, what's called a rollback.
But that's just on the application, like the client side,
not the database itself, somebody were to manually
go into the database and try to do stuff
that's not a matter.
Can we explore important data in SQL?
Absolutely, you can always make what's called a dump
of the data and there are different kinds of dumps.
Usually you can do that with a command in the command line,
any of your database servers, you can do that.
Okay, that's nice, but how do we know,
let's say we have David here,
let's find out what he ordered.
We have a table of orders,
so that's like you're in a store,
you're gonna keep track of all the orders.
Let's understand, okay, what did he buy?
Well, how would you do it?
Well, easiest way is look like you get the,
his customer ID is free, so you're gonna say, okay,
select all the columns from what's the table now?
That's orders, where the customer ID,
notice we have the column customer ID in order
and that's free.
Usually we always make sure that the customer ID here
points to the ID of the customer column.
So you can see David Robinson, customer ID three,
he only bought a monitor, no more orders.
Now this thing here is called a foreign key,
when you have ID in another table,
that is mapped to this table here.
In this case, we have orders and we have a customer ID.
For the customer ID here is a foreign key
that points to the primary key of the customer's table.
This is a very common pattern
you're gonna see all over the place.
We don't usually like mix table data together.
Obviously you could have the orders table,
have the first name, last name, age and so on,
but that's not good practice at all.
We need to separate the data.
So we have a table dedicated for customers
and one for orders.
And if you need information about the customer,
like their first name, last name, whatever,
you just gotta look at the foreign key here
in the order stable and then go to the customer's table
and find a primary key that matches that.
In this case, this case is what it for, right?
So John.
Now I'm gonna talk about how to include that information.
Obviously this is not useful if I'm going here, okay.
Customer number three, who is three?
I don't know.
So I don't wanna manually look up in the table customers.
I want this to already show me the first name,
last name and so on.
That's very common thing we do
and that's called a join, SQL join.
We're joining tables together.
Okay, so I'm gonna go here.
I'm gonna say inner join.
That's a kind of join, right?
Okay, there are different kinds of joins,
but this one is inner join.
They're gonna match the customer ID in orders
with the customer ID in customers.
So we can exactly know every single order
for that customer and their information.
Okay, we gotta say, okay, join with what?
What table?
Customer's table?
And then on, that's a keyword.
You gotta say how you wanna match.
Okay, I wanna match the customer ID of one table
that's orders with the customer ID of the other.
But notice here, both tables have the same column name.
How do I know which one is which?
So I gotta, this is ambiguous.
So how to find a way of, you know, disambiguate here.
So Alice, I can prefix these with the table name.
So okay, let's say customers.customerID.
Oh, okay, so we're saying the customer ID column
from the customers thus match the orders.
Dot customer ID.
So that way we know, okay, is this customer ID here
from customers has to match the customer ID from orders.
Now I'm gonna move the where to the ending, okay?
Because of syntax.
But make sure to move your where to the end
after you did the inner join thing.
Now if I do that, oh, what happened?
Ah, same problem I had, ambiguous, right?
Where customer ID is free?
In this case, it doesn't really matter which one I pick.
So you can say here, either customers or orders.
And there you go.
Now what happens is it takes this record from orders,
and then it kind of puts side by side
with what it matches for the customer's tables.
As you can see this, the left hand side is from orders,
and the right hand side is from customers.
Now I wanna show you customer four
because customer four bought two things, right?
If you notice from the order table,
you bought a keyboard and mouse.
So if I use four, we're gonna get two rows
because we have the one for the keyboard
and one for the mouse,
but observe that each of them on the right hand side,
they get their own copy of the customer row.
The single customer row for John appeared twice
because we have two rows from order.
Does anybody have any questions?
Can we do this thing, this situation?
I know what it means, yes, you can group by things
depending on what your goal is, right?
You can consolidate these into one row.
And I'm gonna show you a way just now,
but there's no need to,
whether you consolidate things is independent
of you having a joint table or not, right?
So we can consolidate into single row.
But the point here is you don't wanna consolidate
because you're trying to figure out the orders, right?
If you consolidate into one,
you're gonna lose keyboard and mouse.
What are you gonna do?
We don't know the orders.
The point here is not to consolidate,
but if you need to consolidate in another situation,
yes, you can.
And I'll show you how to count things in a moment.
So if I remove the where,
this is gonna happen for every customer.
So we got all the orders
and the customer information side on the side, okay?
Now we can see all the orders and the customer
and what they bought joining the two tables.
And this is not narrowing down to anything.
Now I'm gonna talk about counting things.
How can we find out how many keyboards we sold?
One way is to go to the table.
Okay, you got orders, let's count.
Okay, one keyboard, two keyboards.
Nice, two, right?
What if there were a million orders?
Would you do that by now?
Obviously not, right?
So you want the computer to count for you.
So how can we do that?
Well, let's say select star from orders.
If I just did the way this way, I gotta do by eye, right?
Another way could be okay.
Where the item is, keyboard,
still by eye, right?
But a little better.
If there were a million rows,
I would have to count the a million rows, right?
By eye.
So let's make the computer count.
We're gonna say count, parenthesis star here.
Now if you do that, we got two.
So that's as easy as adding count there.
So when you say count, star, blows parenthesis,
this is gonna count the number of rows.
It's not gonna give you the rows,
but rather how many rows were found with this query.
So there were two orders of keyboards.
So using count, it can count anything.
Maybe I wanna count how many customers I've had.
Select count, star, with the parenthesis around,
round customers.
That'll give me how many rows for customers I have.
And you can see it's five.
Notice I have two separate queries and I can run them
and that will give me two separate outputs.
The second one is for the customer count.
Now you might notice there's this count, star here.
If I'm reading this, I'm like, okay,
obviously I have the query.
What if I didn't see the query here?
And I only saw the output.
I don't know what two is, what five is.
So usually people can add an adios to rename this heading.
So I can say here after the count, star,
adds, let's say, keyboard, sales.
And if I do that, instead of seeing count, star,
I'm gonna see keyboard, sales in the table heading.
And I can do the same for customers as customer count.
That way it's clear to me what the output is
if I only look at the output.
Okay, that's called an alias.
You can alias any column you want.
Just make sure to add an ad to the right hand side
of the column name or whatever operation,
in this case, account.
And I'm talking about the select area, okay?
You bet your mind.
If you wanna notice I put the words together,
if you want separate by space,
make sure to add a word surrounding it.
If I want a keyboard, space, sales.
You can do that but add the quotes there.
You know, programming, usually if you don't have the quotes
in this case, it would treat us
to separate things usually.
I don't know if this would work.
See it error, right?
Because it doesn't understand what this is.
Two separate things.
So we need the quotes there.
Now, somebody asked about grouping things
or making this thing.
And I wanna show you a way of grouping.
Let's say I have select start from orders
that give me all the orders.
Let's say I just wanna group the rows by the item.
Because I don't really care about the information,
I just care that I can see the item names.
I can do group by here and the column.
And see what happens there.
You can see all the rows for keyboard
consolidating into one.
We have two for keyboard, right?
Obviously this example, I would have to add more rows here
so you can better see what it would do.
But every single item for keyboard
is consolidating into one single row.
Now you lose information about the amount, right?
Because it only takes the first one.
So that's why some cases you don't wanna do this.
This is only appropriate if you don't care
about this information being lost.
Like this one's for the customer ID four, which is this one.
But you lost information for the one,
customer ID one here for the keyboard.
But this is called group by.
And you can group by anything.
We could group by, I don't know, customer ID
for some reason.
Now consolidating the orders in the customer ID
or amount, okay?
Group by amount.
And you group everything by the amount.
But I think it's more useful to group by the item here.
Yeah, so this is kind of similar to distinct in a way, right?
You could also do, select distinct item, right?
That's another way we start a group by.
So you could also do it that way.
Yeah, so I think distinct is very easy.
So whenever you can use distinct, use that.
But if you can't use distinct, you can group by.
That's more complex for more complex stuff.
Okay, everybody, I think I've reached the end.