{"id":13791,"date":"2023-12-29T01:39:24","date_gmt":"2023-12-29T08:39:24","guid":{"rendered":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/"},"modified":"2023-12-29T01:39:59","modified_gmt":"2023-12-29T08:39:59","slug":"mysql-create-view","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/","title":{"rendered":"MySQL CREATE VIEW"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the MySQL <code>CREATE VIEW<\/code> &nbsp;statement to create a new view in the database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL CREATE VIEW statement<\/h2>\n\n\n\n<p>The <code>CREATE VIEW<\/code> statement creates a new view in the database. Here is the basic syntax of the <code>CREATE VIEW<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> &#91;<span class=\"hljs-keyword\">OR<\/span> <span class=\"hljs-keyword\">REPLACE<\/span>] <span class=\"hljs-keyword\">VIEW<\/span> &#91;db_name.]view_name &#91;(column_list)]\n<span class=\"hljs-keyword\">AS<\/span>\n  <span class=\"hljs-keyword\">select<\/span>-<span class=\"hljs-keyword\">statement<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<p>First, specify the name of the view that you want to create after the <code>CREATE VIEW<\/code> keywords. The name of the view is unique in a database. Because views and tables in the same database share the same namespace, the name a view cannot the same as the name of an existing table.<\/p>\n\n\n\n<p>Second, use the <code>OR REPLACE<\/code> option if you want to replace an existing view if the view already exists. If the view does not exist, the <code>OR REPLACE<\/code> has no effect.<\/p>\n\n\n\n<p>Third, specify a list of columns for the view. By default, the columns of the view are derived from the select list of the <code>SELECT<\/code> statement. However, you can explicitly specify the column list for the view by listing them in parentheses following the view name.<\/p>\n\n\n\n<p>Finally,&nbsp; specify a <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">SELECT<\/a><\/code> statement that defines the view. The <code>SELECT<\/code>&nbsp; statement can query data from tables or views. MySQL allows you to use the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\">ORDER BY<\/a><\/code> clause in the <code>SELECT<\/code> statement but ignores it if you select from the view with a query that has its own <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<p>By default, the <code>CREATE VIEW<\/code> statement creates a view in the current database. If you want to explicitly create a view in a given database, you can qualify the view name with the database name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL CREATE VIEW examples<\/h2>\n\n\n\n<p>Let&#8217;s take some example of using the <code>CREATE VIEW<\/code> statement to create new views.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Creating a simple view example<\/h3>\n\n\n\n<p>Let&#8217;s take a look at the <code>orderDetails<\/code> table from the <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"144\" height=\"144\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/orderdetails.png\" alt=\"\" class=\"wp-image-7995\"\/><\/figure>\n\n\n\n<p>This statement uses the <code>CREATE VIEW<\/code> statement to create a view that represents total sales per order.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">VIEW<\/span> salePerOrder <span class=\"hljs-keyword\">AS<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        orderNumber, \n        <span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered * priceEach) total\n    <span class=\"hljs-keyword\">FROM<\/span>\n        orderDetails\n    <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">by<\/span> orderNumber\n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> total <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you&nbsp;use the <code>SHOW TABLE<\/code> command to view all tables in the <code>classicmodels<\/code> database, you will see the view<code>salesPerOrder<\/code> is showing up in the list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">TABLES<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"147\" height=\"218\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/show-table-view.jpg\" alt=\"show table view\" class=\"wp-image-4136\"\/><\/figure>\n\n\n\n<p>This is because the views and tables share the same namespace as mentioned earlier.<\/p>\n\n\n\n<p>To know which object is a view or table, you use the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-show-tables\/\">SHOW FULL TABLES<\/a><\/code> command as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">FULL<\/span> <span class=\"hljs-keyword\">TABLES<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"233\" height=\"221\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/show-full-table-command.jpg\" alt=\"show full table command\" class=\"wp-image-4137\"\/><\/figure>\n\n\n\n<p>The <code>table_type<\/code> column in the result set specifies the type of the object: view or table (base table).<\/p>\n\n\n\n<p>If you want to query total sales for each sales order, you just need to execute a simple <code>SELECT<\/code> &nbsp;statement against the <code>SalePerOrder<\/code> &nbsp;view as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> salePerOrder;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"160\" height=\"235\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-create-view-simple-view-example.png\" alt=\"mysql create view - simple view example\" class=\"wp-image-7996\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-create-view-simple-view-example.png 160w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-create-view-simple-view-example-136x200.png 136w\" sizes=\"auto, (max-width: 160px) 100vw, 160px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2) Creating a view based on another view example<\/h3>\n\n\n\n<p>MySQL allows you to create a view based on another view.<\/p>\n\n\n\n<p>For example, you can create a view called <code>bigSalesOrder<\/code> based on the <code>salesPerOrder<\/code> view to show every sales order whose total is greater than <code>60,000<\/code> as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">VIEW<\/span> bigSalesOrder <span class=\"hljs-keyword\">AS<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        orderNumber, \n        <span class=\"hljs-keyword\">ROUND<\/span>(total,<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">as<\/span> total\n    <span class=\"hljs-keyword\">FROM<\/span>\n        salePerOrder\n    <span class=\"hljs-keyword\">WHERE<\/span>\n        total &gt; <span class=\"hljs-number\">60000<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, you can query the data from the <code>bigSalesOrder<\/code> view as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    orderNumber, \n    total\n<span class=\"hljs-keyword\">FROM<\/span>\n    bigSalesOrder;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"172\" height=\"90\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/create-a-view-based-on-another-view.jpg\" alt=\"create a view based on another view\" class=\"wp-image-4138\" title=\"create a view based on another view\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3) Creating a view with join example<\/h3>\n\n\n\n<p>The following example uses the <code>CREATE VIEW<\/code> statement to create a view based on multiple tables. It uses the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-inner-join\/\">INNER JOIN<\/a><\/code> clauses to join tables.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR<\/span> <span class=\"hljs-keyword\">REPLACE<\/span> <span class=\"hljs-keyword\">VIEW<\/span> customerOrders <span class=\"hljs-keyword\">AS<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> \n    orderNumber,\n    customerName,\n    <span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered * priceEach) total\n<span class=\"hljs-keyword\">FROM<\/span>\n    orderDetails\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> orders o <span class=\"hljs-keyword\">USING<\/span> (orderNumber)\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> customers <span class=\"hljs-keyword\">USING<\/span> (customerNumber)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> orderNumber;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This statement selects data from the <code>customerOrders<\/code> view:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> customerOrders \n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> total <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This picture shows the partial output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"312\" height=\"237\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-create-view-with-join-example.png\" alt=\"\" class=\"wp-image-7997\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-create-view-with-join-example.png 312w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-create-view-with-join-example-200x152.png 200w\" sizes=\"auto, (max-width: 312px) 100vw, 312px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4) Creating a view with a subquery example<\/h3>\n\n\n\n<p>The following example uses the <code>CREATE VIEW<\/code> statement to create a view whose <code>SELECT<\/code> statement uses a <a title=\"MySQL Subquery\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-subquery\/\">subquery<\/a>. The view contains products whose buy prices are higher than the average price of all products.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">VIEW<\/span> aboveAvgProducts <span class=\"hljs-keyword\">AS<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        productCode, \n        productName, \n        buyPrice\n    <span class=\"hljs-keyword\">FROM<\/span>\n        products\n    <span class=\"hljs-keyword\">WHERE<\/span>\n        buyPrice &gt; (\n            <span class=\"hljs-keyword\">SELECT<\/span> \n                <span class=\"hljs-keyword\">AVG<\/span>(buyPrice)\n            <span class=\"hljs-keyword\">FROM<\/span>\n                products)\n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> buyPrice <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This query data from the <code>aboveAvgProducts<\/code> is simple as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> aboveAvgProducts;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"337\" height=\"198\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-view-with-subquery.png\" alt=\"mysql view with subquery\" class=\"wp-image-3757\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-view-with-subquery.png 337w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/mysql-view-with-subquery-300x176.png 300w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">5) Creating a view with explicit view columns example<\/h3>\n\n\n\n<p>This statement uses the <code>CREATE VIEW<\/code> statement to create a new view based on the customers and orders tables with explicit view columns:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">VIEW<\/span> customerOrderStats (\n   customerName , \n   orderCount\n) \n<span class=\"hljs-keyword\">AS<\/span>\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        customerName, \n        <span class=\"hljs-keyword\">COUNT<\/span>(orderNumber)\n    <span class=\"hljs-keyword\">FROM<\/span>\n        customers\n            <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n        orders <span class=\"hljs-keyword\">USING<\/span> (customerNumber)\n    <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> customerName;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This query returns data from the <code>customerOrderStats<\/code> view:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    customerName,\n    orderCount\n<span class=\"hljs-keyword\">FROM<\/span>\n    customerOrderStats\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n\torderCount, \n    customerName;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"273\" height=\"242\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-create-view-with-explicit-columns.png\" alt=\"\" class=\"wp-image-7890\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-create-view-with-explicit-columns.png 273w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-create-view-with-explicit-columns-200x177.png 200w\" sizes=\"auto, (max-width: 273px) 100vw, 273px\" \/><\/figure>\n\n\n\n<p>In this tutorial, we have shown you how to use the MySQL <code>CREATE VIEW<\/code> statement to create views in the database.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"13791\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/\"\n\t\t\t\tdata-post-title=\"MySQL CREATE VIEW\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"13791\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/\"\n\t\t\t\tdata-post-title=\"MySQL CREATE VIEW\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use the MySQL CREATE VIEW &nbsp;statement to create a new view in the database. Introduction to MySQL CREATE VIEW statement The CREATE VIEW statement creates a new view in the database. Here is the basic syntax of the CREATE VIEW statement: In this syntax: First, specify [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":555,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-13791","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL CREATE VIEW<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to create views in MySQL by using CREATE VIEW statement.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL CREATE VIEW\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to create views in MySQL by using CREATE VIEW statement.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-29T08:39:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/orderdetails.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-views\\\/mysql-create-view\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-views\\\/mysql-create-view\\\/\",\"name\":\"MySQL CREATE VIEW\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-views\\\/mysql-create-view\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-views\\\/mysql-create-view\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/orderdetails.png\",\"datePublished\":\"2023-12-29T08:39:24+00:00\",\"dateModified\":\"2023-12-29T08:39:59+00:00\",\"description\":\"In this tutorial, you will learn how to create views in MySQL by using CREATE VIEW statement.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-views\\\/mysql-create-view\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-views\\\/mysql-create-view\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-views\\\/mysql-create-view\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/orderdetails.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/orderdetails.png\",\"width\":144,\"height\":144},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-views\\\/mysql-create-view\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Views\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-views\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL CREATE VIEW\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL CREATE VIEW","description":"In this tutorial, you will learn how to create views in MySQL by using CREATE VIEW statement.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/","og_locale":"en_US","og_type":"article","og_title":"MySQL CREATE VIEW","og_description":"In this tutorial, you will learn how to create views in MySQL by using CREATE VIEW statement.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-29T08:39:59+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/orderdetails.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/","url":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/","name":"MySQL CREATE VIEW","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/orderdetails.png","datePublished":"2023-12-29T08:39:24+00:00","dateModified":"2023-12-29T08:39:59+00:00","description":"In this tutorial, you will learn how to create views in MySQL by using CREATE VIEW statement.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/orderdetails.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/orderdetails.png","width":144,"height":144},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-views\/mysql-create-view\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Views","item":"https:\/\/www.mysqltutorial.org\/mysql-views\/"},{"@type":"ListItem","position":3,"name":"MySQL CREATE VIEW"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=13791"}],"version-history":[{"count":2,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13791\/revisions"}],"predecessor-version":[{"id":13793,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/13791\/revisions\/13793"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/555"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=13791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}