1.
Create an Employee table with the following attributes:
EmployeeID – Primary Key
FirstName – Not Null Constraint
LastName – Not Null Constraint
Gender – Check Constraint Values (‘M’, ‘F’)
BirthDate – Not Null Constraint
HireDate – Not Null Constraint
Salary – Check Constraint (Salary >0)
Email – Unique and Not null constraint
PhoneNumber – Not NUll
Department – Check Constraint ('HR', 'IT', 'Finance', 'Sales',
'Marketing')
ManagerID – Self referencing Foreign Key
IsFullTime – Boolean Data type (Default value is True)
CREATE TABLE Employee
(EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Gender CHAR(1) CHECK (Gender IN ('M', 'F')),
BirthDate DATE NOT NULL,
HireDate DATE NOT NULL,
Salary DECIMAL(10, 2) CHECK (Salary > 0),
Email VARCHAR(100) UNIQUE NOT NULL,
PhoneNumber Numeric(15) Not NUll,
Department VARCHAR(50) CHECK (Department IN ('HR', 'IT', 'Finance',
'Sales', 'Marketing')),
ManagerID INT,
IsFullTime BOOLEAN DEFAULT TRUE,
FOREIGN KEY (ManagerID) REFERENCES Employee(EmployeeID));
2. Insert 15 rows into the Employee table
INSERT INTO Employee (EmployeeID, FirstName, LastName, Gender,
BirthDate, HireDate, Salary, Email, PhoneNumber, Department, ManagerID,
IsFullTime) VALUES
(1, 'Alice', 'Smith', 'F', '1990-01-15', '2020-02-20', 60000.00,
'
[email protected]', '1234567890', 'HR', NULL, TRUE),
(2, 'Bob', 'Johnson', 'M', '1985-05-25', '2019-03-10', 70000.00,
'
[email protected]', '1234567891', 'IT', 1, TRUE),
(3, 'Carol', 'Williams', 'F', '1992-07-30', '2021-08-15', 55000.00,
'
[email protected]', '1234567892', 'Finance', 1, TRUE),
(4, 'David', 'Brown', 'M', '1988-12-05', '2018-01-25', 80000.00,
'
[email protected]', '1234567893', 'Sales', 2, TRUE),
(5, 'Eve', 'Jones', 'F', '1995-03-14', '2022-06-30', 45000.00,
'
[email protected]', '1234567894', 'Marketing', 3, TRUE),
(6, 'Frank', 'Garcia', 'M', '1987-04-18', '2020-11-01', 62000.00,
'
[email protected]', '1234567895', 'IT', 2, TRUE),
(7, 'Grace', 'Martinez', 'F', '1993-10-09', '2021-05-17', 48000.00,
'
[email protected]', '1234567896', 'Finance', 3, TRUE),
(8, 'Henry', 'Hernandez', 'M', '1980-09-21', '2017-04-12', 75000.00,
'
[email protected]', '1234567897', 'Sales', 4, TRUE),
(9, 'Ivy', 'Lopez', 'F', '1994-02-11', '2022-09-05', 52000.00,
'
[email protected]', '1234567898', 'Marketing', 5, TRUE),
(10, 'Jack', 'Wilson', 'M', '1986-06-16', '2019-12-22', 72000.00,
'
[email protected]', '1234567899', 'HR', 1, TRUE),
(11, 'Karen', 'Anderson', 'F', '1991-08-30', '2021-03-08', 61000.00,
'
[email protected]', '1234567800', 'IT', 2, TRUE),
(12, 'Larry', 'Thomas', 'M', '1984-11-03', '2018-07-19', 82000.00,
'
[email protected]', '1234567801', 'Finance', 3, TRUE),
(13, 'Megan', 'Taylor', 'F', '1990-03-29', '2020-10-29', 53000.00,
'
[email protected]', '1234567802', 'Sales', 4, TRUE),
(14, 'Nina', 'Moore', 'F', '1996-05-12', '2022-02-17', 49000.00,
'
[email protected]', '1234567803', 'Marketing', 5, TRUE),
(15, 'Oscar', 'Jackson', 'M', '1989-07-07', '2018-11-11', 74000.00,
'
[email protected]', '1234567804', 'HR', 10, TRUE);
3. Count the number of Employees in each department.
SELECT Department, COUNT(*) AS EmployeeCount FROM Employee GROUP
BY Department HAVING COUNT(*) > 1;
4. Display the Average salary based on gender if average
salary is greater than 50000.
SELECT Gender, AVG(Salary) AS AverageSalary FROM Employee GROUP BY
Gender HAVING AVG(Salary) > 50000;
5. Display the total salary expenditure for each department.
SELECT Department, SUM(Salary) AS TotalSalary FROM Employee GROUP BY Department HAVING
SUM(Salary) > 100000;
6. Display the department where maximum salary is less than
80000.
SELECT Department, MAX(Salary) AS MaxSalary FROM Employee GROUP
BY Department HAVING MAX(Salary) < 80000;
7. Display the department details where the number of
employee is greater than 2 and all employees are full time
employee.
SELECT Department, COUNT(*) AS FullTimeCount FROM Employee
WHERE IsFullTime = TRUE GROUP BY Department HAVING COUNT(*) > 2;
8. Find Employees Who Have a Phone Number That Starts with
'123'
SELECT EmployeeID, FirstName, LastName, PhoneNumber FROM Employee
WHERE PhoneNumber LIKE '123%';
9. Add a Bonus to Each Employee's Salary (10% increase).
SELECT EmployeeID, FirstName, LastName, Salary, Salary * 1.10 AS
SalaryWithBonus FROM Employee;
10. Convert Employee First Name to Uppercase
SELECT EmployeeID, UPPER(FirstName) AS FirstName_Upper, LastName
FROM Employee;
11. Get the Length of Each Employee's Full Name (FirstName +
LastName)
SELECT EmployeeID, FirstName, LastName, LENGTH(FirstName) +
LENGTH(LastName) AS FullNameLength FROM Employee;
12. Round the Salaries to the Nearest Thousand
SELECT EmployeeID, FirstName, LastName, Salary, ROUND(Salary, -3) AS
RoundedSalary FROM Employee;