Class Time Notes Summary
Class Time Notes Summary
A nested query (also called a subquery) in SQL is a query inside another query, often used to
filter data based on results from another table. The JOIN operation is used to combine rows
from two or more tables based on a related column between them.
Example:
Employees table:
Departments table:
DepartmentID DepartmentName
10 HR
20 IT
Let’s say we want to find all employees who work in the HR department. We first use a
subquery to find the DepartmentID of the HR department.
SELECT EmployeeName
FROM Employees
WHERE DepartmentID = (
SELECT DepartmentID
FROM Departments
WHERE DepartmentName = 'HR'
);
Here, the subquery finds the DepartmentID for the HR department, which is then used by the
outer query to get the names of the employees in that department.
To achieve the same result using a JOIN, we can combine the Employees and Departments
tables based on the DepartmentID.
SELECT Employees.EmployeeName
FROM Employees
JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID
WHERE Departments.DepartmentName = 'HR';
In this query, we use the JOIN operation to combine the rows from both tables where the
DepartmentID matches, and we filter for the department name 'HR'.
We can also combine both techniques. For example, let’s say we want to find employees who
work in a department where at least one employee has an EmployeeID greater than 2:
SELECT EmployeeName
FROM Employees
WHERE DepartmentID IN (
SELECT DepartmentID
FROM Employees
WHERE EmployeeID > 2
);
Simple query optimization focuses on improving query performance using basic techniques
that can significantly reduce the query's execution time and resource usage. Below are some
key techniques for simple query optimization:
1. Use Indexes
Indexes help the database find rows faster by creating a lookup table for specified columns.
They are particularly useful in WHERE clauses, joins, and sorting operations.
Example: Without an index, a query might require a full table scan, which can be slow. Adding
an index can make a huge difference.
Unoptimized query:
This allows the query to quickly find the relevant rows based on the City value.
2. Avoid SELECT *
Fetching all columns from a table (SELECT *) is often inefficient because it retrieves
unnecessary data. Instead, select only the columns you need.
Unoptimized query:
SELECT * FROM Customers WHERE City = 'New York';
This reduces the amount of data retrieved and processed, improving performance.
If you're only interested in a subset of the results, use LIMIT to restrict the number of rows
returned. This is particularly useful when displaying results in a paginated fashion.
Unoptimized query:
Optimization:
SELECT CustomerID, CustomerName FROM Customers WHERE City = 'New York' LIMIT
10;
This limits the result to 10 rows, reducing the amount of data retrieved and processed.
Ensure that your WHERE clause is as specific as possible to reduce the number of rows the
database has to scan.
Unoptimized query:
Optimization: If you have another condition that narrows down the result set, include it:
SELECT CustomerID, CustomerName FROM Customers WHERE City = 'New York' AND
Age > 30;
Adding more specific filters helps the database limit the number of rows it processes.
Calculations or functions in the WHERE clause can slow down a query because they need to
be computed for every row.
Unoptimized query:
When joining tables, make sure to join on indexed columns and avoid unnecessary joins.
Unoptimized query:
Optimization: Use explicit JOIN syntax and ensure that CustomerID is indexed in both tables:
PL/SQL:
PL/SQL programs are organized into blocks. Every block has the following sections:
Syntax:
DECLARE
-- Declarations of variables, cursors, etc.
BEGIN
-- Executable statements (SQL, control structures, etc.)
EXCEPTION
-- Error handling code
END;
DECLARE
v_name VARCHAR2(50); -- Variable declaration
v_age NUMBER; -- Numeric variable declaration
BEGIN
v_name := 'John Doe'; -- Assigning value
v_age := 30;
END;
4. Control Structures
PL/SQL supports procedural programming constructs like loops and conditional logic.
IF-THEN-ELSE Statement:
1. Basic Loop:
LOOP
-- Code to execute
EXIT WHEN condition;
END LOOP;
2. FOR Loop:
3. WHILE Loop:
5. PL/SQL Cursors
Example: DECLARE
v_customer_name Customers.CustomerName%TYPE;
BEGIN
FROM Customers
EXCEPTION
Example: DECLARE
CURSOR cur_customers IS
FROM Customers
v_customer_id Customers.CustomerID%TYPE;
v_customer_name Customers.CustomerName%TYPE;
BEGIN
OPEN cur_customers;
LOOP
END LOOP;
CLOSE cur_customers;
END;
6. Stored Procedures and Functions
Stored Procedures:
A stored procedure is a subprogram that performs a specific task and doesn’t return a value.
7. Packages
Example:
8. Triggers
A trigger is a special kind of stored procedure that automatically runs when a certain event
occurs in the database, such as insert, update, or delete operations.
Example:
9. Exception Handling
PL/SQL provides mechanisms to handle runtime errors using the EXCEPTION block.
Syntax:
BEGIN
-- Code that may cause an exception
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unknown error occurred');
END;