Original address: http://blog.csdn.net/shangboerds/article/details/43983791
--Start
One thing that these keywords have in common is that they are generally used in sub-queries. Everyone is familiar with in and EXISTS, here I do not introduce, below we look at how to use the other several keywords, first, we define the following table:
[SQL]View Plaincopyprint?
- --Student
- CREATE TABLE STUDENT
- (
- ID VARCHAR2 (8), ---school number
- Name VARCHAR2, ---name
- Class VARCHAR2, ---class
- Chinese number (5), ---language scores
- Math Number (5) ---math score
- );
- INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090001 ', ' Zhang San ', ' Grade A ', 80, 90);
- INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090002 ', ' John Doe ', ' Grade A ', 60, 75 );
- INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090003 ', ' Harry ', ' Grade A ', 90, 95);
- INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090004 ', ' Yangwen ', ' grade B ', 70, 90 );
- INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090004 ', ' Li Bai ', ' Grade B ', 85, 80);
- INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090005 ', ' King Blue ', ' Grade B ', N ULL, 70);
Suppose now let you inquire, Class A which student mathematics result is higher than Class B mathematics result minimum value, how to do? We can use the following SQL:
[SQL]View Plaincopyprint?
- SELECT NAME from STUDENT WHERE class=' Grade A class ' and MATH >
- (
- SELECT MIN (MATH) from STUDENT WHERE class=' Grade B class '
- );
In addition, we can also use some or any. Note: Any and SOME work exactly the same way as they are used.
[SQL]View Plaincopyprint?
- SELECT NAME from STUDENT WHERE class=' V Class A ' and MATH > any
- (
- SELECT MATH from STUDENT WHERE class=' Grade B class '
- );
Suppose now let you inquire, Class A which student mathematics result is higher than Class B mathematics result maximum, how to do? We can use the following sql:
[SQL]View Plaincopyprint?
- SELECT NAME from STUDENT WHERE class=' Grade A class ' and MATH >
- (
- SELECT MAX (MATH) from STUDENT WHERE class=' Grade B class '
- );
In addition, we can also use all, as shown in sql:
[SQL]View Plaincopyprint?
- SELECT NAME from STUDENT WHERE class=' Grade A ' and MATH > All
- (
- SELECT MATH from STUDENT WHERE class=' Grade B class '
- );
At this point, we can sum up the correspondence between Some,any,all and MIN, MAX:
[Plain]View Plaincopyprint?
- > Any (sub-qurey)---> MIN (Sub-qurey)
- < any (sub-query)---< MAX (Sub-qurey)
- > All (sub-query)---> MAX (Sub-qurey)
- < All (sub-query)---< MIN (Sub-qurey)
At this point, you should understand the role of the Some,any,all keyword. This is all about math, and you're wrong if you think you'll get a similar answer to a similar operation on your language score. All of the reasons are caused by null. Let's discuss the effect of NULL on individual keywords.
[SQL]View Plaincopyprint?
- ---statement 1
- SELECT NAME from STUDENT WHERE class=' Grade A class ' and Chinese >
- (
- SELECT MAX (Chinese) from STUDENT WHERE class=' Grade B class '
- );
- ---statement 2
- SELECT NAME from STUDENT WHERE class=' Grade A class ' and Chinese > all
- (
- SELECT Chinese from STUDENT WHERE class=' Grade B class '
- );
In general, we think that statement 1 and statement 2 will return the same result, however, the result of the above two statements is surprising, statement 1 returns Harry, Statement 2 does not return anything, why does this happen? The MAX function ignores null values by default, so statement 1 returns Harry. However, Statement 2 does not ignore NULL, and any values and null comparisons return an unknown value. Not only that, the following two statements also return different results:
[SQL]View Plaincopyprint?
- ---statement 1
- SELECT NAME from STUDENT WHERE class=' Grade A class ' and Chinese <
- (
- SELECT MIN (Chinese) from STUDENT WHERE class=' Grade B class '
- );
- ---statement 2
- SELECT NAME from STUDENT WHERE class=' Grade A class ' and Chinese < all
- (
- SELECT Chinese from STUDENT WHERE class=' Grade B class '
- );
Go Oracle Some,any,all,exists,in