Creating a View Based on Another View

The following view is created to count the employees based on the job title from the employee_details view. We use the COUNT( ) to get the number of employees which we group by job title.

CREATE VIEW employee_count_by_job_title AS
SELECT job_title, COUNT(*) AS employee_count
FROM employee_details
GROUP BY job_title;
SELECT * FROM employee_count_by_job_title ;

Output:

Creating a view based on another view

Explanation: In the above query, we have creates a virtual table named employee_count_by_job_title that counts the number of employees for each job title. It selects the job title and the count of employees with that title from the employee_details view, grouping the results by job title. The SELECT * FROM employee_count_by_job_title statement then retrieves the data from this view.

MariaDB Create View

Views in MariaDB are powerful tools that allow us to create virtual tables based on the result set of a SELECT query. They provide a way to simplify complex queries, improve security by limiting access to certain columns,, and enhance performance by precomputing expensive operations. In this article, we will explore how to create views in MariaDB along with multiple examples.

Similar Reads

How to Create View in MariaDB?

Creating views in MariaDB involves defining a query that selects data from one or more tables and then saving this query as a view. Views can be used to simplify complex queries, provide a layer of abstraction over the underlying tables, and improve performance by storing the results of frequently used queries....

Creating a View Based on Multiple Tables

The following view is created to display the employees name along with their department and job title with respect to their id. The INNER JOIN keyword is used to match the id of employees, departments and jobs table to provide the respective result....

Creating a View Based on Another View

The following view is created to count the employees based on the job title from the employee_details view. We use the COUNT( ) to get the number of employees which we group by job title....

Creating a View Based on The Summary Data of other Tables

The following view is used to get the count of the employees based on the department. We use the COUNT( ) to get the number of employees which we group by department name. To join the employees and department table we use the INNER JOIN keyword to display result by matching the employees id to the respective department....

Conclusion

MariaDB CREATE VIEW statement is a very useful and time saving features as we can save the SELECT statements which is frequently being queried. It can also be used to simplify complex queries and manipulate them and you can also use it to replace an existing query without any deletion. Knowing when to use the statement is a deciding factor for efficient data monitoring and management....

Contact Us