---[REF]---"style":"heading"---[REF]---
---[SIDEBAR]---
"type":"objectives",
"title":"Learning Objectives",
"text":"After completing this lesson, the successful student will be able to:
FROM museum;" ---[EVALUATE]--- The query is ready to try. The name, city, and country columns will be included in the query result, because they are listed in the SELECT clause. Notice that the three columns are separated by commas. The FROM clause ensures that the data will come from the museum table. All rows of the museum table will be returned. The query does not specify any order for the displayed rows. **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]--- ---[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. You could 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. ---[EVALUATE]--- "type":"plan", "task":"List all of the data stored on the museum table.", "columns":"All (*)", "tables":"museum", "rows":"All", "order":"Not specified" ---[EVALUATE]--- The following SQL statement will retrieve all of the columns from the museum table: SELECT * FROM museum; ---[EVALUATE]--- "type":"evaluate", "task":"List all of the data stored on the museum table.", "query":"SELECT *
FROM museum;" ---[EVALUATE]--- Using the * character in the SELECT clause will return all of the columns on the table. The museum table is set in the FROM clause. All rows will be displayed and the order of the rows is not specified. **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 don't want to retrieve all rows. Often the rows retrieved by a query repeat. For example, you may wish to list all of the countries in the museum database with a museum. ---[EVALUATE]--- "type":"plan", "task":"List all of the countries with a museum.", "columns":"country", "tables":"museum", "rows":"Only the unique countries", "order":"Not specified" ---[EVALUATE]--- The SQL statement that retrieves the cities with museums is: SELECT country FROM museum; ---[EVALUATE]--- "type":"evaluate", "task":"List all of the countries with a museum.", "query": "SELECT country
FROM museum;", "row_issues":"All rows (even duplicate countries) will be displayed" ---[EVALUATE]--- Although the SQL statement is not correct, it is instructive to see what the query will produce. **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; ---[EVALUATE]--- "type":"evaluate", "task":"List all of the countries with a museum.", "query": "SELECT DISTINCT country
FROM museum;" ---[EVALUATE]--- With the keyword DISTINCT in the SELECT clause, only the unique countries will be displayed. **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. ---[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. ## 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. ---[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: ---[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.
- 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.
- Add comments to a SQL statement.
---[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/AVvXsEh7hX_BsoPaH4zbcUkf25Qf4QBazzQDsi4XqDY6Y9Tef5ODWacEiCNh2gTyXnk5egU7xQljugE1OxqtSZi3tGO8QixxI9zh-GY00O1uwPshuEYqw-r7pFrAEvmBDQu4OxklYro62CgO_RiDA_KCabOk4E0s34yC-_o_mUYKe_V-XqMqi1Et7P_nLHguBEU/s16000/canvas-trans.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.
---[EVALUATE]---
"type":"plan",
"task":"List the names of all museums.",
"columns":"name",
"tables":"museum"
---[EVALUATE]---
You can use this SQL statement to do that:
SELECT name
FROM museum;
---[EVALUATE]---
"type":"evaluate",
"query":"SELECT name
FROM museum;" ---[EVALUATE]---
The query will return the "name" column from the "museum" table. The SELECT statement is set to retrieve a single column called "name" from the "museum" table. The desired column name is specified right after the SELECT keyword. The FROM keyword specifies the name of the table from which to retrieve the data. In this case, the rows will come from the museum table. A SELECT statement without a filter (discussed in a later lesson) returns all the rows in a table. This query does not have a filter nor is it sorted (also discussed in a later lesson).
**Try the SQL statement.**
Click the "Play" button below the statement to see the results.
---[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.
---[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-name
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.
---[EVALUATE]---
"type":"plan",
"task":"List the names, cities, and countries of all museums.",
"columns":"name, city, country",
"tables":"museum",
"rows":"All",
"order":"Not specified"
---[EVALUATE]---
FROM museum;" ---[EVALUATE]---
The following SELECT statement retrieves the three columns from the Museum table:
SELECT name, city, country
FROM museum;
---[EVALUATE]---
"type":"evaluate",
"query":"SELECT name, city, countryFROM museum;" ---[EVALUATE]--- The query is ready to try. The name, city, and country columns will be included in the query result, because they are listed in the SELECT clause. Notice that the three columns are separated by commas. The FROM clause ensures that the data will come from the museum table. All rows of the museum table will be returned. The query does not specify any order for the displayed rows. **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]--- ---[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. You could 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. ---[EVALUATE]--- "type":"plan", "task":"List all of the data stored on the museum table.", "columns":"All (*)", "tables":"museum", "rows":"All", "order":"Not specified" ---[EVALUATE]--- The following SQL statement will retrieve all of the columns from the museum table: SELECT * FROM museum; ---[EVALUATE]--- "type":"evaluate", "task":"List all of the data stored on the museum table.", "query":"SELECT *
FROM museum;" ---[EVALUATE]--- Using the * character in the SELECT clause will return all of the columns on the table. The museum table is set in the FROM clause. All rows will be displayed and the order of the rows is not specified. **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 don't want to retrieve all rows. Often the rows retrieved by a query repeat. For example, you may wish to list all of the countries in the museum database with a museum. ---[EVALUATE]--- "type":"plan", "task":"List all of the countries with a museum.", "columns":"country", "tables":"museum", "rows":"Only the unique countries", "order":"Not specified" ---[EVALUATE]--- The SQL statement that retrieves the cities with museums is: SELECT country FROM museum; ---[EVALUATE]--- "type":"evaluate", "task":"List all of the countries with a museum.", "query": "SELECT country
FROM museum;", "row_issues":"All rows (even duplicate countries) will be displayed" ---[EVALUATE]--- Although the SQL statement is not correct, it is instructive to see what the query will produce. **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; ---[EVALUATE]--- "type":"evaluate", "task":"List all of the countries with a museum.", "query": "SELECT DISTINCT country
FROM museum;" ---[EVALUATE]--- With the keyword DISTINCT in the SELECT clause, only the unique countries will be displayed. **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. ---[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. ## 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. ---[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: ---[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.