Drupal - Views & SQL Analogy - An easier way to explain Views

With over 4 million downloads, Views is the most used contributed module in Drupal. Every Drupal developer knows that Views is an absolutely inevitable part of almost every Drupal project. Using Views, one can build anything from simple list of content, slideshows, jump menus to RSS feeds, JSON feeds through to contextual blocks of information and even PDF and Excel downloads of data. That's why, in the Drupal development universe, understanding & effectively using views separates novices from experts!

This blog post is by Prasad Shirgaonkar who has recently joined our Learning Services team. He is also delivering training in India, you can meet him in person there. Check out our Site Building in Drupal 7 course to learn more about Views.

Explaining Views to Developers

At times, it becomes challenging to explain or teach Views to developers who are new to Drupal. This could be because something like Views doesn’t exist in any other CMS or possibly because of infinite use cases where Views could be used and potential configuration options for each use case.

So, we tend to sum up in a single line - Views is a powerful Query Builder for Drupal.

This builds excitement with newbies and motivates them to go to build a new view. All seems well and easy on the first page of creating a new view. Simple drop down options for selecting 'what to display', 'how to display' etc. And then, newbies go to Edit page of a View. Most new learners get overwhelmed by enormous amount of configuration options on View Edit page. Some are also likely to get confused and lost due to Views specific jargon used on the page! This is the time and place when many newcomers are likely to decide 'Drupal is very complex and Views are not my cup of tea!'

In some of the recent Drupal Training workshops, I discovered an easier way of teaching Views to developers, who have some previous experience but are new to Drupal. I explained Views by making an analogy with standard SQL Select Query! I had observed that most of the participants, although new to Drupal, had some web development experience. Most of the participants also knew SQL, either as a part of their college curriculum or as a part of their previous development experience. So I thought comparing Views configuration options with SQL syntax might gave them some insight about Views module.

Here's the diagram that I used for explaining:

Views as query builder

In a nutshell, I explained in the following manner - standard SQL select query is:

SELECT t1.field1, t2.field2 FROM table1 t1 INNER JOIN table2 t2
ON t1.fieldX = t2.fieldY WHERE t1.fieldZ = '%some value%' ORDER BY
t1.fieldW DESC

Comparing this with Views

  • SELECT {fields list } is in the Fields section.
  • FROM {tables list} - [This is a bit complex in Views, the base table is determined when we create a view - whether its node, users, terms, node revisions, files etc]
  • INNER JOIN - is in the Advance Options > Relationships section in Views
  • WHERE - is defined in the Filters & Contextual Filters sections
  • ORDER By - is determined by Sort Criteria

After explaining in this manner, I observed that participants in the workshops get the core concept of Views!

Reviewing the query

After, show participants they can see the query that Views created.

  1. 1Go to Structure > Views > Click the “Settings” tab, and scroll down to the Live Preview Settings.
  2. 2Click “Show the SQL query”
  3. 3Save configuration.
    Show SQL query.

After this, when you create a view, you will see the query output as you preview your view results.

query output

Hope this method helps other Drupal trainers so that they can help people to start the journey from being Drupal novice to an expert!