Do You Want to Learn SQL?

Take our free eight day course, where I'll teach you in simple to understand English all you need to get started learning SQL.

-
DAYS
-
HOURS
-
MINUTES
-
SECONDS

Free Beginners SQL Course!

SQL Select Statement

You can use the SQL SELECT statement to query a database table. It is the most common means to query the database, and one of the most important commands to learn.

You’ll find there are many ways to write a query. For now let’s keep it simple. In future lessons we’ll go over other items such as sorting your data.

All of the examples in this tutorial are based on PizzaDB. You can get the script to build the PizzaDB here.

Below is the Customer table. We’re going to use this for several examples.

Example

Return data from the Customer table

select LastName, Email from Customer
/* Answer */
select LastName, Email
from Customer

SQL Select Statement Syntax

Listed below is the simple format for a select statement.

SELECT column1, column2, ...
FROM TableName

A query needs to things to run. First you need to know which columns to show, and the second is the specify the table name.

You’ll see the statement starts off with columns, then the FROM clause is used to specify the TableName.

Note: In this course I’ll usually type SQL keywords as upper case in the text to make it easier for you to read the tutorial, but you’ll see I use lower case in my code. Why? Because modern editor highlight the keywords for you, so I don’t bother special formatting!

Return All Columns from a table

In some cases you’ll want to return every column in a table. To do this you can use an asterisk * within your select statement.

Example

Return all columns from each Customer.

select * from customer
/* Answer */
select  *
from customer

Though the * is great shortcut, it isn’t best practice to return every column. To write efficient SQL get in the habit of listing the columns you wish to include in your results.

Exercise

Now it’s time for you to write your own SQL select statement! For each person, list their LastName, Email, PhoneNumber, and StreetAddress in a query.

/* Answer */
select LastName, Email, PhoneNumber, StreetAddress
from Customer