Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video you'll learn how to write your first SQL query: retrieving all information from one table.
Definitions
Syntax The vocabulary and grammatical rules surrounding the structure of your code.
Keywords The vocabulary words of a programming language used to issue commands to a computer.
SQL Used
SELECT * FROM <table name>;
The asterisk or star symbol (*
) means all columns.
The semi-colon (;
) terminates the statement like a period in sentence or question mark in a question.
Examples:
SELECT * FROM books;
SELECT * FROM products;
SELECT * FROM users;
SELECT * FROM countries;
See all of the SQL used in SQL Basics in the SQL Basics Cheat Sheet.
-
0:00
To recap, SQL or Structured Query Language
-
0:04
is a specialized language to get information or data out of a database.
-
0:09
There are many different database systems such as MySQL,
-
0:13
Microsoft SQL Server, Oracle, PostgreSQL, and SQLite.
-
0:18
All of them use the same language, SQL.
-
0:21
Just like spoken languages, programming languages have their own vocabulary and
-
0:25
grammatical structure.
-
0:27
Programmers refer to this vocabulary and grammar as syntax.
-
0:30
Thankfully, most programming languages have a much smaller vocabulary
-
0:34
than spoken languages.
-
0:36
Though they are few,
-
0:37
the vocabulary words in programming languages are very important.
-
0:41
And we call them key words.
-
0:42
[SOUND] A keyword is a command that you issue to a computer to do a certain thing.
-
0:48
And SQL has its own sets of keywords.
-
0:51
Lines of SQL code unknown as a Statement or a Query.
-
0:56
A statement in SQL is like a sentence in English.
-
0:59
A query in SQL is like asking a question in English.
-
1:03
Most queries, no matter how complex, are issued to the database one at a time.
-
1:09
You may hear the issuing of a statement being referred to as running a query, or
-
1:14
executing the SQL.
-
1:16
Let's take a look at a common SQL query.
-
1:19
We will treat all information from the table with the data about books.
-
1:24
Remember, a table is one of the spreadsheet like structures
-
1:27
inside a database made up of rows and columns.
-
1:31
Let's see how we might write a query in English first.
-
1:36
[SOUND] What is all the information we have about each book in the books table?
-
1:41
Now, let's see the SQL syntax to bring back all the information from
-
1:44
the books table.
-
1:45
See there's upper case words there, select and from.
-
1:48
They are key words or special words reserved for the SQL programming language.
-
1:54
You issue a select command when you want to write a query.
-
1:58
You're asking the database to select or
-
2:00
retrieve some of the information from a particular table.
-
2:04
In this case, it's books.
-
2:06
The asterisk, or the star character, means you want to bring
-
2:09
all of the information in all of the tables, columns back.
-
2:13
I'll show you how to bring back specific columns of information later.
-
2:17
The semicolon at the end of the query is like a period or
-
2:21
a question mark at the end of a sentence or question.
-
2:24
The semicolon lets the database know where the statement ends or terminates.
-
2:29
When we issue the statement to the database,
-
2:31
you can expect all entries in the books table to be returned.
-
2:35
Let's see the results of this query in the SQL playground and
-
2:39
see this query in action.
-
2:42
As you can see, it brings back all information in the books table.
-
2:46
Let's ask another question.
-
2:48
Who are all the users of the library in the patrons table?
-
2:52
We'd use the SELECT keyword.
-
2:55
Stop, meaning all of the columns.
-
2:58
From, and then the table name.
-
3:00
In this case, the table name is patrons.
-
3:04
Don't forget the semicolon to terminate the statement.
-
3:07
Once we execute this query, it will return all patron information.
-
3:12
So generally speaking, writing SELECT * FROM <table name>
-
3:17
is a query we can use to retrieve all data from any table.
-
3:22
This can be useful for small data sets.
-
3:25
When you have larger data sets like the users of a large social network, or
-
3:30
the orders from a popular e-commerce site, you probably don't want to run queries
-
3:35
like this because it can take a very long time to execute and return the results.
-
3:40
This can slow the performance down for other database users too.
-
3:44
However, the data sets in this course are very small, and
-
3:48
running these types of queries were having a real performance impact.
-
3:52
Databases have more utility than reporting all the information in one table.
-
3:57
In the next video, I'll show you how to bring back specific columns.
You need to sign up for Treehouse in order to download course files.
Sign up