Go Oracle Some,any,all,exists,in

Source: Internet
Author: User

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?
  1. --Student
  2. CREATE TABLE STUDENT
  3. (
  4. ID VARCHAR2 (8), ---school number
  5. Name VARCHAR2, ---name
  6. Class VARCHAR2, ---class
  7. Chinese number (5), ---language scores
  8. Math Number (5) ---math score
  9. );
  10. INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090001 ', ' Zhang San ', ' Grade A ', 80, 90);
  11. INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090002 ', ' John Doe ', ' Grade A ', 60, 75  );
  12. INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090003 ', ' Harry ', ' Grade A ', 90, 95);
  13. INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090004 ', ' Yangwen ', ' grade B ', 70, 90  );
  14. INSERT into STUDENT (ID, NAME, class, Chinese, MATH) VALUES (' 20090004 ', ' Li Bai ', ' Grade B ', 85, 80);
  15. 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?
    1. SELECT NAME from STUDENT WHERE class=' Grade A class ' and MATH >
    2. (
    3. SELECT MIN (MATH) from STUDENT WHERE class=' Grade B class '
    4. );


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?
    1. SELECT NAME from STUDENT WHERE class=' V Class A ' and MATH > any
    2. (
    3. SELECT MATH from STUDENT WHERE class=' Grade B class '
    4. );


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?
    1. SELECT NAME from STUDENT WHERE class=' Grade A class ' and MATH >
    2. (
    3. SELECT MAX (MATH) from STUDENT WHERE class=' Grade B class '
    4. );


In addition, we can also use all, as shown in sql:

[SQL]View Plaincopyprint?
    1. SELECT NAME from STUDENT WHERE class=' Grade A ' and MATH > All
    2. (
    3. SELECT MATH from STUDENT WHERE class=' Grade B class '
    4. );


At this point, we can sum up the correspondence between Some,any,all and MIN, MAX:

[Plain]View Plaincopyprint?
    1. > Any (sub-qurey)---> MIN (Sub-qurey)
    2. < any (sub-query)---< MAX (Sub-qurey)
    3. > All (sub-query)---> MAX (Sub-qurey)
    4. < 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?
  1. ---statement 1
  2. SELECT NAME from STUDENT WHERE class=' Grade A class ' and Chinese >
  3. (
  4. SELECT MAX (Chinese) from STUDENT WHERE class=' Grade B class '
  5. );
  6. ---statement 2
  7. SELECT NAME from STUDENT WHERE class=' Grade A class ' and Chinese > all
  8. (
  9. SELECT Chinese from STUDENT WHERE class=' Grade B class '
  10. );


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?
    1. ---statement 1
    2. SELECT NAME from STUDENT WHERE class=' Grade A class ' and Chinese <
    3. (
    4. SELECT MIN (Chinese) from STUDENT WHERE class=' Grade B class '
    5. );
    6. ---statement 2
    7. SELECT NAME from STUDENT WHERE class=' Grade A class ' and Chinese < all
    8. (
    9. SELECT Chinese from STUDENT WHERE class=' Grade B class '
    10. );

Go Oracle Some,any,all,exists,in

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.