Use Sophia to knock out your gen-ed requirements quickly and affordably. Learn more
×

VIEW & Complex Queries

Author: Sophia

what's covered
This tutorial explores using views to provide a useful report on a complex set of data in two parts:
  1. Introduction
  2. VIEW In Practice

1. Introduction

Views can also be used to help simplify very complex queries such as queries that require subqueries or ones that use aggregate data. This can get very complex without the use of views, especially if you have to write out the statements each time. The use of the views helps simplify a lot of that underlying content.

2. VIEW In Practice

For example, we may have a view created to list the calculated total of the amount per country:


CREATE VIEW invoice_country
AS
SELECT billing_country, SUM(quantity*unit_price) AS calculated_total, MAX(quantity*unit_price) AS max_total, MIN(quantity*unit_price) AS min_total
FROM invoice
INNER JOIN invoice_line ON invoice.invoice_id = invoice_line.invoice_id
GROUP BY invoice.billing_country;

This can get complex to look at specific criteria on the aggregate calculations but querying the view would result in the following results:


SELECT * 
FROM invoice_country
ORDER BY calculated_total ASC, billing_country;

table

From there, we may want to filter that data out further by doing:


SELECT * 
FROM invoice_country
WHERE max_total = 1.99
ORDER BY calculated_total ASC, billing_country;

table

Compare this to the full statement on the base tables to get that same information:


SELECT billing_country, SUM(quantity*unit_price) AS calculated_total, MAX(quantity*unit_price) AS max_total, MIN(quantity*unit_price) AS min_total
FROM invoice
INNER JOIN invoice_line
ON invoice.invoice_id = invoice_line.invoice_id
GROUP BY invoice.billing_country
HAVING MAX(quantity*unit_price)  = 1.99;

table

We can take the data from the view and use the information to query other tables. Using the same example above, if we find that the max_total = 1.99, we may want to list all of the customers in those countries while listing all of the countries' calculated criteria:


SELECT * 
FROM invoice_country
INNER JOIN customer ON invoice_country.billing_country = customer.country
WHERE max_total = 1.99;

table

Think about what that the query would need to look like if you queried this using just the base tables. Consider the complexity of that type of statement. Aggregate data scenarios like this would be very difficult to create without the use of views.

Imagine, too, if you wanted to link all of the tables in our database together through a consistent data set to get a list of the track names that a customer has purchased. Rather than writing that query each time, having a view that joins all of the tables together may make it much simpler to query.


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 want the query to provide.

summary
Complex queries can be created with views to help simplify a lot of the processing, especially when using aggregate functions.

Source: Authored by Vincent Tran