Site icon TestingDocs.com

MySQL Inline Views

Overview

In this tutorial, we will learn about MySQL inline views with example. An inline view is a special type of subquery that appears in the FROM clause.

A subquery that creates a derived table that can be used in the FROM clause of a SELECT statement
is called as an inline view.  Inline views are sometimes called  as virtual tables.

Syntax

SELECT…. FROM (subquery) AS  table_alias_name

When a derived table is used in a query, it should be given a table alias We can accomplish this with the AS clause that will act as the table alias.

Example

In the following query, the average of the sums of the population of each continent is determined:

mysql> SELECT *
-> FROM (SELECT Continent, SUM(Population) AS Continent_Population_Sum
-> FROM Country
-> GROUP BY Continent)
-> AS temp
-> ORDER BY Continent;


+—————+————————–+
| Continent | Continent_Population_Sum |
+—————+————————–+
| Asia | 3705025700 |
| Europe | 730074600 |
| North America | 482993000 |
| Africa | 784475000 |
| Oceania | 30401150 |
| Antarctica | 0 |
| South America | 353186400 |
+—————+————————–+
7 rows in set (0.00 sec)

 

Every table that appears in a FROM clause must have a name, so a subquery in the FROM clause must be
followed by a table alias. The SELECT in the FROM clause is a table subquery. Note that the subqueries in FROM clauses cannot be correlated with the outer query statement.

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Exit mobile version