# 2. Retrieving Data
---[SIDEBAR]---
"type":"objectives",
"title":"Learning Objectives",
"text":"After completing this lesson you can:
- Use the SELECT statement to retrieve one or more columns of data from a table.
- Use the keyword DISTINCT to show only unique rows in a database query
- Use the keyword LIMIT to show a specified number of rows in a query result
---[SIDEBAR]---
"type":"tip",
"title":"Examples in the Lessons",
"text":"The best way to learn a new technology skill like writing SQL statements is to practice writing them. You will find opportunities to practice throughout the lessons of this book. Each example will present a question that can be answered with data from a database. You will see a SQL statement that retrieves the needed data in a text box. You can execute the statement and retrieve the actual data. You can also edit the statement to see how changes to the statement will result in different data being returned."
---[SIDEBAR]---
This lesson features example SQL statements that interact with the museum database (see ---[museumDBDiagram]---). The museum database has five tables:
1. artist: contains information about the artists whose work is exhibited in the museums in the database.
2. work: contains information about the individual works of art exhibited in the museums in the database.
3. subject: contains information about the topic or focus of the works of art in the database
4. museum: contains information about the museums in the database
5. museum_hours: contains information about the days and times the museums are open
---[FIGURE]---
"id":"museumDBDiagram",
"title":"The Museum Database Diagram",
"url":"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTUdqdAE1lgoTZdSeCFhvs7DjP1IpmkrmcgvG4cvbm6s6bj8S5HMHVZvqDcktQH8pfW0Y_YWPrNTMJYDgjRqMYn3Th50b_MXDvNjACrzqiqLTKecTaWj9fZpVjy1-lKG4ElONb6MU4Wxevq8KSeL4BedSCJld7EdmE1W4EIXibj4rkCYNCBbaG6FYL/s16000/canvas_transparent.png",
"altText": "A diagram of the museum database. The museum database has the following tables: artist, subject, work, museum, and museum_hours."
---[FIGURE]---
Each of the examples in this lesson highlight how to write SQL statements to retrieve one or more columns from a table in the museum database. They also describe some basic ways to limit the number of rows that are returned in the query results.
## Retrieving Individual Columns
Perhaps the most basic SELECT statement is used to return a single column from a database table. This statement will have both a SELECT clause and a FROM clause. The SELECT clause is used to define the columns you wish to display in the query result. The FROM clause is used to specify the table from which you want to return the data.
For example, let's say you want to see the names of the museums in the database. You can use this SQL statement to do that.
*SELECT name
FROM museum*
**Try the SQL statement.**
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas|visible
owner:atlas-query
result:museum-name
---[SQL]---
SELECT name
FROM museum;
---[SQL]---
The previous statement uses the SELECT statement to retrieve a single column called "name" from the "museum" table. The desired column name is specified right after the SELECT keyword, and the FROM keyword specifies the name of the table from which to retrieve the data. Click the "Play" button below the statement to see the results.
A simple SELECT statement returns all the rows in a table. Data is not filtered to retrieve a subset of the results, nor is it sorted. We’ll discuss these topics in the next few lessons.
---[SIDEBAR]---
"type":"tip",
"title":"Terminating Statements",
"text":"You can execute more than one SQL statement at the same time. Multiple SQL statements need to be separated by semicolons. For most database management systems, a semicolon is not required at the end of a single SQL statement executed alone. If your particular DBMS complains, you might have to add one. There is no harm in adding a semicolon when it is not required."
---[SIDEBAR]---
All extra white space within a SQL statement is ignored when that statement is processed. SQL statements can be specified on one long line or broken up over many lines. So, these three statements are will all be treated the same:
1. SELECT name
FROM    museum;
2. SELECT name FROM   museum;
3. SELECT
     name
FROM
     museum;
**Try the SQL statements.**
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-3
height:10
---[SQL]---
SELECT name
FROM museum;
SELECT name FROM museum;
SELECT
name
FROM
museum;
---[SQL]---
Most people who write a lot of SQL statements find that breaking them up over multiple lines makes them easier to read and debug.
## Retrieving Multiple Columns
The SELECT statement used to retrieve more than one column from a table is very similar to the statement used to retrieve a single column. The only difference is that each column must be specified right after the SELECT keyword. Each column is separated by a comma.
---[SIDEBAR]---
"type":"tip",
"title":"Be Careful with Commas",
"text":"When selecting multiple columns, be sure to specify a comma between each column name, but not after the last column name. Adding a comma that the end of the list of columns will result in an error."
---[SIDEBAR]---
Let's say you want see the name, city and country of each of the museums in the database. The following SELECT statement retrieves the three columns from the Museum table:
*SELECT name, city, country
FROM museum*
**Try the SQL statement.**
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:museum-3
height:3
---[SQL]---
SELECT name, city, country
FROM museum;
---[SQL]---
Just as in the prior example, this statement uses the SELECT statement to retrieve data from the "museum" table. In this example, three column names are specified, each separated by a comma.
---[SIDEBAR]---
"type":"note",
"title":"Ordering Columns",
"text":"You probably noticed that the columns are displayed in the query result in the order that they are specified after the keyword SELECT. You can display columns in any order by changing the order in the SELECT statement."
---[SIDEBAR]---
## Retrieving All Columns
There are times that you will want to retrieve all of the columns from a database table. One way is to list all of the column names in the the SELECT statement; however, there is a nice shortcut that you can use to save time. The asterisk "*" is a wildcard character that can be used to retrieve all columns from a table.
Suppose you want to get a quick look at all of the data on the museum table. The following SQL statement will retrieve all of the columns from the museum table:
*SELECT \*
FROM museum*
**Try the SQL statement.**
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
result:museum
owner:atlas-query
---[SQL]---
SELECT *
FROM museum;
---[SQL]---
When you use the wildcard (*), all columns in the table are returned. Generally, the columns will be returned in the order that they appear in the table. It is usually best to specify the columns that you want to retrieve in your query and not use the wildcard. Retrieving unneeded columns can slow the process of retrieving data from a database.
---[SIDEBAR]---
"type":"tip",
"title":"Using Wildcards to Explore Data",
"text":"It can be helpful to explore the data in a database to help you effectively write queries to retrieve the specific data you need. This is particularly true if you don't have a database diagram to show you the names of the columns in the database tables. Using the wildcard character to retrieve all of the columns in a database table give you a quick and easy way to explore the data stored on the table."
---[SIDEBAR]---
## Removing Duplicate Rows
All of the queries in this lesson have returned all of the rows from the source table. There are times that you won't want to retrieve all rows. Often the rows retrieved by a query repeat. For example, you may wish to list all of the country in the museum database with a museum. The SQL statement that retrieves the cities with museums is:
*SELECT country
FROM museum*
**Try the SQL statement.**
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:all-museum-countries
---[SQL]---
SELECT country
FROM museum;
---[SQL]---
Notice that the SQL statement returns 57 rows even though there are only 16 different countries on the museum table. Many of the countries displayed appear in the results more than once. It would be easier to use the results if each country were list only once.
You can edit this query to show only the unique values for country. This is done by adding the keyword DISTINCT directly after the keyword SELECT. The updated SQL statement would be:
*SELECT DISTINCT country
FROM museum*
**Try the SQL statement.**
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:distinct-museum-countries
---[SQL]---
SELECT DISTINCT country
FROM museum;
---[SQL]---
Notice that how adding the keyword DISTINCT reduces the result set to 16 rows. Each of the rows in the query result contains a unique country.
When you use DISTINCT in a SQL statement, it applies to all of the columns of the query not just the first column. For example, retrieving both the country and state (as shown below) will return more than 16 rows. A country with more than one state will appear once for every state on the table. The *combination* of countries and states will all be unique.
*SELECT DISINCT country, state
FROM museum*
**Try the SQL statement.**
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:distinct-comparison
---[SQL]---
SELECT
DISTINCT country,
state
FROM museum;
SELECT
DISTINCT country
FROM museum;
---[SQL]---
Notice how both queries in the example return a unique set of values. In the second query, the unique list of countries is returned. In the first, the unique combinations of countries and states are returned.
## Limiting Results
There are times that you want to retrieve only the first row or set of rows from a table. Unfortunately, the different database management systems have implemented this in different ways. A common way to retrieve only a few rows involves using the keyword LIMIT. For example, the SQL statement used to retrieve the first five rows of the museum table is:
*SELECT \*
FROM museum
LIMIT 5*
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:limit
---[SQL]---
SELECT *
FROM museum
LIMIT 5;
---[SQL]---
## Using Comments
SQL statements are instructions that are processed by the database management system to interact with the database. There are times that you want include notes in the the SQL statement that you don't want to be processed. These notes are called comments.
---[SIDEBAR]---
"type":"term",
"title":"Comments",
"text":"Notes placed inside a SQL statement that are not processed by the database management system."
---[SIDEBAR]---
Here are a few reasons you might want to leave some comment in your SQL statements:
1. The SQL statements you write can become complex. It is often helpful to leave descriptive comments in the statement to explain the logic of the statement.
2. You may want to leave a comment at the beginning of the SQL statement to describe what the statement does.
3. There are times you may want to troubleshoot a SQL statement that isn't working as you expect it to. It can be helpful to temporarily stop part of a statement from being executed, by putting it in a comment, to see the effect of removing that part of the statement.
Most database management systems support at least two ways to insert a comment. The first is an inline comment. An inline comment is added using two hyphens (--). Any text on the same line after the hyphens will not be processed. For example, the SQL statement below will retrieve the list of museums in the database, ignoring the text, "This is a comment" when the statement is processed.
*SELECT name --This is a comment
FROM museum*
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:inline-comment
---[SQL]---
SELECT name --This is a comment
FROM museum;
---[SQL]---
You can also create comments that can span across the lines of a SQL statement. Multiline comments begin with the combination of the slash and astericks characters "/*" and end with the astericks and slash characters "*/". For example, the text "This is a multiline comment" will not be processed in this SQL statement:
*SELECT name
/* This is a
multiline comment.*/
FROM museum
---[SQL]---
connection:sql_book_connection
schema:canvas
diagram:canvas
owner:atlas-query
result:block-comment
---[SQL]---
SELECT name
/* This is a
multiline comment*/
FROM museum
---[SQL]---
## Summary
This lesson discussed simple SQL statements that retrieve data from a database. It covered retrieving a single column and multiple columns. Additionally, it described how to use the wildcard character to retrieve all of the columns in a table. Finally, it covered how to retrieve only unique rows, a specified number of rows, and how to embed comments in a SQL statement.
## Practice
It's a good idea to take a few minutes now to practice what you have learned in this lesson
[Lesson Pracice](?select-where-assessment)