Based on the SQLD exam syllabusReport an error
= or <> comparison is neither true nor false but UNKNOWN, so the row is filtered out.IS NULL / IS NOT NULL.COUNT(*) counts the rows themselves. That is why AVG(COMM) and AVG(NVL(COMM, 0)) give different values.At the modeling stage, you decide for each attribute "whether the value may be empty." This is the nullability setting.
COMM (commission) in EMP exists only for sales jobs, so it is NULL for other jobs.' ' is a character value.| Value | Meaning | Example |
|---|---|---|
0 | The commission was set to 0 | TURNER |
NULL | The commission was not set | BLAKE, JAMES |
'' as NULL. SQL Server distinguishes '' from NULL. This comes up again in the DBMS differences in Subject 2.Whatever you add to or multiply with NULL, the result is NULL. We checked with department 30.
SELECT ENAME, SAL, COMM, SAL + COMM AS TOTAL, SAL + NVL(COMM, 0) AS TOTAL2
FROM EMP WHERE DEPTNO = 30 ORDER BY EMPNO;ENAME SAL COMM TOTAL TOTAL2
---------- ---------- ---------- ---------- ----------
ALLEN 1600 300 1900 1900
WARD 1250 500 1750 1750
MARTIN 1250 1400 2650 2650
BLAKE 2850 2850
TURNER 1500 0 1500 1500
JAMES 950 950COMM is NULL, so SAL + COMM also became NULL.COMM is 0, so 1500 came out normally. The difference between 0 and NULL shows up directly in the result.NVL(COMM, 0). NULL-related functions are covered in Week 3.You cannot say NULL is equal to or different from any value. The comparison result is UNKNOWN, and since the WHERE clause keeps only the rows that are true, UNKNOWN rows drop out.
SELECT COUNT(*) FROM EMP WHERE COMM = NULL;
SELECT COUNT(*) FROM EMP WHERE COMM IS NULL; COUNT(*)
----------
0
COUNT(*)
----------
10COMM = NULL runs without an error, but the result is 0 rows, because the comparison is UNKNOWN for every row.IS NULL.<> works the same way. If you look for "employees whose commission is not 0," the employees with NULL drop out.
SELECT ENAME, COMM FROM EMP WHERE COMM <> 0 ORDER BY EMPNO;ENAME COMM
---------- ----------
ALLEN 300
WARD 500
MARTIN 1400⚠️ Trap:
WHERE column = NULLis not a syntax error. It runs and returns 0 rows. Both the choice "an error occurs" and the choice "rows with NULL are returned" are wrong.
Aggregate functions (COUNT, SUM, AVG, MAX, MIN) exclude NULL from the calculation.
SELECT COUNT(*) AS CNT, COUNT(COMM) AS COMM_CNT, SUM(COMM) AS COMM_SUM,
AVG(COMM) AS COMM_AVG, AVG(NVL(COMM, 0)) AS COMM_AVG2
FROM EMP; CNT COMM_CNT COMM_SUM COMM_AVG COMM_AVG2
---------- ---------- ---------- ---------- ----------
14 4 2200 550 157.142857| Expression | Result | Calculation |
|---|---|---|
COUNT(*) | 14 | Total number of rows. Unrelated to NULL |
COUNT(COMM) | 4 | Only non-NULL values. TURNER with 0 is included |
SUM(COMM) | 2200 | 300 + 500 + 1400 + 0 |
AVG(COMM) | 550 | 2200 ÷ 4 |
AVG(NVL(COMM, 0)) | 157.142857 | 2200 ÷ 14 |
AVG is the number of non-NULL rows. If the goal is "the average commission over all employees," you must convert with NVL first and then take the average.SUM and AVG is NULL. It is not 0.Grouping by department reveals the last rule.
SELECT DEPTNO, COUNT(*), COUNT(COMM), SUM(COMM), AVG(COMM)
FROM EMP GROUP BY DEPTNO ORDER BY DEPTNO; DEPTNO COUNT(*) COUNT(COMM) SUM(COMM) AVG(COMM)
---------- ---------- ----------- ---------- ----------
10 3 0
20 5 0
30 6 4 2200 550COMM is entirely NULL. COUNT(COMM) is 0, but SUM and AVG are blank as NULL.SELECT ENAME, COMM FROM EMP WHERE DEPTNO = 30 ORDER BY COMM, ENAME;ENAME COMM
---------- ----------
TURNER 0
ALLEN 300
WARD 500
MARTIN 1400
BLAKE
JAMESLet us look closely at the "substitution" classification from Day 1.
| Category | Meaning | Example |
|---|---|---|
| Natural identifier | An identifier created by the business. Depending on the textbook, also called the original identifier | (order number, product number) of an order line item |
| Surrogate identifier | An identifier that does not exist in the business but is created artificially because the natural identifier is complex | OrderItemID (a serial number) of an order line item |
We created a table that uses a serial number as the PK with no constraint at all on (ORDER_NO, PROD_NO), and inserted the same row twice.
CREATE TABLE T_ITEM_SURR (ITEM_ID NUMBER(6) PRIMARY KEY, ORDER_NO NUMBER(4), PROD_NO VARCHAR2(4), QTY NUMBER(3));
INSERT INTO T_ITEM_SURR VALUES (1, 1001, 'P1', 2);
INSERT INTO T_ITEM_SURR VALUES (2, 1001, 'P1', 2);
SELECT * FROM T_ITEM_SURR ORDER BY ITEM_ID; ITEM_ID ORDER_NO PROD QTY
---------- ---------- ---- ----------
1 1001 P1 2
2 1001 P1 2ITEM_ID is different, the DBMS does not stop it.(ORDER_NO, PROD_NO) as its PK, the second INSERT is rejected.CREATE TABLE T_ITEM_NAT (ORDER_NO NUMBER(4), PROD_NO VARCHAR2(4), QTY NUMBER(3),
CONSTRAINT T_ITEM_NAT_PK PRIMARY KEY (ORDER_NO, PROD_NO));
INSERT INTO T_ITEM_NAT VALUES (1001, 'P1', 2);
INSERT INTO T_ITEM_NAT VALUES (1001, 'P1', 2);ERROR at line 1:
ORA-00001: unique constraint (SCOTT.T_ITEM_NAT_PK) violated on table SCOTT.T_ITEM_NAT columns (ORDER_NO, PROD_NO)After the exercise, we dropped both tables with DROP TABLE … PURGE.
(ORDER_NO, PROD_NO) in addition to the surrogate identifier PK.ITEM_ID PK index can end up being a rarely used index.| Comparison | Natural identifier | Surrogate identifier |
|---|---|---|
| Key composition | A combination of business attributes, easily composite | A single column (serial number, etc.) |
| Duplicate prevention | The PK blocks it directly | Duplicates are allowed unless there is a separate constraint on the natural columns |
| Indexes | One PK index | PK + UNIQUE on natural columns, so there can be two |
| Inheritance to children | Several columns go down | Only one column goes down |
Starting next week, we move on to Subject 2, SQL, and cover the SELECT statement and functions.
= or <>. In a WHERE clause it is filtered out like false.IS NOT NULL.Click a choice to reveal the answer and explanation.
Question 1
The COMM values in the EMP table are 300, 500, 1400, 0, and the remaining 10 employees have NULL. What is the result of SELECT AVG(COMM) FROM EMP;?
Question 2
Which statement about the result of running SELECT COUNT(*) FROM EMP WHERE COMM = NULL; is correct? (There are 10 rows where COMM is NULL.)
Question 3
Which of the following statements about NULL is LEAST appropriate?
Question 4
Instead of the natural identifier (order number, product number), you decided to use a serial-number surrogate identifier as the primary identifier of the order line item. Which of the following best describes a problem that can arise?
Question 5
In Oracle, when you run SELECT ENAME, COMM FROM EMP ORDER BY COMM;, where do the rows with NULL in COMM appear?