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:
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.