Introduction
Operator precedence in WHERE helps decide which parts of a condition are checked first, so the database returns the right results.
SELECT column1, column2 FROM table_name WHERE condition1 AND condition2 OR condition3;
SELECT * FROM employees WHERE department = 'Sales' OR department = 'Marketing' AND salary > 50000;
SELECT * FROM employees WHERE (department = 'Sales' OR department = 'Marketing') AND salary > 50000;
SELECT * FROM products WHERE price > 100 AND stock > 0 OR discontinued = FALSE;
CREATE TABLE employees ( id INT, name VARCHAR(50), department VARCHAR(50), salary INT ); INSERT INTO employees VALUES (1, 'Alice', 'Sales', 60000), (2, 'Bob', 'Marketing', 45000), (3, 'Charlie', 'Marketing', 55000), (4, 'Diana', 'Sales', 40000); SELECT name, department, salary FROM employees WHERE department = 'Sales' OR department = 'Marketing' AND salary > 50000;
WHERE clause without parentheses: AND or OR?AND has higher precedence than OR. This means AND conditions are evaluated before OR conditions when no parentheses are used.AND conditions first, then applies OR. This affects which rows match the query.AND is evaluated before OR -> Option BWHERE clauses is syntactically correct in SQL?AND and OR correctly between conditions. Options A, B, and D have misplaced or missing operators.Employees with columns age, salary, and city, what rows will this query return?SELECT * FROM Employees WHERE age > 30 OR salary < 50000 AND city = 'NY';
AND has higher precedence than OR, the condition is evaluated as: age > 30 OR (salary < 50000 AND city = 'NY').SELECT * FROM Employees WHERE age > 25 OR salary < 40000 AND;
AND; which means the AND operator is not followed by a condition. This is a syntax error.WHERE clause correctly uses operator precedence to get this result?Options:
A) WHERE age > 40 AND salary < 60000 OR city = 'LA'
B) WHERE (age > 40 OR salary < 60000) AND city = 'LA'
C) WHERE age > 40 OR (salary < 60000 AND city = 'LA')
D) WHERE (age > 40 OR city = 'LA') AND salary < 60000
Pass the test and this pattern moves unknown