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

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. Go to Structure > Views > Click the “Settings” tab, and scroll down to the Live Preview Settings.
  2. Click “Show the SQL query”
  3. Save 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!


Posted on by Chris Estes (non vérifié).

I've been using SQL for 15 years. I've struggled with Views and people who said it's a "simple query builder" made me mad. This explains what they mean. And, knowing SQL, I think most of them were just repeating something they've heard but don't understand. A good SQL developer is going to have more trouble with a GUI than with just writing the code. Ask anyone who's ever been forced to use Frontpage....

Posted on by Michael Anello.

While I think you hit the nail on the head, don't discount the other half of what Views provides: an awesome display engine.

When I teach Views to Drupal newbies, I describe it as a query generator plus a display engine.


Posted on by Prasad Shirgaonkar.

You are spot on! This indeed explains just half the story - the query building part of Views... The display engine part of Views is indeed equally if not more powerful. Also, its unique to Drupal Views, no parallels anywhere to make an analogy!!

Ajouter un commentaire

Plain text

  • Aucune balise HTML autorisée.
  • Les adresses de pages web et de courriels sont transformées en liens automatiquement.
  • Les lignes et les paragraphes vont à la ligne automatiquement.

Filtered HTML

  • Use [acphone_sales], [acphone_sales_text], [acphone_support], [acphone_international], [acphone_devcloud], [acphone_extra1] and [acphone_extra2] as placeholders for Acquia phone numbers. Add class "acquia-phones-link" to wrapper element to make number a link.
  • Pour publier des morceaux de code, entourez-les avec les balises <code>...</code>. Pour du PHP, utilisez. <?php ... ?>, ce qui va colorier le code en fonction de sa syntaxe.
  • Les adresses de pages web et de courriels sont transformées en liens automatiquement.
  • Tags HTML autorisés : <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <h4> <h5> <h2> <img>
  • Les lignes et les paragraphes vont à la ligne automatiquement.
By submitting this form, you accept the Mollom privacy policy.