---[REF]---"style":"heading"---[REF]---
---[SIDEBAR]---
"type":"objectives",
"title":"Learning Objectives",
"text":"After completing this lesson, the successful student will be able to:
FROM work
WHERE width > (SELECT AVG(width) FROM work);" ---[EVALUATE]--- This query will retrieve the correct rows. The outer query displays all of the columns from the work table. The WHERE clause of the outer query is constructed to ensure that the rows included in the result have a value for the width column that is greater than the value returned by the subquery. The subquery calculates the average value for the width column of the work table. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas owner:atlas-query diagram:canvas result:works-above-average-width-subquery ---[SQL]--- SELECT * FROM work WHERE width > (SELECT AVG(width) FROM work); ---[SQL]--- Notice the syntax of the query. Whenever you use a subquery, you always enclose the subquery in parenthese. Additionally, the subquery replaces the hard-coded average in the previous query. In this case, the subquery calculates the average value. This calculated average is then used to create the logical test for the filter in the outer query. Constructing the query in this way makes accounts for any change to the average width that may occur when new works of art are added to the work table. A subquery in the WHERE clause is always used in similar ways. The subquery will return a value or a set of values that will be used in the outer query to complete a logical test. Consider a few examples. Suppose you want display all of the works by Renior using a subquery in the WHERE clause. ---[EVALUATE]--- "type":"plan", "task":"Display all of the works by Renior using a subquery in the WHERE clause.", "columns":"artwork name", "tables":"work", "rows":"artist_id for artist with last name of Renoir", "order":"none specified" ---[EVALUATE]---
FROM work
WHERE artist_id = (SELECT artist_id FROM artist WHERE last_name = 'Renoir');" ---[EVALUATE]--- This SQL statement retrieves the correct records. In this query, the operator in the WHERE clause includes a subquery that returns the artist_id for the artist whose last_name is Renoir. This value is then used in a logical test the filters the works of art to show only those whose artist_id matches the id returned by the subquery. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:renoir-works-subquery ---[SQL]--- SELECT name FROM work WHERE artist_id = (SELECT artist_id FROM artist WHERE last_name = 'Renoir'); ---[SQL]--- The subquery is necessary in this query, because the artist last_name column is not on the work table. This task could also be accomplished using a query that joins the work and artist tables. This is often the case with a subquery in the WHERE clause. Whether you use a subquery or a join query in these instances is a matter of personal preference. ---[SIDEBAR]--- "type":"caution", "title":"Subquery Performance", "text":"Using subqueries can be a processor intensive process for the DBMS. You may find that the DBMS can more easily process a join query than one with a subquery." ---[SIDEBAR]--- Notice the syntax of the logical test. It uses the '=' operator. This works because we expect only one artist_id to be returned by the subquery. If you think that a subquery might return more than value (perhaps more than one artist has a last name of Renoir), you would need to use the IN operator that can handle a list of values. For example, suppose you want to display the names of works of art by Canadian artists using a subquery in the WHERE clause. ---[EVALUATE]--- "type":"plan", "task":"Display the names of works of art by Canadian artists using a subquery in the WHERE clause.", "columns":"artwork name", "tables":"work", "rows":"artist_id for Canadian artists", "order":"none specified" ---[EVALUATE]---
FROM work
WHERE artist_id IN (SELECT artist_id FROM artist WHERE nationality = 'Canadian');" ---[EVALUATE]--- This SQL statement retrieves the correct records. In this query, the WHERE clause includes a subquery that returns the artist_id for the Canadian artists. This value is then used in a logical test the filters the works of art to show only those whose artist_id matches the ids returned by the subquery. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:canadian-artist-works-subquery ---[SQL]--- SELECT name FROM work WHERE artist_id IN (SELECT artist_id FROM artist WHERE nationality = 'Canadian'); ---[SQL]--- The subquery is necessary in this query, because the artist nationality column is not on the work table. Notice the syntax of the logical test. It uses the IN operator. This is necessary, because the subquery is likely to return more than one artist_id (there is likely more than one Canadian artist). ---[SIDEBAR]--- "type":"tip", "title":"Subqueries and Logical Operators", "text":"Recall that the IN operator handles a list of values and the '=' operator can be used to compare only a single value. You may not know that the IN operator can also handle a list of values that contains only a single value. For this reason, it is generally best to use the IN operator rather than the '=' operator when using a subquery in the WHERE clause. The IN operator can be used when the subquery returns any number of values, where the '=' operator only works when the subquery produces a single value." ---[SIDEBAR]--- Consider one final example of using a subquery in the WHERE clause. Suppose you wish to display all of the museums that exhibit the works of art created by Canadian artists. ---[EVALUATE]--- "type":"plan", "task":"Display all of the museums that exhibit the works of art created by Canadian artists.", "columns":"museum name", "tables":"museum", "rows":"Canadian artists", "order":"none specified" ---[EVALUATE]---
FROM museum
WHERE museum_id IN
   (SELECT museum_id FROM work WHERE artist_id IN
      (SELECT artist_id FROM artist WHERE nationality = 'Canadian')
   );" ---[EVALUATE]--- This SQL statement retrieves the correct records. In this query, the WHERE clause includes two subqueries. The first subquery returns the artist_id for the Canadian artists. The second uses these artist_ids to produce a set of museum_ids on the work table for the works of art created by Canadian artists. These values are then used in a logical test the filters the museums to show only those works of art by Canadian artists (defined by the subqueries). **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:museums-with-canadian-artist-works-subquery ---[SQL]--- SELECT museum.name FROM museum WHERE museum_id IN (SELECT museum_id FROM work WHERE artist_id IN (SELECT artist_id FROM artist WHERE nationality = 'Canadian')); ---[SQL]--- Notice that this query contains two subqueries. A query can theoretically contain any number of subqueries. As you review the subqueries in this section, you will notice that all of the subqueries could be executed as standalone queries. This means that each of these subqueries are independent subqueries. Also note one other commonallity. A subquery in the where clause will produce either a single value or a set of values to be used as part of a logical test. If the subquery returns anthing other than a value or values compatible with the logical test, it will not work. In the last example, if the inner most subquery returned a list of artist names rather than artist_ids it would not work. ### Using Subqueries in the SELECT clause A subquery used in the SELECT clause is not used as part of a logical test. Recall that the SELECT clause is used to define the columns and calculations that will be included in the query result. This means that a subquery in the SELECT clause will define a column that will be included in the query result. For example, a subquery could be used to display the number of works of art exhibited in each museum using a subquery in the SELECT clause. ---[EVALUATE]--- "type":"plan", "task":"Display the number of works of art exhibited in each museum using a subquery in the SELECT clause sorted by artwork_count from high to low.", "columns":"museum name, artwork_count", "tables":"museum", "rows":"all museums", "order":"descending order by artwork_count" ---[EVALUATE]--- A possible SQL statement that could be used to address this task is: SELECT name, (SELECT count(*) from work) AS artwork_count FROM museum ORDER BY artwork_count DESC; **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:museum-artwork-count-independent-subquery ---[SQL]--- SELECT name, (SELECT count(*) from work) AS artwork_count FROM museum ORDER BY artwork_count DESC; ---[SQL]--- Notice the syntax of the subquery. It is enclosed in parentheses as before. In this case, it is used just as you would specify any other column or calculation. It is part of the comma separated list of items to be displayed in the query result. Note how the subquery is renamed using the keyword AS so that the results of the subquery have a header label at the top of the column in the query results. In this case, the values returned by the subquery do not provide the correct results. The subquery returns the number of works of art fromt he work table. This value is repeated for each row of the query result. Instead, you would like to see the number of works of art exhibited in each museum. This would likely be different for each museum. To accomplish this, you need to use a dependent subquery.
FROM museum
ORDER BY artwork_count DESC;" ---[EVALUATE]--- This SQL statement retrieves the correct records. It displays the museum name and artwork_count for each museum. Unlike the last query, this one will calculate and display the number of works of art exhibited in each museum. This is done with the WHERE clause in the subquery. The condition WHERE work.museum_id = museum.museum_id makes it so the subquery will be processed for each row of the outer query. The museum_id for each row of the outer query is used in the subquery to determine which works of art to count (those that match the museum_id of the current row of the outer query). This means that the subquery is dependent on the outer query. It will not execute without the museum_ids of the outer query. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:museums-with-artwork-count-dependent-subquery ---[SQL]--- SELECT name, (SELECT COUNT(*) FROM work WHERE work.museum_id = museum.museum_id) AS artwork_count FROM museum ORDER BY artwork_count DESC; ---[SQL]--- The results display the name of each museum. They also show the number of works of art associated with each museum. The number of works of art for each museum is calculated using a dependent or correlated subquery. ### Using Subqueries in the FROM Clause It is also common to use a subquery in the FROM clause. Recall that the FROM clause is used to specify which tables are the source of the data for the query. A subquery in the FROM clause must produce a table - a set of columns and rows. You could use a subquery in the FROM clause to complete the task in the last example. That task was to display the names of museums and the number of works of art (artwork_count) exhibited in each museum. ---[EVALUATE]--- "type":"plan", "task":"Display the number of works of art exhibited in each museum using a subquery in the FROM clause sorted by artwork_count from high to low.", "columns":"museum name, artwork_count", "tables":"museum, subquery (with the artwork_count column)", "rows":"all museums", "order":"descending order by artwork_count" ---[EVALUATE]--- For this to work, the subquery in the FROM clause must include at least two things: 1. A calculated column, named artwork_count, based on the work table that counts the number of works of art in each museum. 2. A way to join the subquery with the museum table so that the museum names can be associated with the artwork_count for each museum.
FROM museum JOIN
   (SELECT museum_id, COUNT(*) AS artwork_count FROM work GROUP BY museum_id) subquery
ON museum.museum_id = subquery.museum_id
ORDER BY artwork_count DESC;" ---[EVALUATE]--- This SQL statement retrieves the correct results. It displays the museum name and artwork_count for each museum. In this query, the subquery returns the museum_id and the count of the works of art for each museum grouped by the museum_id. The subquery is then joined with the museum table on the museum_id column in the museum table and the subquery. The museum name column is displayed from the museum table and the artwork_count column is displayed from the subquery. The results are sorted by artwork_count in descending order. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:museums-with-artwork-count-from-subquery ---[SQL]--- SELECT name, artwork_count FROM museum JOIN (SELECT museum_id, COUNT(*) AS artwork_count FROM work GROUP BY museum_id) subquery ON museum.museum_id = subquery.museum_id ORDER BY artwork_count DESC; ---[SQL]--- The results display the name of each museum. They also show the number of works of art associated with each museum. The number of works of art for each museum is calculated using a subquery in the FROM clause. This subquery is technically an independent subquery, since it could be executed without the outer query. ## Summary In this lesson, you learned how to use subqueries. In particular, you learned how to use subqueries in the WHERE SELECT, and FROM clauses. Specifically, we discussed that: * A subquery in the WHERE clause returns a value or set of values to be used in a logical test in the WHERE clause. * A subquery in the SELECT clause returns a column or calculated column that is displayed in the query results. * A subquery in the FROM clause creates a table (a set of columns and rows) that can be used as a source of the data for the query.
- Describe what a subquery is.
- Use a subquery to construct a filter condition in the WHERE clause.
- Use a subquery to create a calculated field the SELECT clause.
- Use a subquery to a create a temporary table in the FROM clause.
FROM work
WHERE width > (SELECT AVG(width) FROM work);" ---[EVALUATE]--- This query will retrieve the correct rows. The outer query displays all of the columns from the work table. The WHERE clause of the outer query is constructed to ensure that the rows included in the result have a value for the width column that is greater than the value returned by the subquery. The subquery calculates the average value for the width column of the work table. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas owner:atlas-query diagram:canvas result:works-above-average-width-subquery ---[SQL]--- SELECT * FROM work WHERE width > (SELECT AVG(width) FROM work); ---[SQL]--- Notice the syntax of the query. Whenever you use a subquery, you always enclose the subquery in parenthese. Additionally, the subquery replaces the hard-coded average in the previous query. In this case, the subquery calculates the average value. This calculated average is then used to create the logical test for the filter in the outer query. Constructing the query in this way makes accounts for any change to the average width that may occur when new works of art are added to the work table. A subquery in the WHERE clause is always used in similar ways. The subquery will return a value or a set of values that will be used in the outer query to complete a logical test. Consider a few examples. Suppose you want display all of the works by Renior using a subquery in the WHERE clause. ---[EVALUATE]--- "type":"plan", "task":"Display all of the works by Renior using a subquery in the WHERE clause.", "columns":"artwork name", "tables":"work", "rows":"artist_id for artist with last name of Renoir", "order":"none specified" ---[EVALUATE]---
The SELECT statement to retrieve this data is:
SELECT name
FROM work
WHERE artist_id = (SELECT artist_id FROM artist WHERE last_name = 'Renoir');
---[EVALUATE]---
"type":"evaluate",
"query":"SELECT name FROM work
WHERE artist_id = (SELECT artist_id FROM artist WHERE last_name = 'Renoir');" ---[EVALUATE]--- This SQL statement retrieves the correct records. In this query, the operator in the WHERE clause includes a subquery that returns the artist_id for the artist whose last_name is Renoir. This value is then used in a logical test the filters the works of art to show only those whose artist_id matches the id returned by the subquery. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:renoir-works-subquery ---[SQL]--- SELECT name FROM work WHERE artist_id = (SELECT artist_id FROM artist WHERE last_name = 'Renoir'); ---[SQL]--- The subquery is necessary in this query, because the artist last_name column is not on the work table. This task could also be accomplished using a query that joins the work and artist tables. This is often the case with a subquery in the WHERE clause. Whether you use a subquery or a join query in these instances is a matter of personal preference. ---[SIDEBAR]--- "type":"caution", "title":"Subquery Performance", "text":"Using subqueries can be a processor intensive process for the DBMS. You may find that the DBMS can more easily process a join query than one with a subquery." ---[SIDEBAR]--- Notice the syntax of the logical test. It uses the '=' operator. This works because we expect only one artist_id to be returned by the subquery. If you think that a subquery might return more than value (perhaps more than one artist has a last name of Renoir), you would need to use the IN operator that can handle a list of values. For example, suppose you want to display the names of works of art by Canadian artists using a subquery in the WHERE clause. ---[EVALUATE]--- "type":"plan", "task":"Display the names of works of art by Canadian artists using a subquery in the WHERE clause.", "columns":"artwork name", "tables":"work", "rows":"artist_id for Canadian artists", "order":"none specified" ---[EVALUATE]---
The SELECT statement to retrieve this data is:
SELECT name
FROM work
WHERE artist_id IN (SELECT artist_id FROM artist WHERE nationality = 'Canadian');
---[EVALUATE]---
"type":"evaluate",
"query":"SELECT name FROM work
WHERE artist_id IN (SELECT artist_id FROM artist WHERE nationality = 'Canadian');" ---[EVALUATE]--- This SQL statement retrieves the correct records. In this query, the WHERE clause includes a subquery that returns the artist_id for the Canadian artists. This value is then used in a logical test the filters the works of art to show only those whose artist_id matches the ids returned by the subquery. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:canadian-artist-works-subquery ---[SQL]--- SELECT name FROM work WHERE artist_id IN (SELECT artist_id FROM artist WHERE nationality = 'Canadian'); ---[SQL]--- The subquery is necessary in this query, because the artist nationality column is not on the work table. Notice the syntax of the logical test. It uses the IN operator. This is necessary, because the subquery is likely to return more than one artist_id (there is likely more than one Canadian artist). ---[SIDEBAR]--- "type":"tip", "title":"Subqueries and Logical Operators", "text":"Recall that the IN operator handles a list of values and the '=' operator can be used to compare only a single value. You may not know that the IN operator can also handle a list of values that contains only a single value. For this reason, it is generally best to use the IN operator rather than the '=' operator when using a subquery in the WHERE clause. The IN operator can be used when the subquery returns any number of values, where the '=' operator only works when the subquery produces a single value." ---[SIDEBAR]--- Consider one final example of using a subquery in the WHERE clause. Suppose you wish to display all of the museums that exhibit the works of art created by Canadian artists. ---[EVALUATE]--- "type":"plan", "task":"Display all of the museums that exhibit the works of art created by Canadian artists.", "columns":"museum name", "tables":"museum", "rows":"Canadian artists", "order":"none specified" ---[EVALUATE]---
The SELECT statement to retrieve this data is:
SELECT museum.name
FROM museum
WHERE museum_id IN (SELECT museum_id FROM work WHERE artist_id IN (SELECT artist_id FROM artist WHERE nationality = 'Canadian'));
---[EVALUATE]---
"type":"evaluate",
"query":"SELECT museum.name FROM museum
WHERE museum_id IN
   (SELECT museum_id FROM work WHERE artist_id IN
      (SELECT artist_id FROM artist WHERE nationality = 'Canadian')
   );" ---[EVALUATE]--- This SQL statement retrieves the correct records. In this query, the WHERE clause includes two subqueries. The first subquery returns the artist_id for the Canadian artists. The second uses these artist_ids to produce a set of museum_ids on the work table for the works of art created by Canadian artists. These values are then used in a logical test the filters the museums to show only those works of art by Canadian artists (defined by the subqueries). **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:museums-with-canadian-artist-works-subquery ---[SQL]--- SELECT museum.name FROM museum WHERE museum_id IN (SELECT museum_id FROM work WHERE artist_id IN (SELECT artist_id FROM artist WHERE nationality = 'Canadian')); ---[SQL]--- Notice that this query contains two subqueries. A query can theoretically contain any number of subqueries. As you review the subqueries in this section, you will notice that all of the subqueries could be executed as standalone queries. This means that each of these subqueries are independent subqueries. Also note one other commonallity. A subquery in the where clause will produce either a single value or a set of values to be used as part of a logical test. If the subquery returns anthing other than a value or values compatible with the logical test, it will not work. In the last example, if the inner most subquery returned a list of artist names rather than artist_ids it would not work. ### Using Subqueries in the SELECT clause A subquery used in the SELECT clause is not used as part of a logical test. Recall that the SELECT clause is used to define the columns and calculations that will be included in the query result. This means that a subquery in the SELECT clause will define a column that will be included in the query result. For example, a subquery could be used to display the number of works of art exhibited in each museum using a subquery in the SELECT clause. ---[EVALUATE]--- "type":"plan", "task":"Display the number of works of art exhibited in each museum using a subquery in the SELECT clause sorted by artwork_count from high to low.", "columns":"museum name, artwork_count", "tables":"museum", "rows":"all museums", "order":"descending order by artwork_count" ---[EVALUATE]--- A possible SQL statement that could be used to address this task is: SELECT name, (SELECT count(*) from work) AS artwork_count FROM museum ORDER BY artwork_count DESC; **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:museum-artwork-count-independent-subquery ---[SQL]--- SELECT name, (SELECT count(*) from work) AS artwork_count FROM museum ORDER BY artwork_count DESC; ---[SQL]--- Notice the syntax of the subquery. It is enclosed in parentheses as before. In this case, it is used just as you would specify any other column or calculation. It is part of the comma separated list of items to be displayed in the query result. Note how the subquery is renamed using the keyword AS so that the results of the subquery have a header label at the top of the column in the query results. In this case, the values returned by the subquery do not provide the correct results. The subquery returns the number of works of art fromt he work table. This value is repeated for each row of the query result. Instead, you would like to see the number of works of art exhibited in each museum. This would likely be different for each museum. To accomplish this, you need to use a dependent subquery.
The SELECT statement to retrieve this data is:
SELECT name, (SELECT COUNT(*) FROM work WHERE work.museum_id = museum.museum_id) AS artwork_count
FROM museum
ORDER BY artwork_count DESC;
---[EVALUATE]---
"type":"evaluate",
"query":"SELECT name, (SELECT COUNT(*) FROM work WHERE work.museum_id = museum.museum_id) AS artwork_count FROM museum
ORDER BY artwork_count DESC;" ---[EVALUATE]--- This SQL statement retrieves the correct records. It displays the museum name and artwork_count for each museum. Unlike the last query, this one will calculate and display the number of works of art exhibited in each museum. This is done with the WHERE clause in the subquery. The condition WHERE work.museum_id = museum.museum_id makes it so the subquery will be processed for each row of the outer query. The museum_id for each row of the outer query is used in the subquery to determine which works of art to count (those that match the museum_id of the current row of the outer query). This means that the subquery is dependent on the outer query. It will not execute without the museum_ids of the outer query. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:museums-with-artwork-count-dependent-subquery ---[SQL]--- SELECT name, (SELECT COUNT(*) FROM work WHERE work.museum_id = museum.museum_id) AS artwork_count FROM museum ORDER BY artwork_count DESC; ---[SQL]--- The results display the name of each museum. They also show the number of works of art associated with each museum. The number of works of art for each museum is calculated using a dependent or correlated subquery. ### Using Subqueries in the FROM Clause It is also common to use a subquery in the FROM clause. Recall that the FROM clause is used to specify which tables are the source of the data for the query. A subquery in the FROM clause must produce a table - a set of columns and rows. You could use a subquery in the FROM clause to complete the task in the last example. That task was to display the names of museums and the number of works of art (artwork_count) exhibited in each museum. ---[EVALUATE]--- "type":"plan", "task":"Display the number of works of art exhibited in each museum using a subquery in the FROM clause sorted by artwork_count from high to low.", "columns":"museum name, artwork_count", "tables":"museum, subquery (with the artwork_count column)", "rows":"all museums", "order":"descending order by artwork_count" ---[EVALUATE]--- For this to work, the subquery in the FROM clause must include at least two things: 1. A calculated column, named artwork_count, based on the work table that counts the number of works of art in each museum. 2. A way to join the subquery with the museum table so that the museum names can be associated with the artwork_count for each museum.
The SELECT statement to retrieve this data is:
SELECT name, artwork_count
FROM museum JOIN (SELECT museum_id, COUNT(*) AS artwork_count FROM work GROUP BY museum_id) subquery
ON museum.museum_id = subquery.museum_id
ORDER BY artwork_count DESC;
---[EVALUATE]---
"type":"evaluate",
"query":"SELECT name, artwork_count FROM museum JOIN
   (SELECT museum_id, COUNT(*) AS artwork_count FROM work GROUP BY museum_id) subquery
ON museum.museum_id = subquery.museum_id
ORDER BY artwork_count DESC;" ---[EVALUATE]--- This SQL statement retrieves the correct results. It displays the museum name and artwork_count for each museum. In this query, the subquery returns the museum_id and the count of the works of art for each museum grouped by the museum_id. The subquery is then joined with the museum table on the museum_id column in the museum table and the subquery. The museum name column is displayed from the museum table and the artwork_count column is displayed from the subquery. The results are sorted by artwork_count in descending order. **Try the SQL statement.** ---[SQL]--- connection:sql_book_connection schema:canvas diagram:canvas owner:atlas-query result:museums-with-artwork-count-from-subquery ---[SQL]--- SELECT name, artwork_count FROM museum JOIN (SELECT museum_id, COUNT(*) AS artwork_count FROM work GROUP BY museum_id) subquery ON museum.museum_id = subquery.museum_id ORDER BY artwork_count DESC; ---[SQL]--- The results display the name of each museum. They also show the number of works of art associated with each museum. The number of works of art for each museum is calculated using a subquery in the FROM clause. This subquery is technically an independent subquery, since it could be executed without the outer query. ## Summary In this lesson, you learned how to use subqueries. In particular, you learned how to use subqueries in the WHERE SELECT, and FROM clauses. Specifically, we discussed that: * A subquery in the WHERE clause returns a value or set of values to be used in a logical test in the WHERE clause. * A subquery in the SELECT clause returns a column or calculated column that is displayed in the query results. * A subquery in the FROM clause creates a table (a set of columns and rows) that can be used as a source of the data for the query.