tags:

views:

84

answers:

3

Question: In which four clauses can a subquery be used? (Choose four.)

A. in the INTO clause of an INSERT statement

B. in the FROM clause of a SELECT statement

C. in the GROUP BY clause of a SELECT statement

D. in the WHERE clause of a SELECT statement

E. in the SET clause of an UPDATE statement

F. in the VALUES clause of an INSERT statement

Answer: B, D, E, F

But I think right answers are the following: A, B, D, E BUT NOT F. Is not it so???

+1  A: 

Hi,

First of all we must acknowledge that a subquery returns a set of value tuples (rows): {(value_1_A, value_1_B, ...), (value_2_A, value_2_B, ...), ...}

A - The INTO clause expects a table name, not a set of values so you may not use a subquery. Note that this is not similar to B because you are allowed to query an anonymous table (a set of rows returned by a subquery), but it makes no sense to insert values into an anonymous table that will not be used.

F - The VALUES clause of the INSERT statement expects exactly what I said a subquery returns, a set of values.

The right answer will be B, D, E and F.

Here's some more info on subqueries: http://www.techonthenet.com/oracle/subqueries.php

Alin Purcaru
insert into (select col1,col2 from table1)values ('k','k')COMMIT;it will insert rows into table1 try
kupa
I tried on (MySQL) and it did not work. Unfortunately I can't test on Oracle, but I think your query makes no sense. It may work, but it makes no sense. (Arguments can be found in my answer.)
Alin Purcaru
@Alin, Oracle (and other RDBMS) allow writable views, and basically the same rules apply to the subquery here. But as I wrote in my answer, the subquery cannot generally be used there but only if it basically uses the same structure/rules as a writable view.
Lucero
+3  A: 

No, B, D, E, F sounds correct.

Why do you thik A should be valid? The INTO is designating a target rowset, which cannot be a subquery. And in F you can use subqueries which return a scalar value just fine.

Lucero
insert into (select col1,col2 from table1) values ('k','k') COMMIT; it will insert rows into table1 tryand can you tell me what do you mean when you say "And in F you can use subqueries which return a scalar value just fine"
kupa
If subquery in the `INSERT INTO` is defined such that the columns to be added are unambigious, it may accept it; but that is just a special case and not a general rule. Try making the query more complex (for instance by adding a function call, like `SELECT GETDATE() AS col1, col2 FROM table1`), and it won't work anymore. Regarding scalar values, I already explained in the comment to the question.
Lucero
Great!!! I understand what you mean,answer "A" is not true because it doesn't work always especially for your case "SELECT GETDATE() AS col1, col2 FROM table1" But the question "can be used" doesn't mean that in some case can be used it is not forced that always must be used
kupa
+2  A: 
kupa