You are on page 1of 3

BCSE-510L

PRACTICAL-8
Aim: To Describe Various MySQL Functions.

CREATING A SAMPLE TABLE:-

-- Create a sample employees table

CREATE TABLE employees


(

employee_id INT PRIMARY KEY,

employee_name VARCHAR(255),

salary DECIMAL(10, 2),

department VARCHAR(50)

);

-- Insert sample data

INSERT INTO employees (employee_id, employee_name, salary, department)

VALUES

(1, 'John Doe', 50000.00, 'IT'),

(2, 'Jane Smith', 60000.00, 'HR'),

(3, 'Bob Johnson', 55000.00, 'Finance'),

(4, 'Alice Williams', 70000.00, 'Marketing');


BCSE-510L

1. Aggregate function: Calculate average salary in the company

SELECT AVG(salary) AS average_salary FROM employees;


gate functions

2. String function: Display employee names in uppercase

SELECT employee_name, UPPER(employee_name) AS uppercase_name FROM


employees;

3. Numeric function: Increase the salary by 10%

UPDATE employees SET salary = salary * 1.1;

4. Conditional function: Display employees in the IT department with a salary


above 60000

SELECT * FROM employees WHERE department = 'IT' AND salary > 60000;
BCSE-510L

5. Conditional function: Display employees in the IT department with a salary


above 60000

SELECT department, SUM(salary) AS total_salary_expenditure


FROM employees
GROUP BY department;

6. Subquery: Display employees with a salary higher than the average salary

SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM


employees);

7. Window function: Rank employees based on their salary within each


department

SELECT employee_name, salary, department, RANK() OVER (PARTITION BY


department ORDER BY salary DESC) AS salary_rank
FROM employees;

You might also like