0% found this document useful (0 votes)
23 views5 pages

Example 1

13 f doc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Example 1

13 f doc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Example 1: Count unique records based on a single column

If you want to count the number of unique values in a specific column, say EmployeeID, you can use:

SELECT COUNT(DISTINCT EmployeeID) AS UniqueEmployeeCount

FROM Employee;

Example 2: Count unique combinations of multiple columns

If you want to count the unique combinations of values in multiple columns (e.g., EmployeeID and
Salary), you can use:

SELECT COUNT(DISTINCT EmployeeID, Salary) AS UniqueEmployeeSalaryCount

FROM Employee;

Explanation:

 COUNT(DISTINCT column_name): This counts the number of distinct (unique) values in the
specified column.

 If you use multiple columns inside COUNT(DISTINCT ...), it will count unique combinations of
the values from those columns.

Example 3: Count unique rows (based on all columns in the table)

To count unique rows in the entire table, you can do this:

SELECT COUNT(*)

FROM (SELECT DISTINCT * FROM Employee) AS UniqueRecords;

SQL for count of customers with orders more than or equal to 3

Order_id CUSTOMER_id

1 101

2 102

3 101

4 103

5 102

6 102

select count(order_id), Customer_id

from Orders

Group by Customer_id

Having count(order_id)>=3
UPDATE SYNTAX

UPDATE table_name

SET column_name = new_value

WHERE condition;

Suppose you want to update the salary of employees in Department 3 who are older than 30:

UPDATE Employee

SET Salary = Salary * 1.1 -- Increase salary by 10%

WHERE DepartmentID = 3 AND Age > 30;

------------------------------------------------------------------------------------------------

set different salary increases based on performance ratings, you can use the CASE statement within
the SET clause.

For example, increase the salary by different percentages based on performance


(PerformanceRating):

UPDATE Employee

SET Salary = CASE

WHEN PerformanceRating = 'Excellent' THEN Salary * 1.2

WHEN PerformanceRating = 'Good' THEN Salary * 1.1

ELSE Salary

END;

---------------------------------------------------------------------------------------------------

set the salary of employees based on the average salary in their department:

UPDATE Employee e SET e.Salary = ( SELECT AVG(Salary) FROM Employee WHERE DepartmentID =
e.DepartmentID ) WHERE e.DepartmentID IN (1, 2);

------------------------------------------------------------------------------------------------------

UPDATE Employee

SET Salary = Salary * 1.05, -- Increase salary by 5%

Age = Age + 1 -- Increase age by 1 year

WHERE DepartmentID = 3;
Example 1: BEFORE INSERT Trigger

Let's say you want to check the age of an employee before inserting the record to make sure the age
is valid (e.g., must be greater than or equal to 18).

CREATE TRIGGER check_employee_age

BEFORE INSERT ON Employee

FOR EACH ROW

BEGIN

IF NEW.Age < 18 THEN

SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Employee age must be 18 or older';

END IF;

END;

 NEW.Age: Refers to the value that is about to be inserted into the Age column.

 SIGNAL SQLSTATE: Raises an error if the condition is not met, preventing the insertion.

This trigger will prevent the insertion if the employee's age is below 18.

Example 2: AFTER INSERT Trigger

Suppose you want to log the details of any new employee that has been inserted into the Employee
table. You can create an AFTER INSERT trigger to insert a log record into a separate Employee_Log
table.

CREATE TRIGGER log_employee_insert

AFTER INSERT ON Employee

FOR EACH ROW

BEGIN

INSERT INTO Employee_Log (EmployeeID, EmployeeName, Action, ActionDate)

VALUES (NEW.EmployeeID, NEW.EmployeeName, 'INSERT', NOW());

END;

 NEW.EmployeeID: Refers to the value that was just inserted into the EmployeeID column.

 NOW(): Gets the current timestamp.

This trigger logs the insertion of a new employee into the Employee_Log table after the record has
been successfully inserted.

Example 3: BEFORE UPDATE Trigger


Let's say you want to validate an update to ensure that an employee’s salary cannot be decreased.
You can create a BEFORE UPDATE trigger.

CREATE TRIGGER prevent_salary_decrease

BEFORE UPDATE ON Employee

FOR EACH ROW

BEGIN

IF NEW.Salary < OLD.Salary THEN

SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary cannot be decreased';

END IF;

END;

 NEW.Salary: Refers to the new salary value that is about to be updated.

 OLD.Salary: Refers to the current salary value (before the update).

This trigger prevents the salary from being decreased by raising an error if the new salary is less than
the old salary.

Example 4: AFTER DELETE Trigger

You can use an AFTER DELETE trigger to, for example, log the deletion of an employee into an
Employee_Delete_Log table after the employee record is deleted.

CREATE TRIGGER log_employee_delete

AFTER DELETE ON Employee

FOR EACH ROW

BEGIN

INSERT INTO Employee_Delete_Log (EmployeeID, EmployeeName, DeleteDate)

VALUES (OLD.EmployeeID, OLD.EmployeeName, NOW());

END;

 OLD.EmployeeID: Refers to the value of the EmployeeID column before the deletion.

 NOW(): Gets the current timestamp.

This trigger logs the deleted employee’s information after the employee record is deleted from the
Employee table.

Example 5: Compound Trigger (Before and After)


Some databases (like Oracle) allow compound triggers, which contain both BEFORE and AFTER logic
within the same trigger.

CREATE TRIGGER salary_update_trigger

BEFORE UPDATE ON Employee

FOR EACH ROW

BEGIN

-- Before update logic: Prevent salary from being decreased

IF NEW.Salary < OLD.Salary THEN

SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary cannot be decreased';

END IF;

END;

"Insert" a Column at a Specific Position:

ALTER TABLE Employee

ADD COLUMN Department VARCHAR(255) AFTER EmployeeName;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy