SQLAssignments
SQLAssignments
SQL ASSIGNMENT
GOPIKRISHNA UPPUTURI
4/21/2025
SQL ASSIGNME NT
Database , Tables , Constraints
1.Create new database & employee table (based on give sample data)
create employee table with primary key (EmployeeID)
USE company;
Department VARCHAR(50),
DateOfHire DATE,
VALUES
A). SELECT
UPPER(FirstName) AS first_name_upper,
LOWER(FirstName) AS first_name_lower,
CONCAT(FirstName, ' ', LastName) AS full_name,
CONCAT('Hello ', FirstName) AS greeting
FROM employee;
-- Whose first_name starts with any single character between 'm' and 'v'
SELECT * FROM employee WHERE FirstName REGEXP '^[m-v]';
-- Whose first_name does not start with any single character between 'm'
and'v'
SQL ASSIGNME NT
10. Write a query to get all unique values of"department" from the employee
table.
A).SELECT DISTINCT Department FROM employee;
12. Write down the query to print first letter of a Name in Upper Case and
all other letter in Lower Case.(EmployDetail table)
A).SELECT CONCAT(UPPER(SUBSTRING(FirstName, 1, 1)),
13. Write down the query to display all employee name in one cell
separated by ','
14. Query to get the below values of "salary" from employee table
Lowest salary
Highest salary
Average salary
Highest salary - Lowest salary as diff_salary
% of difference betweenHighest salary and lowest salary. (sample output
format: 10.5%)
A).SELECT
MIN(Salary) AS LowestSalary,
MAX(Salary) AS HighestSalary,
AVG(Salary) AS AverageSalary,
SQL ASSIGNME NT
MAX(Salary) - MIN(Salary) AS diff_salary,
(MAX(Salary) - MIN(Salary)) / MIN(Salary) * 100 AS percent_difference FROM employee;
1.Display first_name and gender as M/F.(if male then M, if Female then F).
SQL ASSIGNME NT
A) SELECT FirstName,CASE
WHEN Gender = 'Male' THEN 'M'
WHEN Gender = 'Female' THEN 'F'
ELSE 'Other'
END AS Gender
FROM employee;
2. Display first_name, salary and a salary category. (If salary is below 50,000,
categorize as
'Low'; between 50,000 a 60,000 as 'Medium'; above 60,000 as 'High').
A).SELECT FirstName,Salary,
CASE
4.Display first_name, salary, and eligibility for a salary raise. (If salary is
less than 50,000,
mark as 'Eligible for Raise'; otherwise, 'Not Eligible')
SELECT FirstName,Salary,
SQL ASSIGNME NT
CASE
WHEN Salary < 50000 THEN 'Eligible for Raise'
ELSE 'Not Eligible'
END AS RaiseEligibility FROM employee;
A) SELECT FirstName,Salary,
CASE
WHEN Salary > 60000 THEN Salary * 0.10
WHEN Salary BETWEEN 50000 AND 60000 THEN Salary * 0.07
ELSE Salary * 0.05
END AS BonusAmount
FROM employee;
A) SELECT FirstName,Salary,
CASE
WHEN Salary > 60000 THEN 'Senior'
WHEN Salary BETWEEN 50000 AND 60000 THEN 'Mid-Level'
ELSE 'Junior'
END AS SeniorityLevel
FROM employee;
FROM employee;
10. (If salary is greater than 60,000, classify as 'Senior'; between 50,000 and
60,000 as
'Mid-Level'; below 50,000 as 'Junior')
A) SELECT FirstName,Salary,
CASE
WHEN Salary > 60000 THEN 'Senior'
WHEN Salary BETWEEN 50000 AND 60000 THEN 'Mid-Level'
ELSE 'Junior'
END AS SeniorityLevel
FROM employee;
11. Display first_name, department, and job level for IT employees. (If
department is 'IT'
And salary is greater than 55,000, mark as 'Senior IT Employee';
otherwise,'Other').
SQL ASSIGNME NT
A)SELECT FirstName, Department,
CASE
WHEN Department = 'IT' AND Salary > 55000 THEN 'Senior IT Employee'
ELSE 'Other'
END AS JobLevel
FROM employee
WHERE Department = 'IT';
15. Display first_name, salary, and overtime pay eligibility. (If salary is
below 50,000,
mark as 'Eligible for Overtime Pay'; otherwise, 'Not Eligible')
16. Display first_name, department, salary, and job title. (If department
is 'HR' and salary is
above 60,000, mark as 'HR Executive'; if department is 'Finance' and
salary is above
55,000, mark as 'Finance Manager'; otherwise, 'Regular Employee')
Group by
1.Write the query to get the department and department wise
total(sum) salary, display it
in ascending and descending order according to salary.
A) -- Ascending Order
SELECT Department, SUM(Salary) AS TotalSalary
FROM employee
GROUP BY Department
ORDER BY TotalSalary ASC;
-- Descending Order
SELECT Department, SUM(Salary) AS TotalSalary
FROM employee
GROUP BY Department
ORDER BY TotalSalary DESC;
2.Write down the query to fetch Project name assign to more than one
Employee
3.Write the query to get the department, total no. of departments, total(sum)
salary with
SUM(Salary) AS TotalSalary
FROM employee
GROUP BY Department;
A) -- Average Salary
-- Maximum Salary
8. Display the number of employees who joined each year and categorize
hiring trends. (Ifa year had more than 5 hires, mark as 'High Hiring'; 3 to 5 as
'Moderate Hiring'; otherwise,'Low Hiring')
A) SELECT
YEAR(DateOfHire) AS HireYear,
COUNT(*) AS EmployeeCount,
CASE
WHEN COUNT(*) > 5 THEN 'High Hiring'
WHEN COUNT(*) BETWEEN 3 AND 5 THEN 'Moderate Hiring'
ELSE 'Low Hiring'
END AS HiringTrend
FROM employee GROUP BY YEAR(DateOfHire) ORDER BY HireYear;
9. Display department-wise highest salary and classify senior roles. (If the
highest salary in a department is above 70,000, label as 'Senior Leadership';
otherwise, 'Mid-Level')
CURDATE() AS CurrentDate
FROM employee;
A) SELECT first_name,
joining_date,
TIMESTAMPDIFF(MONTH, joining_date, CURDATE()) AS
MonthsSinceJoining,
SQL ASSIGNME NT
DATEDIFF(CURDATE(), joining_date) AS DaysSinceJoining
FROM employee;
3. Get all employee details from the employee table whose joining year is
2020.
A) SELECT *
FROM employee
WHERE YEAR(joining_date) = 2020;
4.Get all employee details from the employee table whose joining month is
Feb.
A) SELECT *
FROM employee
WHERE MONTH(joining_date) = 2;
5.Get all employee details from employee table whose joining date between
"2021-01-01"
and "2021-12-01"
A) SELECT *
FROM employee
WHERE joining_date BETWEEN '2021-01-01' AND '2021-12-01';
3.Get the employee name and project name from the "employee
table" and
"ProjectDetail" for all employees. If an employee has no assigned
project, display "-No Project Assigned," sorted by first name.
A) SELECT e.first_name,
COALESCE(p.ProjectName, '-No Project Assigned') AS ProjectName
FROM employee e
LEFT JOIN ProjectDetail p ON e.EmployeeID = p.EmployeeID
ORDER BY e.first_name;
4.Get all project names from the "ProjectDetail" table, even if they are
not linked to any employee, sorted by first name from the "employee
table"
and "ProjectDetail" table.
5.Find the project names from the "ProjectDetail" table that have not
been assigned to any employee using the "employee table" and
"ProjectDetail" table.
A) SELECT ProjectName
FROM ProjectDetail
WHERE EmployeeID NOT IN (SELECT EmployeeID FROM employee);\
SQL ASSIGNME NT
6.Get the employee name and project name for employees who are
assigned to more than one project.
GROUP BY EmployeeID
Ranking Functions
1.Get all project names from the "ProjectDetail" table, even if they are
not linked to any employee, sorted by first name from the "employee
table" and "ProjectDetail" table.
SQL ASSIGNME NT
A) SELECT e.first_name, p.ProjectName
FROM ProjectDetail p
LEFT JOIN employee e ON p.EmployeeID = e.EmployeeID
ORDER BY e.first_name;
2.Find the project names from the "ProjectDetail" table that have not
been assigned to
any employee using the "employee table" and "ProjectDetail" table.
A) SELECT p.ProjectName
FROM ProjectDetail p
LEFT JOIN employee e ON p.EmployeeID = e.EmployeeID
WHERE e.EmployeeID IS NULL;
3.Get the employee name and project name for employees who are
assigned to more than one project.
Partitioning Data
A).SELECT
first_name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary
DESC) AS row_num
FROM employee;
A).SELECT
first_name, department, salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS
salary_rank
FROM employee;
A).SELECT
first_name, department, salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS
dense_rank
FROM employee;
A).SELECT *
FROM (
SELECT
SQL ASSIGNME NT
first_name, department, salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS
rank_within_dept
FROM employee
) ranked
WHERE rank_within_dept = 1;
A).SELECT *
FROM (
SELECT
first_name, department, salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS
rank_within_dept
FROM employee) ranked WHERE rank_within_dept = 2;
A).SELECT
first_name, department, joining_date,
TIMESTAMPDIFF(YEAR, joining_date, CURDATE()) AS years_experience,
7. Find the employee with the earliest join date in each department using
RANK().
A).SELECT *
FROM (
SELECT
first_name, department, joining_date,
RANK() OVER (PARTITION BY department ORDER BY joining_date ASC) AS
join_rank
FROM employee
) ranked
WHERE join_rank = 1;
SQL ASSIGNME NT
Complex Ranking Scenarios
1. Find the employees who earn more than the average salary of their
department.
A).SELECT *
FROM employee e
WHERE salary > (
SELECT AVG(salary)
FROM employee
WHERE department = e.department
);
2. Rank employees within each job title in every department based on salary.
A).SELECT
first_name, department, job_title, salary,
RANK() OVER (PARTITION BY department, job_title ORDER BY salary DESC) AS
salary_rank
FROM employee;
A).SELECT *cFROM (
SELECT *,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS
dept_salary_rank
FROM employee
) rankedvvWHERE dept_salary_rank <= 3;
4.Find employees who belong to the top 10% earners within their
department using PERCENT_RANK().
A).SELECT * FROM (
SELECT *,
SQL ASSIGNME NT
PERCENT_RANK() OVER (PARTITION BY department ORDER BY
salary DESC) AS pct_rank
FROM employee
) ranked WHERE pct_rank <= 0.10;
A).SELECT
first_name, joining_date,
ROW_NUMBER() OVER (PARTITION BY YEAR(joining_date) ORDER BY
joining_date ASC) AS row_num_by_year
FROM employee;
A).SELECT
e.first_name, e.department, COUNT(p.project_id) AS project_count,
RANK() OVER (PARTITION BY e.department ORDER BY COUNT(p.project_id)
DESC) AS project_rank FROM employee e
JOIN ProjectDetail p ON e.employee_id = p.employee_id
GROUP BY e.employee_id, e.first_name, e.department;
9.Find employees who have been working in the company the longest in
each department.
A).SELECT * FROM (
SELECT *,
RANK() OVER (PARTITION BY department ORDER BY joining_date ASC) AS
seniority_rank
FROM employee
) ranked
WHERE seniority_rank = 1;
Thank You