Introduction
A view lets you save a query so you can use it again easily without rewriting it.
Jump into concepts and practice - no test required
CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition;
CREATE VIEW active_customers AS SELECT id, name, email FROM customers WHERE status = 'active';
CREATE VIEW product_prices AS SELECT product_id, price FROM products;
CREATE VIEW recent_orders AS SELECT order_id, customer_id, order_date FROM orders WHERE order_date > '2024-01-01';
CREATE TABLE employees ( id INT, name VARCHAR(50), department VARCHAR(50), salary INT ); INSERT INTO employees (id, name, department, salary) VALUES (1, 'Alice', 'Sales', 50000), (2, 'Bob', 'HR', 45000), (3, 'Charlie', 'Sales', 55000); CREATE VIEW sales_team AS SELECT id, name, salary FROM employees WHERE department = 'Sales'; SELECT * FROM sales_team;
EmployeeView showing all columns from Employees table?Products with columns id, name, and price, and the view created as:CREATE VIEW CheapProducts AS SELECT id, name FROM Products WHERE price < 50;SELECT * FROM CheapProducts; return if Products contains:id | name | price 1 | Pen | 10 2 | Notebook| 60 3 | Eraser | 30
CREATE VIEW ActiveUsers AS SELECT id, name FROM Users WHERE active = 1;SELECT * FROM ActiveUsers; gives an error: ERROR: relation "activeusers" does not existRecentOrders that always shows orders placed in the last 7 days from the Orders table with columns order_id, customer_id, and order_date. Which SQL statement correctly creates this view?