++++
Data Engineering
May 2026×8 min read

Create reusable saved queries with MySQL views, query them like tables, and update or drop them safely.

17. Views

Driptanil Datta
Driptanil DattaSoftware Developer

Views

A view is a saved query that you can query like a table. It does not usually store data itself; it stores the SQL definition.

Create a view

CREATE VIEW paid_order_summary AS
SELECT
  customer_id,
  COUNT(*) AS paid_order_count,
  SUM(total_amount) AS paid_total
FROM orders
WHERE status = "paid"
GROUP BY customer_id;

Now query the view:

SELECT *
FROM paid_order_summary
ORDER BY paid_total DESC;

Views with joins

CREATE VIEW customer_order_details AS
SELECT
  c.customer_id,
  c.first_name,
  c.last_name,
  o.order_id,
  o.order_date,
  o.status,
  o.total_amount
FROM customers AS c
INNER JOIN orders AS o
  ON c.customer_id = o.customer_id;

Use the view:

SELECT *
FROM customer_order_details
WHERE status = "paid";

Replace a view

Use CREATE OR REPLACE VIEW when changing the definition.

CREATE OR REPLACE VIEW paid_order_summary AS
SELECT
  customer_id,
  COUNT(*) AS paid_order_count,
  SUM(total_amount) AS paid_total,
  AVG(total_amount) AS average_paid_order
FROM orders
WHERE status = "paid"
GROUP BY customer_id;

Drop a view

DROP VIEW paid_order_summary;

When to use views

  • To reuse common reporting logic.
  • To hide complex joins from beginners or dashboards.
  • To expose only selected columns.
  • To keep business definitions consistent.

Views are not a replacement for well-designed tables, but they are useful for making repeated queries easier to maintain.

Drip

Driptanil Datta

Software Developer

Building full-stack systems, one commit at a time. This blog is a centralized learning archive for developers.

Legal Notes
Disclaimer

The content provided on this blog is for educational and informational purposes only. While I strive for accuracy, all information is provided "as is" without any warranties of completeness, reliability, or accuracy. Any action you take upon the information found on this website is strictly at your own risk.

Copyright & IP

Certain technical content, interview questions, and datasets are curated from external educational sources to provide a centralized learning resource. Respect for original authorship is maintained; no copyright infringement is intended. All trademarks, logos, and brand names are the property of their respective owners.

System Operational

© 2026 Driptanil Datta. All rights reserved.