Thread: what is the purpose of a statement that starts with a WITH


Permlink Replies: 4 - Pages: 1 - Last Post: Aug 29, 2007 12:11 AM Last Post By: William Robertson
Suzie

Posts: 467
Registered: 01/26/05
what is the purpose of a statement that starts with a WITH
Posted: Aug 28, 2007 1:27 PM
Click to report abuse...   Click to reply to this thread Reply
I saw sql statement that starts with a WITH command what is the purpose of it...

soemthing like

WITH category AS (select category.name, category.id,
smoradi

Posts: 259
Registered: 04/24/01
Re: what is the purpose of a statement that starts with a WITH
Posted: Aug 28, 2007 1:34 PM   in response to: Suzie in response to: Suzie
Click to report abuse...   Click to reply to this thread Reply
This is from Oracle documentation:

The WITH query_name clause lets you assign a name to a subquery block. You can then reference the subquery block multiple places in the query by specifying the query name. Oracle Database optimizes the query by treating the query name as either an inline view or as a temporary table.

You can specify this clause in any top-level SELECT statement and in most types of subqueries. The query name is visible to the main query and to all subsequent subqueries except the subquery that defines the query name itself.
Helio Dias

Posts: 461
Registered: 06/09/04
Re: what is the purpose of a statement that starts with a WITH
Posted: Aug 28, 2007 8:36 PM   in response to: Suzie in response to: Suzie
Click to report abuse...   Click to reply to this thread Reply
The With have two distinct propose, that could be combine.
First: make the query more clean (more readable) eg:

select col1 alias1,col2 alias2,col3 alias3,col4 alias4,Col5 alias5,col6 alias6,col7 alias 7,col7 alias8, col1+col2 alias1_2
from tab1,tab2,tab3,tab4,tab5,tab6
where
tab1.col10=tab2.col10 and
tab2.col11=tab3.col11 and
tab3.col12=tab4.col12 and
tab4.col13=tab5.col13 and
tab5.col14=tab6.col14
and tab1.col30=10
union all
select col1 alias1,col2 alias2,col3 alias3,col4 alias4,Col5 alias5,col6 alias6,col7 alias 7,col7 alias8, col1+col2 alias1_2
from tab1,tab2,tab3,tab4,tab5,tab6
where
tab1.col10=tab2.col10 and
tab2.col11=tab3.col11 and
tab3.col12=tab4.col12 and
tab4.col13=tab5.col13 and
tab5.col14=tab6.col14
and tab1.col30=15
union all
select col1 alias1,col2 alias2,col3 alias3,col4 alias4,Col5 alias5,col6 alias6,col7 alias 7,col7 alias8, col1+col2 alias1_2
from tab1,tab2,tab3,tab4,tab5,tab6
where
tab1.col10=tab2.col10 and
tab2.col11=tab3.col11 and
tab3.col12=tab4.col12 and
tab4.col13=tab5.col13 and
tab5.col14=tab6.col14
and tab1.col30=20
union all
select col1 alias1,col2 alias2,col3 alias3,col4 alias4,Col5 alias5,col6 alias6,col7 alias 7,col7 alias8, col1+col2 alias1_2
from tab1,tab2,tab3,tab4,tab5,tab6
where
tab1.col10=tab2.col10 and
tab2.col11=tab3.col11 and
tab3.col12=tab4.col12 and
tab4.col13=tab5.col13 and
tab5.col14=tab6.col14
and tab1.col30=25
union all
select col1 alias1,col2 alias2,col3 alias3,col4 alias4,Col5 alias5,col6 alias6,col7 alias 7,col7 alias8, col1+col2 alias1_2
from tab1,tab2,tab3,tab4,tab5,tab6
where
tab1.col10=tab2.col10 and
tab2.col11=tab3.col11 and
tab3.col12=tab4.col12 and
tab4.col13=tab5.col13 and
tab5.col14=tab6.col14
and tab1.col30=30;

Now compare With
With base_table as
(select col1 alias1,col2 alias2,col3 alias3,col4 alias4,Col5 alias5,col6 alias6,col7 alias 7,col7 alias8, col1+col2 alias1_2
from tab1,tab2,tab3,tab4,tab5,tab6
where
tab1.col10=tab2.col10 and
tab2.col11=tab3.col11 and
tab3.col12=tab4.col12 and
tab4.col13=tab5.col13 and
tab5.col14=tab6.col14)
select * from base_table where tab1.col30=10
union all
select * from base_table where tab1.col30=15
union all
select * from base_table where tab1.col30=20
union all
select * from base_table where tab1.col30=25
union all
select * from base_table where tab1.col30=30

I know that in both cases i could use OR but it´s just to ilustrate

So Oracle will have two diferents behaviours,
If selectivity is bad , than Oracle will simply expand the base_table as if was a view.
But if selectivity is good than Oracle execute just once, and give you only the result set.

Regards
Helio Dias
http://heliodias.com

Hans Forbrich

Posts: 9,948
Registered: 03/13/99
Re: what is the purpose of a statement that starts with a WITH
Posted: Aug 28, 2007 10:00 PM   in response to: Suzie in response to: Suzie
Click to report abuse...   Click to reply to this thread Reply
I saw sql statement that starts with a WITH command
what is the purpose of it...

soemthing like

WITH category AS (select category.name, category.id,


It's a named subquery (multiples allowed) that you can use later in your main query.

Very handy
- if the subquery is repeated in the main query, this saves typing AND may improve performance a lot;

- you can use it to create dummy tables in-memory tables for test and demo purposes, instead of creating real tables, such as

Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production

SQL> WITH testit AS (
2 SELECT 'ABC' x FROM DUAL UNION
3 SELECT 'DEF' x FROM DUAL UNION
4 SELECT 'GHI' x FROM DUAL )
5 SELECT x FROM testit;

X
---
ABC
DEF
GHI

SQL>
William Robertson

Posts: 6,456
Registered: 06/17/98
Re: what is the purpose of a statement that starts with a WITH
Posted: Aug 29, 2007 12:11 AM   in response to: Suzie in response to: Suzie
Click to report abuse...   Click to reply to this thread Reply
This construction is known as subquery factoring. It was a new feature in 9i.
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums