0% found this document useful (0 votes)
16 views6 pages

DBMS Lab6 190

Uploaded by

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

DBMS Lab6 190

Uploaded by

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

VANSH SHARMA

DBMS LAB-6
2200290110190

EXP:-1
How to use aggregate functions in sql?
-- Create the Employees table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary DECIMAL(10, 2),
HireDate DATE
);

-- Insert sample data into the Employees table


INSERT INTO Employees (EmployeeID, Name, Department, Salary, HireDate)
VALUES
(1, 'Alice Smith', 'HR', 60000, '2020-01-15'),
(2, 'Bob Brown', 'IT', 80000, '2019-03-22'),
(3, 'Charlie Davis', 'IT', 75000, '2021-07-10'),
(4, 'Diana King', 'Marketing', 55000, '2018-09-30'),
(5, 'Eva White', 'HR', 62000, '2022-02-12');

-- Query to get the count of employees in each department


SELECT Department, COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY Department;

-- Query to get the average salary of employees


SELECT AVG(Salary) AS AverageSalary
FROM Employees;

-- Query to get the total salary paid to employees


SELECT SUM(Salary) AS TotalSalary
FROM Employees;

-- Query to get the maximum salary in the company


SELECT MAX(Salary) AS HighestSalary
FROM Employees;

-- Query to get the minimum hire date


SELECT MIN(HireDate) AS EarliestHireDate
FROM Employees;
2. How to use group by clause?
-- Step 1: Create the Employee Table
CREATE TABLE employee (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2),
hire_date DATE
);

-- Step 2: Insert Sample Data


INSERT INTO employee (employee_id, first_name, last_name, department,
salary, hire_date) VALUES
(1, 'John', 'Doe', 'Sales', 60000, '2020-01-15'),
(2, 'Jane', 'Smith', 'Sales', 65000, '2019-03-10'),
(3, 'Sam', 'Brown', 'IT', 80000, '2021-06-23'),
(4, 'Lucy', 'Green', 'HR', 70000, '2022-02-01'),
(5, 'Mike', 'White', 'IT', 90000, '2021-04-18'),
(6, 'Kate', 'Johnson', 'HR', 72000, '2020-11-11');

-- Step 3: Use GROUP BY Clause to Calculate Average Salary by Department


SELECT department, AVG(salary) AS average_salary
FROM employee
GROUP BY department;

-- Additional Example: Count Number of Employees in Each Department


SELECT department, COUNT(*) AS employee_count
FROM employee
GROUP BY department;

3. How to use having clause?


-- Step 1: Create the Employee Table
CREATE TABLE employee (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2),
hire_date DATE
);

-- Step 2: Insert Sample Data


INSERT INTO employee (employee_id, first_name, last_name, department,
salary, hire_date) VALUES
(1, 'John', 'Doe', 'Sales', 60000, '2020-01-15'),
(2, 'Jane', 'Smith', 'Sales', 65000, '2019-03-10'),
(3, 'Sam', 'Brown', 'IT', 80000, '2021-06-23'),
(4, 'Lucy', 'Green', 'HR', 70000, '2022-02-01'),
(5, 'Mike', 'White', 'IT', 90000, '2021-04-18'),
(6, 'Kate', 'Johnson', 'HR', 72000, '2020-11-11');

-- Step 3: Use HAVING Clause to Filter Groups


-- Example: Find departments with an average salary greater than 70000
SELECT department, AVG(salary) AS average_salary
FROM employee
GROUP BY department
HAVING AVG(salary) > 70000;

-- Additional Example: Count number of employees in each department and


filter where count > 1
SELECT department, COUNT(*) AS employee_count
FROM employee
GROUP BY department
HAVING COUNT(*) > 1;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy