Tag vite :

Using VALUES in SQL queries

 Posté par greg le 31 août 2011 14:34 |  Aucun commentaire
Tags : geek  computing  postgresql 

The power of VALUES

VALUES is a very useful SQL statement that can make very efficient queries. In fact, as soon as you write a bit of SQL, you use VALUES ... in the INSERT statement.

This example is very interesting to understand how VALUES can help. In fact, INSERT INTO can more generally look like this :


INSERT INTO my_table (field1, field2, ... fieldN) SELECT ... 

In fact, INSERT INTO ... VALUES is a special form of INSERT where VALUES allow you to specify a result set by hand. What advantages can we take on using this ? Let’s start with the example of one of my previous articles about window functions with the production table. Let’s say we want to query all the Chinese companies with a production greater or equal to 12, we would write the following query:


SELECT 
  p.* 
FROM 
  production p
WHERE 
    p.country='chine' 
  AND 
    p.production >= 12
;
 company_name | country | production 
--------------+---------+------------
 chang intl   | chine   |         15
 medong       | chine   |         12
(2 rows)

Using VALUES, it is possible to create a parameter like query:


SELECT 
  p.* FROM production p,
  (VALUES ('chine', 12)) data (country, production) 
WHERE 
    p.country=data.country 
  AND 
    p.production >= data.production
;
 company_name | country | production 
--------------+---------+------------
 chang intl   | chine   |         15
 medong       | chine   |         12
(2 rows)

Well, this query is longer, so what is the advantage of using VALUES here ? Well imagine now you have a matrix of requirements. You need to dump all US companies with a production >= 10, Chinese >=12, German > 5 and so on. The naive implementation would be to stack heaps of AND / OR statements but VALUES can be very powerful:


SELECT 
  p.* FROM production p,
  (VALUES 
    ('chine', 12),
    ('états-unis', 10),
    ('allemagne', 5)
  ) data (country, production) 
WHERE 
    p.country = data.country 
  AND 
    p.production >= data.production
;
 company_name |  country   | production 
--------------+------------+------------
 chang intl   | chine      |         15
 eep techno   | états-unis |         14
 futur corp   | états-unis |         12
 Glaß gmbh    | allemagne  |          8
 medong       | chine      |         12
(5 rows)

Enjoy !


Commentaires

Les commentaires utilisent la bibliothèque Textile restreinte. Amusez vous bien !

Soyez le premier à commenter ce post.

Liens récents 

 Crew - code review for Git projects.

Marqué par greg le 6 mai 2012 07:57..
Tags: geek computing git

Suivre le lien

 responsive grid system

Marqué par greg le 5 mai 2012 12:38..
Tags: geek computing dev

Suivre le lien

 Vim "improved

Marqué par greg le 8 mars 2012 09:26..
Tags: geek computing vim

Suivre le lien

 Highly scalable NoSql blog

Marqué par greg le 3 mars 2012 09:12..
Tags: geek computing dev

Suivre le lien

 Bruce Momjian Pg blog

Marqué par greg le 16 février 2012 13:51..
Tags: geek computing postgresql net blog

Suivre le lien

 Richard Miller's blog

Marqué par greg le 16 février 2012 09:57..
Tags: geek computing dev php symfony net blog

Suivre le lien

 Online SQL explain interface

Marqué par greg le 15 février 2012 09:58..
Tags: geek computing postgresql net

Suivre le lien

 Ace online code editor

Marqué par greg le 15 février 2012 08:57..
Tags: net geek computing dev javascript

Suivre le lien

 Coding horror

Marqué par greg le 14 février 2012 12:42..
Tags: geek computing dev net blog

Suivre le lien

 telegraphCQ - Stream oriented database

Marqué par greg le 13 février 2012 14:44..
Tags: geek computing postgresql

Suivre le lien