0% found this document useful (0 votes)
22 views3 pages

UNIT 8 Note 3 More SQL

The document provides a comprehensive guide on basic SQL operations, including table creation, data insertion, retrieval, aggregation, sorting, updating, deleting, and limiting results. It also covers conditional logic, mathematical and string functions, date and time functions, and constraints. Finally, it includes instructions for dropping a table in SQL.

Uploaded by

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

UNIT 8 Note 3 More SQL

The document provides a comprehensive guide on basic SQL operations, including table creation, data insertion, retrieval, aggregation, sorting, updating, deleting, and limiting results. It also covers conditional logic, mathematical and string functions, date and time functions, and constraints. Finally, it includes instructions for dropping a table in SQL.

Uploaded by

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

Unit 8 Database Note 03 Basic SQL AL English Medium Kithsiri jayakody

1. Table Creation
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
Department VARCHAR(50),
Salary DECIMAL(10, 2),
HireDate DATE
);

2–6. Data Insertion


INSERT INTO Employees (EmployeeID, FirstName, LastName, Age, Department,
Salary, HireDate)
VALUES (1, 'Alice', 'Smith', 30, 'HR', 4500.00, '2020-01-15');

INSERT INTO Employees VALUES (2, 'Bob', 'Johnson', 40, 'Finance', 5500.00,
'2019-06-01');
INSERT INTO Employees VALUES (3, 'Charlie', 'Williams', 35, 'IT', 6000.00,
'2018-09-12');
INSERT INTO Employees VALUES (4, 'Diana', 'Brown', 28, 'Marketing',
4000.00, '2021-03-05');
INSERT INTO Employees VALUES (5, 'Eve', 'Jones', 50, 'IT', 7000.00, '2015-
07-20');

7–12. Data Retrieval


SELECT * FROM Employees;
SELECT FirstName, LastName FROM Employees;

Filtering

SELECT * FROM Employees WHERE Department = 'IT';


SELECT * FROM Employees WHERE Age > 40;
SELECT * FROM Employees WHERE Salary BETWEEN 4000 AND 6000;
SELECT * FROM Employees WHERE LastName LIKE 'J%';

13–16. Aggregation

SELECT COUNT(*) AS TotalEmployees FROM Employees;


SELECT AVG(Salary) AS AverageSalary FROM Employees;
SELECT MIN(Age) AS YoungestEmployee, MAX(Age) AS OldestEmployee FROM
Employees;
SELECT Department, SUM(Salary) AS TotalSalaries FROM Employees GROUP BY
Department;

17–20. Sorting

SELECT * FROM Employees ORDER BY LastName ASC;


SELECT * FROM Employees ORDER BY Salary DESC;
SELECT * FROM Employees ORDER BY Department, Age ASC;
SELECT * FROM Employees ORDER BY HireDate DESC;

21–24. Updating Data

UPDATE Employees SET Salary = Salary * 1.1 WHERE Department = 'IT';


UPDATE Employees SET Age = Age + 1 WHERE EmployeeID = 1;
UPDATE Employees SET Department = 'HR' WHERE Department = 'Marketing';
UPDATE Employees SET LastName = 'Doe' WHERE LastName = 'Smith';

25–28. Deleting Data

DELETE FROM Employees WHERE Age < 30;


DELETE FROM Employees WHERE Department = 'Finance';
DELETE FROM Employees WHERE LastName LIKE 'D%';
DELETE FROM Employees WHERE HireDate < '2018-01-01';

29–32. Limiting Results


SELECT * FROM Employees LIMIT 3;
SELECT * FROM Employees ORDER BY Salary DESC LIMIT 2;
SELECT * FROM Employees WHERE Department = 'IT' LIMIT 1;
SELECT * FROM Employees ORDER BY Age ASC LIMIT 1;

33–36. Conditional Logic

SELECT EmployeeID, FirstName, Salary,


CASE
WHEN Salary < 5000 THEN 'Low'
WHEN Salary BETWEEN 5000 AND 7000 THEN 'Medium'
ELSE 'High'
END AS SalaryRange
FROM Employees;

SELECT Department, COUNT(*) AS EmployeeCount


FROM Employees
GROUP BY Department
HAVING COUNT(*) > 1;
37–40. Mathematical and String Functions
SELECT FirstName, LENGTH(LastName) AS LastNameLength FROM Employees;
SELECT EmployeeID, Salary, ROUND(Salary * 1.05, 2) AS NewSalary FROM
Employees;
SELECT CONCAT(FirstName, ' ', LastName) AS FullName FROM Employees;
SELECT UPPER(Department) AS UpperCaseDept FROM Employees;

41–45. Date and Time Functions


SELECT FirstName, DATEDIFF(CURDATE(), HireDate) AS DaysWorked FROM
Employees;
SELECT FirstName, YEAR(HireDate) AS HireYear FROM Employees;
SELECT EmployeeID, HireDate, DATE_ADD(HireDate, INTERVAL 1 YEAR) AS
Anniversary FROM Employees;
SELECT EmployeeID, MONTHNAME(HireDate) AS HireMonth FROM Employees;
SELECT * FROM Employees WHERE HireDate > DATE_SUB(CURDATE(), INTERVAL 5
YEAR);

46–49. Constraints

Add a column with a NOT NULL constraint

ALTER TABLE Employees ADD Email VARCHAR(100) NOT NULL;

Add a UNIQUE constraint

ALTER TABLE Employees ADD CONSTRAINT UNIQUE_Email UNIQUE (Email);

50. Dropping the Table


DROP TABLE Employees;

You might also like