Question: Subqueries Sophia Sophia PDF Version WHAT'S COVERED This tutorial explores subqueries and using them in SELECT statements in three parts: Subquery Basics Considering Subquery Results
Subqueries
Sophia
Sophia
PDF Version
WHAT'S COVERED
This tutorial explores subqueries and using them in SELECT statements in three parts:
Subquery Basics
Considering Subquery Results
Adding Multiple Columns
Subquery Basics
There are many instances when we may want to use subqueries to create more complex types of queries. Very frequently, we will see this with aggregate calculations. For example, we may want to find all of the invoices that have the total amount larger than the average total amount.
We could do this in two separate steps, based on what weve learned so far. We could first calculate the average total:
SELECT ROUNDAVGtotal
FROM invoice;
query results
Next, we can take that value and query the invoice table to find those that are larger than
SELECT invoiceid invoicedate,customerid total
FROM invoice
WHERE total ;
table
Although this approach works, we can also do it in a way that can pass the result from the first query that calculated the average into the second query by using a subquery. The subquery can be nested in a SELECT, INSERT, DELETE, or UPDATE statement, but we mostly see it in a SELECT statement.
To construct the subquery, we would enclose the second query in round brackets and place it in the WHERE clause as an expression. So in our original second statement:
SELECT invoiceid invoicedate,customerid total
FROM invoice
WHERE total ;
We would remove the and replace it with round brackets enclosing the first statement, like this:
SELECT invoiceid invoicedate,customerid total
FROM invoice
WHERE total SELECT ROUNDAVGtotal FROM invoice;
The query inside the round brackets is called the subquery, or inner query. The query that contains the subquery is called the outer query. Notice that we dont include the semicolon at the end of the inner statement in the subquery.
The database would execute the subquery first, then take the result from that subquery and pass it to the outer query. Then, the database would execute the outer query.
Considering Subquery Results
A subquery could potentially return or more results, so it is important to think about the operator that is used to compare with the results. For example, if we use a operator, we would expect that the subquery would return or row as a result.
Let's look at an obvious subquery that is querying on the customerid to return the customerid:
SELECT invoiceid invoicedate,customerid total
FROM invoice
WHERE customerid
SELECT customerid
FROM customer
WHERE customerid ;
Since the primary key of the table is the customerid querying on the customerid would only return one value.
table
If we tried to select a value that doesnt exist:
SELECT invoiceid invoicedate,customerid total
FROM invoice
WHERE customerid
SELECT customerid
FROM customer
WHERE customerid ;
The results would still run, but show that rows were displayed:
query results
However, in the case that we have multiple rows being returned:
SELECT invoiceid invoicedate,customerid total
FROM invoice
WHERE customerid
SELECT customerid
FROM customer
WHERE country 'USA';
Since there are customers that live in the country USA, we will end up getting this error:
query results
In order to avoid this error, we have to use the IN operator instead of the equal sign. This will allow or many results to be returned:
SELECT invoiceid invoicedate, customerid total
FROM invoice
WHERE customerid IN
SELECT customerid
FROM customer
WHERE country 'USA';
table
Adding Multiple Columns
We can also add in multiple columns as criteria to compare with the subquery. In order to do so we must use the round brackets around the columns within the WHERE clause and have them match up with the columns that we want to compare within the subquery. For example, if we wanted to compare the customerid and the billingcountry in the invoice table with the customerid and country in the customer table, we could do the following:
SELECT invoiceid invoicedate,customerid total
FROM invoice
WHERE customeridbillingcountry IN
SELECT customerid country
FROM customer
WHERE country 'USA';
table
If we did not include the round brackets around the customers to be compared to we would get an error:
SELECT invoiceid invoicedate,customerid total
FROM invoice
WHERE customerid billingcountry IN
SELECT customerid country
FROM customer
WHERE country 'USA';
query results
Play Video
VIDEO TRANSCRIPT
TRY IT
Your turn! Open the SQL tool by clicking on the LAUNCH DATABASE button below. Then enter in one of the examples above and see how it works. Next, try your own choices for which columns you wan
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
