You are on page 1of 8

-- Part I – Queries for SoftUni Database

USE `soft_uni`;

-- 01. Employees with Salary Above 35000


-- Create stored procedure usp_get_employees_salary_above_35000 that returns
-- all employees’ first and last names for whose salary is above 35000.
-- The result should be sorted by first_name then by last_name alphabetically, and id ascending.

DELIMITER $$
CREATE PROCEDURE usp_get_employees_salary_above_35000()
BEGIN
SELECT e.first_name, e.last_name
FROM `employees` AS e
WHERE e.salary > 35000
ORDER BY e.first_name, e.last_name, e.employee_id;
END $$
DELIMITER ;

CALL usp_get_employees_salary_above_35000();

DROP PROCEDURE IF EXISTS usp_get_employees_salary_above_35000;


-- 02. Employees with Salary Above Number
-- Create stored procedure usp_get_employees_salary_above that accept a number
-- as parameter and return all employees’ first and last names whose salary is
-- above or equal to the given number. The result should be sorted by first_name
-- then by last_name alphabetically and id ascending.

DELIMITER $$
CREATE PROCEDURE usp_get_employees_salary_above(salary_limit DOUBLE(19,4))
BEGIN
SELECT e.first_name, e.last_name
FROM `employees` AS e
WHERE e.salary >= salary_limit
ORDER BY e.first_name, e.last_name, e.employee_id;
END $$
DELIMITER ;

CALL usp_get_employees_salary_above(48100);

DROP PROCEDURE IF EXISTS usp_get_employees_salary_above;


-- 03. Town Names Starting With
-- Write a stored procedure usp_get_towns_starting_with that accept string as
-- parameter and returns all town names starting with that string. The result
-- should be sorted by town_name alphabetically.

DELIMITER $$
CREATE PROCEDURE usp_get_towns_starting_with(name_start TEXT)
BEGIN
SELECT t.name AS 'town_name'
FROM `towns` AS t
WHERE t.name LIKE concat(name_start,'%')
ORDER BY t.name;
END $$
DELIMITER ;

CALL usp_get_towns_starting_with('b');
CALL usp_get_towns_starting_with('be');
CALL usp_get_towns_starting_with('berlin');

DROP PROCEDURE IF EXISTS usp_get_towns_starting_with;


-- 04. Employees from Town
-- Write a stored procedure usp_get_employees_from_town that accepts town_name
-- as parameter and return the employees’ first and last name that live in the
-- given town. The result should be sorted by first_name then by last_name
-- alphabetically and id ascending.

DELIMITER $$
CREATE PROCEDURE usp_get_employees_from_town(town_name TEXT)
BEGIN
SELECT e.first_name, e.last_name
FROM `employees` AS e
JOIN `addresses` AS a ON e.address_id = a.address_id
JOIN `towns` AS t ON a.town_id = t.town_id
WHERE t.name = town_name
ORDER BY e.first_name, e.last_name, e.employee_id;
END $$
DELIMITER ;

CALL usp_get_employees_from_town('Sofia');

DROP PROCEDURE IF EXISTS usp_get_employees_from_town;


-- 05. Salary Level Function
-- Write a function ufn_get_salary_level that receives salary
-- of an employee and returns the level of the salary.
-- If salary is < 30000 return “Low”
-- If salary is between 30000 and 50000 (inclusive) return “Average”
-- If salary is > 50000 return “High”

CREATE FUNCTION ufn_get_salary_level(salary DOUBLE(19,4))


RETURNS VARCHAR(7)
RETURN (
CASE
WHEN salary < 30000 THEN 'Low'
WHEN salary <= 50000 THEN 'Average'
ELSE 'High'
END
);

SELECT ufn_get_salary_level(13500);
SELECT ufn_get_salary_level(43300);
SELECT ufn_get_salary_level(125500);

DROP FUNCTION IF EXISTS ufn_get_salary_level;

-- Using return parameter and IF-ELSE construction


DELIMITER $$
CREATE FUNCTION ufn_get_salary_level(salary DOUBLE(19,4))
RETURNS VARCHAR(7)
BEGIN
DECLARE level VARCHAR(7);
IF
salary < 30000 THEN SET level := 'Low';
ELSEIF
salary <= 50000 THEN SET level := 'Average';
ELSE
SET level := 'High';
END IF;
RETURN level;
END $$
DELIMITER ;
-- 06. Employees by Salary Level
-- Write a stored procedure usp_get_employees_by_salary_level that
-- receive as parameter level of salary (low, average or high) and
-- print the names of all employees that have given level of salary.
-- The result should be sorted by first_name then by last_name both in descending order.

DELIMITER $$
CREATE PROCEDURE usp_get_employees_by_salary_level(salary_level VARCHAR(7))
BEGIN
SELECT e.first_name, e.last_name
FROM `employees` AS e
WHERE e.salary < 30000 AND salary_level = 'low'
OR e.salary >= 30000 AND e.salary <= 50000 AND salary_level = 'average'
OR e.salary > 50000 AND salary_level = 'high'
ORDER BY e.first_name DESC, e.last_name DESC;
END $$
DELIMITER ;

-- Solution with call to ufn_get_salary_level


DELIMITER $$
CREATE PROCEDURE usp_get_employees_by_salary_level(salary_level VARCHAR(7))
BEGIN
SELECT e.first_name, e.last_name
FROM `employees` AS e
WHERE ufn_get_salary_level(e.salary) = salary_level
ORDER BY e.first_name DESC, e.last_name DESC;
END $$
DELIMITER ;

CALL usp_get_employees_by_salary_level('low');
CALL usp_get_employees_by_salary_level('average');
CALL usp_get_employees_by_salary_level('high');

DROP PROCEDURE IF EXISTS usp_get_employees_by_salary_level;


-- 07. Define Function
-- Define a function ufn_is_word_comprised(set_of_letters varchar(50), word varchar(50))
-- that returns true or false depending on that if the word is a comprised of the given
-- set of letters.

CREATE FUNCTION ufn_is_word_comprised(set_of_letters varchar(50), word varchar(50))


RETURNS BIT
RETURN word REGEXP (concat('^[', set_of_letters, ']+$'));

SELECT ufn_is_word_comprised('oistmiahf', 'Sofia');


SELECT ufn_is_word_comprised('oistmiahf', 'halves');
SELECT ufn_is_word_comprised('bobr', 'Rob');
SELECT ufn_is_word_comprised('pppp', 'Guy');

DROP FUNCTION IF EXISTS ufn_is_word_comprised;


-- 08. * Delete Employees and Departments
-- Write a SQL query to delete all employees from the Production and
-- Production Control departments. Also delete these departments from
-- the departments table.
-- After that exercise restore your database to revert those changes.

ALTER TABLE `departments` DROP FOREIGN KEY `fk_departments_employees`;


ALTER TABLE `departments` DROP INDEX `fk_departments_employees` ;
ALTER TABLE `employees_projects` DROP FOREIGN KEY `fk_employees_projects_employees`;
ALTER TABLE `employees` DROP FOREIGN KEY `fk_employees_employees`;

DELETE FROM `employees`


WHERE
`department_id` IN (SELECT
d.department_id
FROM
`departments` AS d
WHERE
d.name IN ('Production' , 'Production Control'));

DELETE FROM `departments`


WHERE
`name` IN ('Production' , 'Production Control');

You might also like