KevsRobots Learning Platform
By Kevin McAleer, 6 Minutes
In this lesson, weβll explore views and indexes, two powerful tools that improve database usability and performance. Views allow you to simplify complex queries by saving them as virtual tables, while indexes optimize query speed by improving data retrieval efficiency.
A view is a virtual table in SQL that is defined by a query. It doesnβt store data itself but provides a simplified representation of data from one or more tables. Views are useful for creating reusable query results, simplifying complex queries, and improving data security by limiting access to specific data.
You create a view using the CREATE VIEW statement. A view can be as simple or complex as necessary, depending on the query used to define it.
CREATE VIEW SyntaxCREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Suppose we have an employees table with employee details:
employees
| emp_id | name | salary | dept_id |
|--------|----------|--------|---------|
| 1 | Alice | 50000 | 1 |
| 2 | Bob | 60000 | 2 |
| 3 | Charlie | 70000 | 1 |
Example: Create a view called high_earners for employees earning over $60,000.
CREATE VIEW high_earners AS
SELECT name, salary
FROM employees
WHERE salary > 60000;
Now, you can retrieve high earners using a simple SELECT on the high_earners view:
SELECT * FROM high_earners;
To update a viewβs definition, use the CREATE OR REPLACE VIEW statement. This will redefine the view without requiring you to drop it first.
Example: Update the high_earners view to include only employees earning over $65,000.
CREATE OR REPLACE VIEW high_earners AS
SELECT name, salary
FROM employees
WHERE salary > 65000;
This command updates the high_earners view with the new salary threshold.
To remove a view from the database, use the DROP VIEW statement.
DROP VIEW SyntaxDROP VIEW view_name;
Example: Drop the high_earners view.
DROP VIEW high_earners;
An index is a database structure that improves the speed of data retrieval. It works like an index in a book, allowing the database to locate data more quickly. Indexes are particularly useful for large tables or columns frequently used in WHERE, JOIN, or ORDER BY clauses.
Note: While indexes improve read performance, they can slow down write operations (e.g.,
INSERT,UPDATE,DELETE) because the index needs updating whenever the data changes.
To create an index, use the CREATE INDEX statement. You can create indexes on one or more columns to optimize queries on those columns.
CREATE INDEX SyntaxCREATE INDEX index_name ON table_name (column1, column2, ...);
Example: Create an index on the salary column of the employees table to speed up queries that filter by salary.
CREATE INDEX idx_salary ON employees (salary);
This index helps optimize queries such as:
SELECT * FROM employees
WHERE salary > 60000;
A composite index is an index on multiple columns. Itβs useful when queries frequently use multiple columns in the WHERE clause.
Example: Create a composite index on dept_id and salary in the employees table.
CREATE INDEX idx_dept_salary ON employees (dept_id, salary);
This index will optimize queries that filter by both dept_id and salary.
To remove an index from the database, use the DROP INDEX statement.
DROP INDEX SyntaxDROP INDEX index_name;
Example: Drop the idx_salary index.
DROP INDEX idx_salary;
WHERE, JOIN, or ORDER BY clauses.Create a View: Create a view called engineering_staff that shows employees in the Engineering department.
CREATE VIEW engineering_staff AS
SELECT name, salary
FROM employees
WHERE dept_id = 1;
Query the View: Retrieve all data from the engineering_staff view.
SELECT * FROM engineering_staff;
Create an Index: Create an index on the dept_id column of the employees table.
CREATE INDEX idx_dept_id ON employees (dept_id);
Test the Index: Run a query that filters by dept_id and observe the improved performance.
SELECT * FROM employees
WHERE dept_id = 1;
Drop the View: Drop the engineering_staff view.
DROP VIEW engineering_staff;
Hereβs a quick summary of the key concepts and commands related to views and indexes:
| Feature | Purpose | Example |
|---|---|---|
| View | A virtual table created by a saved query | CREATE VIEW view_name AS SELECT ... |
| Index | A structure that improves data retrieval speed | CREATE INDEX index_name ON table (col) |
| Composite Index | An index on multiple columns | CREATE INDEX index_name ON table (col1, col2) |
Views and indexes are valuable tools for optimizing and simplifying your SQL workflows. In the next lesson, weβll dive into SQL functions and expressions, which will further enhance your ability to manipulate and analyze data.
You can use the arrows β β on your keyboard to navigate between lessons.
Comments