0

I understand that SQL views are essentially prewritten queries, but can a user query from multiple views using commands like JOIN and DISTINCT etc. or are they only used for very basic queries?

Daniel64
  • 29
  • 2
  • 8
  • 2
    Of course you can. Nonmaterialized view is nothing more than query with own name. – Lukasz Szozda Sep 27 '18 at 19:00
  • Sorry, what about a materialized view? – Daniel64 Sep 27 '18 at 19:04
  • [Materialized view](https://en.wikipedia.org/wiki/Materialized_view) – Lukasz Szozda Sep 27 '18 at 19:06
  • 1
    You can reference views in a query just like you would reference tables. – trincot Sep 27 '18 at 19:07
  • 1
    For the most part, with `SELECT`s, Views aren't a whole lot different than Tables in the database. The main benefits of Views are that they can hide complex query logic and they can provide for much tighter security. – Shawn Sep 27 '18 at 19:10
  • Does [this](https://stackoverflow.com/questions/30982732/select-statement-on-two-different-views) not answer your question? – trincot Sep 27 '18 at 19:13
  • @LukaszSzozda Note that MySQL doesn't have materialized views. You can emulate them somewhat with temporary tables. – Barmar Sep 27 '18 at 19:43

1 Answers1

1

Views can be very handy when you are doing complex queries over multiple tables. I use them on a daily basis to preserve some queries and clean up what has been written. You can write queries with views, the query written may not look complex until you look into each view itself. I also use views for my basic queries, for example I'm always having to pull up active employee's or use that for a report. I built a view that does just that, so when I need it I don't have to rewrite it I just pull it into my query.

Alex
  • 211
  • 1
  • 8