OCP 1Z0 051 141

141. View the Exhibit and examine the structure of CUSTOMERS and GRADES tables. 
You need to display names and grades of customers who have the highest credit limit. 
Which two SQL statements would accomplish the task? (Choose two.)  
OCP 1Z0 051 141_第1张图片
A. SELECT custname, grade 
FROM customers, grades 
WHERE (SELECT MAX(cust_credit_limit)        
FROM customers) BETWEEN startval and endval; 
B. SELECT custname, grade 
FROM customers, grades 
WHERE (SELECT MAX(cust_credit_limit)        
FROM customers) BETWEEN startval and endval       
AND cust_credit_limit BETWEEN startval AND endval; 
C. SELECT custname, grade 
FROM customers, grades 
WHERE cust_credit_limit = (SELECT MAX(cust_credit_limit)                            
FROM customers) 
AND cust_credit_limit BETWEEN startval AND endval; 
D. SELECT custname, grade 
FROM customers , grades 
WHERE cust_credit_limit IN (SELECT MAX(cust_credit_limit)                             
FROM customers) 
AND MAX(cust_credit_limit) BETWEEN startval AND endval; 

CREATE TABLE grades AS
SELECT to_char(s.grade) AS grade, s.losal AS startval, s.hisal AS endval
  FROM scott.salgrade s;

CREATE TABLE customers AS
SELECT e.empno  AS cutsno,
       e.ename  AS custname,
       to_char(e.deptno) AS custaddress,
       e.sal    AS cust_credit_limit
  FROM scott.emp e;


A 只有限制条件,缺少关联条件,没对customers过滤
SQL> SELECT custname, cust_credit_limit, grade
  2    FROM customers, grades
  3   WHERE (SELECT MAX(cust_credit_limit) FROM customers) BETWEEN startval AND
  4         endval;
CUSTNAME   CUST_CREDIT_LIMIT GRADE
---------- ----------------- ----------------------------------------
SMITH                 800.00 5
ALLEN                1600.00 5
WARD                 1250.00 5
JONES                2975.00 5
MARTIN               1250.00 5
BLAKE                2850.00 5
CLARK                2450.00 5
SCOTT                3000.00 5
KING                 5000.00 5
TURNER               1500.00 5
ADAMS                1100.00 5
JAMES                 950.00 5
FORD                 3000.00 5
MILLER               1300.00 5
14 rows selected


B 与 C 均有关联条件,限制条件分别限制customers与 grades ,产生的效果一样。
B

SQL> SELECT custname, grade
  2    FROM customers, grades
  3   WHERE (SELECT MAX(cust_credit_limit) FROM customers) BETWEEN startval AND
  4         endval
  5     AND cust_credit_limit BETWEEN startval AND endval;
CUSTNAME   GRADE
---------- ----------------------------------------
KING       5
1 row selected

C
SQL> SELECT custname, grade
  2    FROM customers, grades
  3   WHERE cust_credit_limit = (SELECT MAX(cust_credit_limit) FROM customers)
  4     AND cust_credit_limit BETWEEN startval AND endval;
CUSTNAME   GRADE
---------- ----------------------------------------
KING       5
1 row selected


D 在where后面使用聚合函数,会报错
SQL> SELECT custname, grade
  2    FROM customers, grades
  3   WHERE cust_credit_limit IN (SELECT MAX(cust_credit_limit) FROM customers)
  4     AND MAX(cust_credit_limit) BETWEEN startval AND endval;
SELECT custname, grade
  FROM customers, grades
 WHERE cust_credit_limit IN (SELECT MAX(cust_credit_limit) FROM customers)
   AND MAX(cust_credit_limit) BETWEEN startval AND endval
ORA-00934: group function is not allowed here


Answer: BC 

你可能感兴趣的:(OCP 1Z0 051 141)