Tag finder:

Using VALUES in SQL queries

 Posted by greg on August 31, 2011 2:34 PM |  No comments
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 !


Comments

Comments are using Textile restricted Markup language. Enjoy !

Be the first to comment this post.

Recent Links 

 Bruce Momjian Pg blog

Bookmarked by greg on February 16, 2012 1:51 PM..
Tags: geek computing postgresql net blog

Follow link

 Richard Miller's blog

Bookmarked by greg on February 16, 2012 9:57 AM..
Tags: geek computing dev php symfony net blog

Follow link

 Online SQL explain interface

Bookmarked by greg on February 15, 2012 9:58 AM..
Tags: geek computing postgresql net

Follow link

 Ace online code editor

Bookmarked by greg on February 15, 2012 8:57 AM..
Tags: net geek computing dev javascript

Follow link

 Coding horror

Bookmarked by greg on February 14, 2012 12:42 PM..
Tags: geek computing dev net blog

Follow link

 telegraphCQ - Stream oriented database

Bookmarked by greg on February 13, 2012 2:44 PM..
Tags: geek computing postgresql

Follow link

 Relational Database Technologies

Bookmarked by greg on February 13, 2012 1:28 PM..
Tags: geek computing postgresql net blog

Follow link

 J.Davis blog «Ideas on Databases»

Bookmarked by greg on February 12, 2012 8:59 PM..
Tags: geek computing postgresql net blog

Follow link

 Flotr2, javascript graph library

Bookmarked by greg on February 6, 2012 8:17 AM..
Tags: geek computing dev javascript

Follow link

 Node.js toolbox

Bookmarked by greg on January 31, 2012 10:09 AM..
Tags: geek computing dev javascript nodejs

Follow link