Here’s a fully merged and comprehensive note in Markdown format, based on the content
of your course material, enriched with additional SQL knowledge, covering everything
systematically.
# Oracle SQL Notes
## 1. Introduction to Databases
### **Key Definitions**
- **Data**: Raw facts about objects and events.
- **Information**: Processed data useful for decision-making.
- **Database**: An organized collection of interrelated data.
- **DBMS**: Software that manages databases (e.g., Oracle, MySQL).
- **ERD (Entity Relationship Diagram)**: A visual representation of
entities and their relationships.
### **Database Normalization**
- **1NF**: Each cell contains a single value; rows are unique.
- **2NF**: Meets 1NF and has no partial dependency.
- **3NF**: Meets 2NF and has no transitive dependency.
---
## 2. SQL Basics
### **Categories of SQL**
| **Category** | **Commands** |
**Purpose**
|
|---------------|------------------------------------------|----------
----------------------------------------------------------------------
-------------------------------------------------------------------|
| **DDL** | `CREATE`, `ALTER`, `DROP`, `TRUNCATE` | Define
database schema and objects like tables, views, sequences.
|
| **DML** | `INSERT`, `UPDATE`, `DELETE`, `MERGE` |
Manipulate data within tables.
|
| **DQL** | `SELECT` | Retrieve
data from the database.
|
| **TCL** | `COMMIT`, `ROLLBACK`, `SAVEPOINT` | Manage
transaction control.
|
| **DCL** | `GRANT`, `REVOKE` | Manage
access control.
|
---
## 3. Data Types
| **Type** | **Description** | **Examples**
|
|---------------|---------------------------------------|-------------
-----------------|
| `CHAR(n)` | Fixed-length character string. | `CHAR(10)`
|
| `VARCHAR2(n)` | Variable-length character string. |
`VARCHAR2(50)` |
| `NUMBER(p,s)` | Numeric data with precision/scale. |
`NUMBER(10,2)` |
| `DATE` | Stores date and time. | `YYYY-MM-DD`
|
---
## 4. Constraints
| **Constraint** | **Purpose**
| **Example** |
|-------------------|-------------------------------------------------
-----|----------------------------------------|
| `PRIMARY KEY` | Uniquely identifies rows.
| `id INT PRIMARY KEY` |
| `FOREIGN KEY` | Enforces referential integrity.
| `FOREIGN KEY (dept_id) REFERENCES departments(dept_id)` |
| `CHECK` | Limits column values.
| `CHECK (age >= 18)` |
| `NOT NULL` | Prevents NULL values.
| `name VARCHAR2(50) NOT NULL` |
---
## 5. DDL (Data Definition Language)
### **Commands**
- **`CREATE TABLE`**: Define a new table.
```sql
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR2(50),
age INT
);
• ALTER TABLE: Modify a table.
ALTER TABLE students ADD COLUMN grade CHAR(1);
• DROP TABLE: Delete a table.
DROP TABLE students;
• TRUNCATE TABLE: Removes all rows from a table.
TRUNCATE TABLE students;
6. DML (Data Manipulation Language)
Commands
• INSERT: Add data to a table.
INSERT INTO students (id, name, age) VALUES (1, 'Alice', 20);
• UPDATE: Modify existing data.
UPDATE students SET age = 21 WHERE id = 1;
• DELETE: Remove rows from a table.
DELETE FROM students WHERE id = 1;
7. Queries (DQL)
Simple SELECT
• Retrieve all columns:
SELECT * FROM students;
• Retrieve specific columns:
SELECT name, age FROM students WHERE age > 18;
Sorting with ORDER BY
• Ascending order:
SELECT name, age FROM students ORDER BY age ASC;
• Descending order:
SELECT name, age FROM students ORDER BY age DESC;
8. Functions
Single-Row Functions
• Numeric: ROUND, CEIL, FLOOR.
• Character: UPPER, LOWER, LENGTH.
• Date: SYSDATE, ADD_MONTHS.
• Conversion: TO_CHAR, TO_DATE.
Aggregate Functions
• COUNT: Count rows.
SELECT COUNT(*) FROM students;
• SUM: Sum values.
SELECT SUM(age) FROM students;
9. Joins
Types of Joins
Type Description Example
INNER JOIN Rows matching both SELECT * FROM A
tables. JOIN B ON A.id =
B.id;
LEFT JOIN All rows from left table, SELECT * FROM A
matching rows from LEFT JOIN B ON
right table. A.id = B.id;
RIGHT JOIN All rows from right SELECT * FROM A
table, matching rows RIGHT JOIN B ON
from left table. A.id = B.id;
FULL OUTER JOIN All matching and non- SELECT * FROM A
matching rows from FULL OUTER JOIN B
both tables. ON A.id = B.id;
SELF JOIN Joins a table with itself. SELECT a.col,
b.col FROM A a, A
b WHERE a.id >
b.id;
10. Set Operations
Operation Description Syntax
UNION Combines results, SELECT col FROM A
removing duplicates. UNION SELECT col
FROM B;
UNION ALL Combines results, SELECT col FROM A
includes duplicates. UNION ALL SELECT
col FROM B;
INTERSECT Finds common rows SELECT col FROM A
between two queries. INTERSECT SELECT
col FROM B;
MINUS Rows in the first query SELECT col FROM A
but not in the second MINUS SELECT col
query. FROM B;
11. Conditional Expressions
Expression Purpose Example
CASE Conditional logic. SELECT CASE WHEN
age > 18 THEN
'Adult' ELSE
'Minor' END;
NVL Handle NULL values. SELECT NVL(grade,
'N/A') FROM
students;
NULLIF Compare values. SELECT
NULLIF(col1, col2)
FROM table;
12. Tasks and Practice
DDL Practice
• Create tables for departments and employees.
• Use constraints like NOT NULL, CHECK, PRIMARY KEY.
DML Practice
• Insert rows into a table.
• Update records to test CHECK constraints.
• Delete records based on a condition.
Query Practice
• Retrieve employees with salary > $50,000.
• Aggregate queries for average salaries by department.
• Use joins to find employees and their managers.
This Markdown file comprehensively combines all topics, commands, and examples
covered in your course material. If you’d like, I can refine this or export it to a different
format such as PDF or HTML.