Views

Using SQL code to create views

We can create a View to act as a representation of our data in a format that is useful to us. For example, in the last section we ran an SQL query with a join to give us a summary of the employees and their departments.

select emp_no, first_name, last_name, gender, birth_date, dept_name, hire_date from employees 
join departments on employees.dept_id = departments.dept_id order by last_name;

Once this query has been run in DB Browser it can be saved as a View.

Run the above SQL query and in the results screen select the icon indicated in the following image.

Save View

Select 'Save as view' and give the view a name. Now when you select the 'Browse Data' tab this View will be available in addition to the employees and the departments tables.

One advantage of using a View is that any changes to the underlying table will be reflected in the View. For example, changing the name of a department in the departments table will be reflected in every row where that department name appears in the view.

Try changing the name 'Quality Management' to 'Quality Assurance' in the departments table, save the changes by clicking on 'Write Changes' and open the view again to review the changes.