SQL: the language for asking
Say you stand before a database full of tables. How do you pull out only what you want from inside it? Just like asking a person, you ask the database clearly too. You choose which columns to look at, and you say a condition for which rows to keep. This language for asking is exactly SQL.
We ask the database
Earlier we learned a database
is a warehouse of gathered tables.
Inside a table, rows and columns line up neatly.
But among all those rows,
how do we pull out only what we want?
We ask, just like asking a person.
Only, we fix the phrasing clearly.
Turn human words into the set phrasing
and the database understands and answers.
Tap the human-word pieces in turn to turn it into a question. Choose the columns to see and the condition, and the set phrasing forms.
Human words turned into a question.
Look closely and it splits in two.
One part is which columns to look at,
the other is which rows to keep.
Fix just these two and the question is complete.
It looks complex but the inside is simple.
Shall we start with choosing columns, step by step?
Choose the columns to see, SELECT
A table has many columns.
Like name, age, and city.
But you do not always need every column.
Sometimes you only want to see the name.
Then you choose only the columns you want.
The chosen columns stay, the rest are hidden.
Choosing columns this way is called SELECT.
It decides what to show.
Tap the heads of the columns you want. Only chosen columns stay, the rest are hidden. Tap again to release.
Only the needed columns stayed, neat and clean.
The table got much easier to read.
But choosing columns alone is sometimes not enough.
All the rows are still there.
Everyone shows up, whatever their age.
We usually ask with a condition attached.
Like only people aged twenty or more.
So this time, shall we filter the rows?
Filter rows by a condition, WHERE
Now it is the rows' turn.
Not every row in the table is needed.
The rows you want share something.
Like being aged twenty or more.
You write that shared thing as a condition.
Only rows that match the condition stay
and rows that do not match drop out.
Filtering rows this way is called WHERE.
It decides which ones to keep.
Tap a condition to apply it. Only rows whose age matches stay, rows that do not match fade and drop out.
Only rows matching the condition stayed.
The rest quietly dropped out.
Now you have both knacks,
choosing columns and filtering rows.
But the real power comes when you combine them.
Choose the columns you want,
and at the same time keep only the rows you want,
and a table with just the needed part comes out.
Shall we use both at once?
Combine both for the result table
Now use both knacks at once.
First filter rows by the condition.
Only matching rows stay.
Then choose the columns you want to see.
From the kept rows, only those columns show.
Whatever the order, the result is the same.
Where chosen columns meet kept rows,
there a small result table appears.
The answer that fits the question came out as a table.
Tap the column choice and the condition in turn. Combining both builds a result table with only the matching rows and chosen columns.
The answer you wanted came out as a table.
Among many rows and columns,
one question picked out just the needed amount.
Without a person scanning by eye one by one,
the database picks it out for you.
Even with a million rows, the way is the same.
Write the question well and the answer comes fast.
This is the power SQL has for handling data.
Now let's wrap the whole thing up.
Let's wrap up
Gathered on one line, it is this.
SQL is the language for asking a database.
Choosing which columns to look at is SELECT,
filtering which rows to keep is WHERE.
Combine both and a result table that fits the condition comes out.
Choosing columns and filtering rows, that is all of it.
Getting the answer you want as a table with one question,
that is the basics of SQL.
Tap the key points in order to review. (the asking language SQL -> choose columns SELECT -> filter rows WHERE -> combine into a result table)
Now you know how to ask a database
clearly, word by word.
The knack of choosing columns and filtering rows
to receive only the answer you need.
But so far we looked at just one table.
In real life, tables are often split into several.
A people table apart, an orders table apart.
So how do we ask across several tables?
Next, the story of joining table to table,
let's keep exploring together.