Lecture Week 4-Databases
Lecture Week 4-Databases
Databases-Week 4
Definition: Used to perform mathematical
calculations.
Operators:
+ (Addition)
- (Subtraction)
Arithmetic * (Multiplication)
Operators / (Division)
% (Modulo)
Example:
SELECT 10 + 5 AS Addition, 10 - 5 AS
Subtraction;
Definition: Used to assign a value to a variable.
Operator:
Assignmen Example:
Operators:
= (Equal to)
Compariso != or <> (Not equal to)
n
Operators > (Greater than)
Example:
Operators Example:
SET @x += 5;
Definition: Used to combine multiple conditions
in queries.
Operators:
Example:
Set
);
-- Insert sample data into Employees
INSERT INTO Employees (EmployeeID,
Operators
• Definition: Used to combine Name)
result sets. VALUES
(1, 'Alice'),
• Operators:
(2, 'Bob'),
• UNION (Without duplicates) (3, 'Charlie');
-- Create the Managers table
• UNION ALL (With duplicates) CREATE TABLE Managers (
• INTERSECT (Common rows) ManagerID INT PRIMARY KEY,
Name VARCHAR(100)
• EXCEPT (Rows not in second );
set). -- Insert sample data into Managers
INSERT INTO Managers (ManagerID, Name)
• Example
VALUES
A={1,2,3}, B={2,3,4} (1, 'David'),
(2, 'Eve'),
A UNION B = {1,2,3,4} (3, 'Alice'); -- Note: Alice appears in both
tables
A UNION ALL B={1,2,3,2,3,4}
-- Run the UNION query
A INTESECT B = {2,3} SELECT Name FROM Employees
UNION
A EXCEPT B ={1} SELECT Name FROM Managers;
Definition: Used to join strings.
Operator:
String
Concatenati + (String concatenation)
on Operator
Example:
Inner Query: Computes the average salary for each department (DepartmentID).
The WHERE DepartmentID = e.DepartmentID ensures that the average salary is calculated
for only the employees within the same department as the current employee e.
Nested Queries
• List departments with more than 2 employees.
SELECT FirstName,
REPLACE(FirstName, 'a', 'X') AS ReplacedString,
SUBSTRING(FirstName, 1, 3) AS SubStringExample,
UPPER(FirstName) AS UpperCaseName,
LOWER(LastName) AS LowerCaseName,
LEFT(LastName, 1) AS FirstChaName
FROM Employees;
CASE Statement
• The CASE statement allows conditional logic in SQL queries.
• It is useful for categorizing data into different groups.
SELECT
FirstName,
LastName,
Salary,
CASE
WHEN Salary > 75000 THEN 'High Salary'
WHEN Salary BETWEEN 60000 AND 75000 THEN 'Medium Salary'
ELSE 'Low Salary'
END AS SalaryCategory
FROM Employees;
• To read a table from an MS SQL
Server into a pandas DataFrame
DELIMITER //
CREATE PROCEDURE GetTotalSales(IN emp_id INT)
BEGIN
SELECT SUM(amount) AS TotalSales
FROM Orders
WHERE employee_id = emp_id;
END // --Test the Stored Procedure
DELIMITER ;
CALL GetTotalSales(101);
SQL Triggers
• A trigger is an automatic action executed before or after an event on a table.
• Example: Automatically logs any new order in an E-commerce Database
DELIMITER //
CREATE TRIGGER after_order_insert
AFTER INSERT ON Orders
FOR EACH ROW
BEGIN
INSERT INTO AuditLog (action, order_id, timestamp)
VALUES ('INSERT', NEW.order_id, NOW());
END //
INSERT INTO Orders (customer_id, employee_id,
DELIMITER ; amount, order_date) VALUES (1, 104, 300.00,
'2025-03-25');
SQL Events
• An event is a scheduled task in SQL that runs at a specific time or interval.
• Example: Automatically deletes orders older than one year in an E-commerce
Database.
DELIMITER //
CREATE EVENT DeleteOldOrders
ON SCHEDULE EVERY 1 DAY
DO
BEGIN
DELETE FROM Orders WHERE order_date < NOW() - INTERVAL 1 YEAR;
--NOW() - INTERVAL 1 YEAR: Computes the date exactly one year before today.
END // -- Enable the event scheduler
DELIMITER ; SET GLOBAL event_scheduler = ON;
PARTITION BY
The PARTITION BY clause is used in conjunction with window functions to divide the result set
into partitions (or groups). Common window functions are:
•ROW_NUMBER(): Assigns a unique sequential integer to rows within a partition.
•RANK(): Assigns a rank to rows within a partition, with gaps in rank values for ties.
•DENSE_RANK(): Assigns ranks without gaps for tied rows.
•SUM(), AVG(), COUNT(): Aggregate functions applied to each partition