The Sub-Languages of SQL
The Sub-Languages of SQL
1. DDL (Data Definition)
Defines & alters table structures.
CREATE, ALTER, DROP, TRUNCATE2. DML (Data Manipulation)
Inserts, modifies & queries records.
INSERT, UPDATE, DELETE, SELECT3. DCL (Data Control)
Manages user security & permissions.
GRANT, REVOKE4. TCL (Transaction Control)
Manages database transactions.
COMMIT, ROLLBACK, SAVEPOINTExplanation
- Definition statements set up objects such as tables and manipulation statements change rows.
- Other SQL commands control access and transaction boundaries.
The Three Core Database Change Operations
The Three Core Database Change Operations
1. INSERT
Adds new rows into an existing database table.
2. UPDATE
Modifies existing values in one or more matching rows.
3. DELETE
Removes specific rows that satisfy a WHERE condition.
Explanation
- INSERT adds rows and UPDATE changes values in existing rows.
- DELETE removes rows that meet the specified condition.
Visualizing SQL Joins (Venn Diagram Perspective)
Visualizing SQL Joins (Venn Diagram Perspective)
INNER JOIN
Returns ONLY rows that have matching values in both tables.
LEFT OUTER JOIN
Returns ALL rows from Left table + matching rows from Right table.
RIGHT OUTER JOIN
Returns ALL rows from Right table + matching rows from Left table.
FULL OUTER JOIN
Returns ALL rows from BOTH tables; unmatched fields become NULL.
Explanation
- Matching keys produce joined rows and repeated keys can produce several matches.
- An outer join also keeps specified unmatched rows with NULLs for missing values.
The Five Built-In Aggregate Functions in SQL
The Five Built-In Aggregate Functions in SQL
1. COUNT()
Returns the total number of rows or non-null values.
2. SUM()
Calculates the total mathematical sum of a numeric column.
3. AVG()
Calculates the arithmetic mean value of a numeric column.
4. MIN()
Returns the lowest (minimum) value in a column.
5. MAX()
Returns the highest (maximum) value in a column.
Explanation
- COUNT counts qualifying items, while SUM and AVG combine numeric values.
- MIN and MAX select the smallest and largest qualifying values.
The Subquery Hierarchy
The Subquery Hierarchy
1. Single-Row Subquery
Returns exactly ONE value (1 row, 1 column).
Uses operators:
Uses operators:
=, >, <, >=, <=, !=2. Multi-Row Subquery
Returns a list of multiple values (multiple rows, 1 column).
Uses operators:
Uses operators:
IN, ANY, ALL3. Correlated Subquery
Inner query references a column from the outer query!
Executes repeatedly once for every outer row.
Executes repeatedly once for every outer row.
Explanation
- An independent subquery can supply a set or value for the outer query.
- A correlated subquery refers to a value from the current outer row.
The Four Major Benefits of Views
The Four Major Benefits of Views
1. Security & Access Control
Hides sensitive columns (like salaries, passwords) from unauthorized users.
2. Query Simplification
Hides complex 5-table joins behind a simple
SELECT * FROM MyView.3. Logical Data Independence
If base tables are restructured, the view definition can be modified without breaking client apps.
4. Customized Perspectives
Different departments see only the data relevant to their day-to-day operations.
Explanation
- A view gives a stored name to a query and can simplify repeated joins.
- Exposing selected columns through a view can also support restricted access.
Cursor Life-Cycle Steps
Cursor Life-Cycle Steps
1. DECLARE
Defines the SQL query for the cursor
Defines the SQL query for the cursor
➔ 2. OPEN
Executes query & allocates memory buffer
Executes query & allocates memory buffer
➔ 3. FETCH
Reads row into variables (Loop)
Reads row into variables (Loop)
➔ 4. CLOSE
Releases memory resources
Releases memory resources
Explanation
- Declare and open a cursor before fetching rows.
- Close it when no further rows are needed so its resources can be released.