
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we create a MySQL view with a subquery?
To illustrate the making of MySQL view with subquery we are using the following data from the table ‘Cars’ −
mysql> select * from cars; +------+--------------+---------+ | ID | Name | Price | +------+--------------+---------+ | 1 | Nexa | 750000 | | 2 | Maruti Swift | 450000 | | 3 | BMW | 4450000 | | 4 | VOLVO | 2250000 | | 5 | Alto | 250000 | | 6 | Skoda | 1250000 | | 7 | Toyota | 2400000 | | 8 | Ford | 1100000 | +------+--------------+---------+ 8 rows in set (0.08 sec)
Now, the following query will create a view named ‘cars_avgprice’ by using a subquery that will supply the values to the view. The subquery must be enclosed within parentheses.
mysql> Create view cars_avgprice AS SELECT NAME, Price FROM Cars WHERE price > (SELECT AVG(Price) from cars); Query OK, 0 rows affected (0.12 sec) mysql> Select * from cars_avgprice; +--------+---------+ | NAME | Price | +--------+---------+ | BMW | 4450000 | | VOLVO | 2250000 | | Toyota | 2400000 | +--------+---------+ 3 rows in set (0.03 sec)
If we will run the above subquery individually we can understand how view got its values −
mysql> Select AVG(Price) from cars; +--------------+ | AVG(Price) | +--------------+ | 1612500.0000 | +--------------+ 1 row in set (0.00 sec)
That is why the view ‘cars_avgprice’ has the list of cars having price more than the average of price i.e. 1612500.
- Related Articles
- How can we create a MySQL view with LEFT JOIN?
- How can we create a MySQL view with INNER JOIN?
- How can we create a MySQL view with RIGHT JOIN?
- How can we modify a MySQL view with CREATE OR REPLACE VIEW statement?
- How can we create a MySQL view with GROUP BY clause?
- How can we use a MySQL subquery with INSERT statement?
- How can we use a MySQL subquery with FROM clause?
- How can we create a MySQL view based on another existing view?
- How can we create the MySQL view with ORDER BY clause?
- How can we nest a subquery within another subquery?
- How can we filter data with the help of MySQL subquery?
- How can we create a MySQL view by using data from multiple tables?
- How can we drop a MySQL view from the database?
- How can we create a MySQL view by selecting some range of values from a base table?
- Can we create a database with a numeric name with MySQL?

Advertisements