Based on the SQLD exam syllabusReport an error
BETWEEN a AND b includes both a and b. If a is greater than b, not a single row comes back.LIKE, % is zero or more characters and _ is exactly one character. To search for these symbols themselves, define an escape character with ESCAPE.=. Use only IS NULL / IS NOT NULL.NOT IN (…) list contains even one NULL, the result is 0 rows.If SELECT picks columns, WHERE picks rows. Only rows where the condition is true (TRUE) stay in the result.
SELECT ENAME, SAL FROM EMP WHERE SAL >= 3000;ENAME SAL
---------- ----------
SCOTT 3000
KING 5000
FORD 3000| Operator | Meaning |
|---|---|
= | Equal |
>, >=, <, <= | Greater than, greater than or equal, less than, less than or equal |
<>, !=, ^= | Not equal (all three mean the same) |
'clerk' can't find CLERK. The result is 0 rows.SELECT COUNT(*) FROM EMP WHERE JOB = 'clerk'; COUNT(*)
----------
0Same as a <= value AND value <= b. Both endpoints are included.
SELECT ENAME, SAL FROM EMP WHERE SAL BETWEEN 1250 AND 1600;ENAME SAL
---------- ----------
ALLEN 1600
WARD 1250
MARTIN 1250
TURNER 1500
MILLER 1300BETWEEN 1600 AND 1250 is SAL >= 1600 AND SAL <= 1250, which can never be true.SELECT COUNT(*) FROM EMP WHERE SAL BETWEEN 1600 AND 1250; COUNT(*)
----------
0NOT BETWEEN is outside the range, that is, value < a OR value > b. BETWEEN can be used with dates too. HIREDATE BETWEEN DATE '1982-01-01' AND DATE '1987-12-31' picks three employees: MILLER, SCOTT, and ADAMS.
IN is the same as joining = conditions with OR. DEPTNO IN (10, 40) is DEPTNO = 10 OR DEPTNO = 40.
SELECT ENAME, DEPTNO FROM EMP WHERE DEPTNO IN (10, 40);ENAME DEPTNO
---------- ----------
CLARK 10
KING 10
MILLER 10You can also group several columns and compare them as pairs. The (job, department) pair must exactly match a pair in the list.
SELECT ENAME, JOB, DEPTNO FROM EMP WHERE (JOB, DEPTNO) IN (('MANAGER', 20), ('CLERK', 30));ENAME JOB DEPTNO
---------- --------- ----------
JONES MANAGER 20
JAMES CLERK 30| Symbol | Meaning | Example | Names it matches |
|---|---|---|---|
% | Any string of zero or more characters | 'J%' | Starts with J |
_ | Exactly one character | '_A%' | Second character is A |
| Combination | '%S' | Ends with S | |
| Combination | '____' | Exactly four characters |
SELECT ENAME FROM EMP WHERE ENAME LIKE '_A%';ENAME
----------
WARD
MARTIN
JAMESLIKE is case-sensitive too. LIKE 'k%' can't find KING.LIKE 'KING' without any wildcard is the same as = 'KING'.Even if you want to find _ literally, LIKE '%_%' means "any string of one or more characters." With ESCAPE you define an escape character, and the _ or % right after that character is read as an ordinary character.
SELECT 'OK' R FROM DUAL WHERE 'SALES_2026' LIKE '%\_%' ESCAPE '\';R
--
OKSELECT COUNT(*) FROM DUAL WHERE 'SALES2026' LIKE '%\_%' ESCAPE '\'; COUNT(*)
----------
0SALES2026, which has no underscore, is not matched. It is a value that would have matched if written as '%_%' without ESCAPE.'50%' LIKE '%#%' ESCAPE '#' looks for strings that end with %.Comparing NULL with anything gives UNKNOWN, neither true nor false. WHERE keeps only true rows, so rows that are UNKNOWN drop out.
⚠️
COMM = NULLis not an error; it gives 0 rows. The syntax is valid, so it runs, but no row ever becomes true. When looking for NULL, always useIS NULL, andIS NOT NULLfor the opposite.
SELECT COUNT(*) FROM EMP WHERE COMM = NULL; COUNT(*)
----------
0SELECT ENAME, COMM FROM EMP WHERE COMM IS NOT NULL;ENAME COMM
---------- ----------
ALLEN 300
WARD 500
MARTIN 1400
TURNER 0For the same reason, NULL rows drop out of negative conditions too. The 10 employees whose COMM is NULL are not included in COMM <> 300.
SELECT ENAME, COMM FROM EMP WHERE COMM <> 300;ENAME COMM
---------- ----------
WARD 500
MARTIN 1400
TURNER 0| Operator | True when | Precedence |
|---|---|---|
NOT | The condition is false | 1 (first) |
AND | Both conditions are true | 2 |
OR | At least one is true | 3 |
Without parentheses, AND binds before OR.
SELECT ENAME, JOB, SAL FROM EMP WHERE JOB = 'SALESMAN' OR JOB = 'MANAGER' AND SAL > 2900;ENAME JOB SAL
---------- --------- ----------
ALLEN SALESMAN 1600
WARD SALESMAN 1250
JONES MANAGER 2975
MARTIN SALESMAN 1250
TURNER SALESMAN 1500JOB = 'SALESMAN' OR (JOB = 'MANAGER' AND SAL > 2900). That's why all four low-paid SALESMEN came out.If you wanted "SALESMAN or MANAGER with salary over 2900," group the OR first with parentheses.
SELECT ENAME, JOB, SAL FROM EMP WHERE (JOB = 'SALESMAN' OR JOB = 'MANAGER') AND SAL > 2900;ENAME JOB SAL
---------- --------- ----------
JONES MANAGER 2975| When NULL is involved | Result |
|---|---|
TRUE AND UNKNOWN | UNKNOWN |
FALSE AND UNKNOWN | FALSE |
TRUE OR UNKNOWN | TRUE |
FALSE OR UNKNOWN | UNKNOWN |
NOT UNKNOWN | UNKNOWN |
| Positive | Negative |
|---|---|
= | <>, !=, ^=, NOT column = |
BETWEEN a AND b | NOT BETWEEN a AND b |
IN (…) | NOT IN (…) |
LIKE | NOT LIKE |
IS NULL | IS NOT NULL |
NOT IN (a, b) is column <> a AND column <> b. If the list contains NULL, column <> NULL becomes UNKNOWN, and since the parts are joined with AND, the whole thing can never be true.
SELECT COUNT(*) FROM EMP WHERE MGR NOT IN (7698, 7839, NULL); COUNT(*)
----------
0Conversely, IN is joined with OR, so even with NULL mixed in, the search works normally on the remaining values.
SELECT ENAME, MGR FROM EMP WHERE MGR IN (7566, NULL);ENAME MGR
---------- ----------
SCOTT 7566
FORD 7566This pitfall is even harder to spot in subqueries. Because KING's MGR is NULL, the SQL below, which looks for "employees who are nobody's manager," returns 0.
SELECT COUNT(*) FROM EMP WHERE EMPNO NOT IN (SELECT MGR FROM EMP); COUNT(*)
----------
0If you remove NULL from the subquery result, you get the expected 8. Subqueries come up again in Week 5.
SELECT COUNT(*) FROM EMP WHERE EMPNO NOT IN (SELECT MGR FROM EMP WHERE MGR IS NOT NULL); COUNT(*)
----------
8ROWNUM, which picks only the top N rows, is also used in the WHERE clause, but its behavior is distinctive, so it is covered in the Top-N queries of Week 6.Next week we move on to GROUP BY and aggregate functions, which group rows and summarize them.
= with OR.% is zero or more characters, and _ is one character.% and _ be read as ordinary characters.Click a choice to reveal the answer and explanation.
Question 1
Given the salaries below in the EMP table, which is the correct result of the following SQL? (ALLEN 1600, WARD 1250, MARTIN 1250, TURNER 1500, MILLER 1300, ADAMS 1100, the rest are above 1600 or below 1100) ```sql SELECT COUNT(*) FROM EMP WHERE SAL BETWEEN 1250 AND 1600; ```
Question 2
Which employee is not included in the result of the following SQL? ```sql SELECT ENAME FROM EMP WHERE ENAME LIKE '_A%'; ```
Question 3
Which condition has the same meaning as the following WHERE clause? ```sql SELECT ENAME FROM EMP WHERE JOB = 'SALESMAN' OR JOB = 'MANAGER' AND SAL > 2900; ```
Question 4
Which is the correct result of the following SQL? (The MGR column of EMP contains 7566, 7698, 7782, 7788, 7839, 7902, and NULL) ```sql SELECT COUNT(*) FROM EMP WHERE MGR NOT IN (7698, 7839, NULL); ```
Question 5
Which condition correctly retrieves employees whose COMM is NULL?