loading..
Русский    English
20:59

Predicates I page 2

Logical operators as arithmetic ones are being estimated in accordance with their seniority.

One-place operation NOT has highest priority. We can assure ourselves if we'll execute the two following queries.

Console
Execute
  1. -- models which are not PC;
  2. -- the second predicate is useless because it is entering condition that yet taken into account
  3. -- by the first predicate
  4. SELECT maker, model, type
  5. FROM Product
  6. WHERE NOT type='PC' OR type='Printer';

Console
Execute
  1. -- models of maker A, which are not PC
  2. SELECT maker, model, type
  3. FROM Product
  4. WHERE NOT type='PC' AND maker='A';

Parentheses can change the order of estimation of logical operators:

Console
Execute
  1. -- models that are not PC or printer, i.e. models of laptops in our case
  2. SELECT maker, model, type
  3. FROM Product
  4. WHERE NOT (type='PC' OR type='Printer');

Console
Execute
  1. -- models that are not PC produced by maker A
  2. SELECT maker, model, type
  3. FROM Product
  4. WHERE NOT (type='PC' AND maker='A');

Next priority has AND operator. Compare results of the following queries.

Console
Execute
  1. -- PC models produced by maker A and models of any type belonging to maker B
  2. SELECT maker, model, type
  3. FROM Product
  4. WHERE type='PC' AND maker='A' OR maker='B';

Console
Execute
  1. -- PC models produced by maker A or maker B
  2. SELECT maker, model, type
  3. FROM Product
  4. WHERE type='PC' AND (maker='A' OR maker='B');

Notes:

If you are afraid to forget the order in which logical operators are being estimated, use the parentheses.

Next page

Pages 1 2 3
Развернуть всё
Свернуть всё

Content:

Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
several days ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.