Based on the SQLD exam syllabusReport an error
SELECT is usually classified as DML, though it is sometimes called DQL separately.AS can be omitted in an alias, and to preserve spaces, case, or special characters you wrap the alias in double quotes.DISTINCT removes duplicates across the whole combination of the SELECT list. NULL also counts as a single value and is kept just once.|| skips NULL as if it were an empty string.The model you drew with entities, attributes, and relationships in Subject 1 becomes tables, columns, and keys in a real DB.
| Modeling term | DB term | Other names | Example (EMP table) |
|---|---|---|---|
| Entity | Table | Relation | All employees |
| Instance | Row | Tuple, record | One employee (SMITH) |
| Attribute | Column | Field | Name (ENAME), salary (SAL) |
| Primary identifier | Primary key (PK) | Primary Key | Employee number (EMPNO) |
| Relationship | Foreign key (FK) | Foreign Key | Department number (DEPTNO) → DEPT |
ORDER BY (Week 4).All examples in this note use the three tables below.
| Table | Contents | Rows | Main columns |
|---|---|---|---|
| EMP | Employees | 14 | EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO |
| DEPT | Departments | 4 | DEPTNO, DNAME, LOC |
| SALGRADE | Salary grades | 5 | GRADE, LOSAL, HISAL |
| Category | Meaning | Commands |
|---|---|---|
| DML (Data Manipulation Language) | Query and change data | SELECT, INSERT, UPDATE, DELETE, MERGE |
| DDL (Data Definition Language) | Define structures (tables, etc.) | CREATE, ALTER, DROP, RENAME, TRUNCATE |
| DCL (Data Control Language) | Grant and revoke privileges | GRANT, REVOKE |
| TCL (Transaction Control Language) | Confirm or cancel transactions | COMMIT, ROLLBACK, SAVEPOINT |
SELECT and call it DQL (Data Query Language). If DQL is not among the choices, treat it as DML.TRUNCATE deletes data but is DDL. It is easy to confuse with DELETE. The difference is covered in Week 7.SELECT only. The other statements are left for Week 7.SELECT [DISTINCT] column, expression, ... ← what to show
FROM table ← where to get it fromSELECT and FROM are required. Conditions (WHERE) and sorting (ORDER BY) come after them.select ename from emp is the same. String values inside single quotes are case-sensitive.; at the end of a statement is a marker telling the SQL tool "this is where one statement ends."* fetches all columns in the order defined in the table.
SELECT * FROM DEPT; DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTONYou can also list just the columns you need, separated by commas. A calculation can go in a column position too.
SELECT ENAME, SAL, SAL * 12 AS ANNUAL FROM EMP WHERE DEPTNO = 10;ENAME SAL ANNUAL
---------- ---------- ----------
CLARK 2450 29400
KING 5000 60000
MILLER 1300 15600WHERE DEPTNO = 10 is a condition that picks only department 10. It was added to keep the example short; details come in Day 5.If you don't name a calculation, the expression itself becomes the column heading. An alias changes the heading.
| How to write it | Example | Heading shown |
|---|---|---|
AS + name | SAL * 12 AS ANNUAL | ANNUAL |
AS omitted | SAL * 12 ANNUAL | ANNUAL |
| Double quotes | SAL * 12 "Annual Pay" | Annual Pay (exactly as written) |
| Lowercase without quotes | ENAME name | NAME (converted to uppercase) |
SELECT ENAME AS "Employee Name", SAL "Monthly", SAL * 12 Yearly FROM EMP WHERE DEPTNO = 10;Employee Name Monthly YEARLY
------------- ---------- ----------
CLARK 2450 29400
KING 5000 60000
MILLER 1300 15600Using an alias that contains a space without quotes causes an error. Employee is read as the alias, and FROM is expected next, but Name shows up instead.
SELECT ENAME Employee Name FROM EMP WHERE DEPTNO = 10;SELECT ENAME Employee Name FROM EMP WHERE DEPTNO = 10
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expectedSAL 'MONTHLY', is an error. Remember: single quotes = values, double quotes = names.⚠️ An alias created in SELECT cannot be used in WHERE.
WHEREis processed beforeSELECT, so the alias does not exist yet at that point. The execution order is covered in Week 4, Day 2.
SELECT EMPNO, ENAME, SAL AS PAY FROM EMP WHERE PAY > 3000;SELECT EMPNO, ENAME, SAL AS PAY FROM EMP WHERE PAY > 3000
*
ERROR at line 1:
ORA-00904: "PAY": invalid identifierDISTINCT is written once, right after SELECT. Looking at the 14 employees' jobs without duplicates gives five.
SELECT DISTINCT JOB FROM EMP ORDER BY JOB;JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMANORDER BY was added to fix the result order. Without it, the order of DISTINCT results is not defined.With two or more columns, rows are merged into one only when the entire combination is the same. DISTINCT does not apply to just the first column.
SELECT DISTINCT DEPTNO, JOB FROM EMP ORDER BY DEPTNO, JOB; DEPTNO JOB
---------- ---------
10 CLERK
10 MANAGER
10 PRESIDENT
20 ANALYST
20 CLERK
20 MANAGER
30 CLERK
30 MANAGER
30 SALESMANSELECT DEPTNO, DISTINCT JOB that puts it in the middle.NULL means "no value," but DISTINCT treats NULLs as the same and keeps just one row.
SELECT DISTINCT COMM FROM EMP ORDER BY COMM DESC; COMM
----------
1400
500
300
0| Operator | Meaning | Precedence |
|---|---|---|
*, / | Multiply, divide | First |
+, - | Add, subtract | Later |
( ) | Inside parentheses first | Highest |
SELECT ENAME, SAL + 100 * 2, (SAL + 100) * 2 FROM EMP WHERE EMPNO = 7369;ENAME SAL+100*2 (SAL+100)*2
---------- ---------- -----------
SMITH 1000 1800SAL + 100 * 2 is 800 + 200, and (SAL + 100) * 2 is 900 × 2.10 / 4 is 2.5, not 2.Even for a calculation unrelated to any table, Oracle traditionally requires a FROM. That is what DUAL is for.
DUMMY and a single row with the value 'X'. It is used to make sure exactly one row comes back.FROM, as in SELECT 1 + 2;. In versions before 23ai that is an error, so on the exam, assume the form with FROM DUAL.SELECT 1 + 2 without FROM.|| joins values into a single string. Numbers and dates are converted to text and joined.
SELECT ENAME || ' has the job ' || JOB || '.' AS INTRO FROM EMP WHERE DEPTNO = 10;INTRO
---------------------------------------
CLARK has the job MANAGER.
KING has the job PRESIDENT.
MILLER has the job CLERK.+ instead of ||. The CONCAT function works in both DBs (Day 2).NULL is neither 0 nor blank; it is an "unknown value." Adding anything to an unknown value gives an unknown result.
SELECT ENAME, SAL, COMM, SAL + COMM, SAL * 12 + COMM AS ANNUAL FROM EMP WHERE DEPTNO = 30;ENAME SAL COMM SAL+COMM ANNUAL
---------- ---------- ---------- ---------- ----------
ALLEN 1600 300 1900 19500
WARD 1250 500 1750 15500
MARTIN 1250 1400 2650 16400
BLAKE 2850
TURNER 1500 0 1500 18000
JAMES 950SAL + COMM and the annual figure become NULL (blank). Having a SAL value doesn't help.NVL(COMM, 0) (Day 4).⚠️ Oracle's
||is an exception. Unlike arithmetic, it skips NULL as if it were an empty string.'A' || NULL || 'B'is'AB', not NULL. Standard SQL and SQL Server's+concatenation return NULL.
SELECT ENAME || NULL || JOB FROM EMP WHERE EMPNO = 7839;ENAME||NULL||JOB
-------------------
KINGPRESIDENTTomorrow covers character functions that cut and join strings, and numeric functions that round and truncate.
AS can be omitted.Click a choice to reveal the answer and explanation.
Question 1
Which of the following is a TCL (Transaction Control Language) command?
Question 2
In the EMP table, BLAKE's SAL is 2850 and COMM is NULL. Which is the correct result of the following SQL? ```sql SELECT SAL * 12 + COMM FROM EMP WHERE ENAME = 'BLAKE'; ```
Question 3
Which of the following SQL statements causes an error when run in Oracle?
Question 4
How many rows does the following SQL return for the EMP table (14 employees)? (Jobs by department: dept 10 CLERK·MANAGER·PRESIDENT, dept 20 ANALYST·CLERK·MANAGER, dept 30 CLERK·MANAGER·SALESMAN) ```sql SELECT DISTINCT DEPTNO, JOB FROM EMP; ```
Question 5
In Oracle, which is the correct result of the following SQL? ```sql SELECT 'A' || NULL || 'B' FROM DUAL; ```