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