Bautista, Kingsley M.
BSIT 2A-WMAD
Laboratory Module VIII
Writing Basic SQL Queries using Functions and Aggregations
Lab Title: Exploring SQL Basics: SELECT, WHERE, ORDER BY, GROUP BY, and
Aggregate Functions
ACTIVITIES AND EXERCISES:
Part 1: Basic SELECT Queries
1. Display all records from the employees table.
2. Display only the names and departments of all employees.
Part 2: Filtering with WHERE Clause
3. Display employees who work in the 'IT' department.
4. Display employees with a salary greater than 60000.
5. Display employees hired after January 1, 2019.
Part 3: Sorting Results
6. Display all employees sorted by salary in descending order.
7. Display all employees sorted by hire date in ascending order.
Part 4: Using Aggregate Functions
8. Count the total number of employees.
9. Get the average salary of all employees.
10. Get the minimum and maximum salary.
Part 5: Grouping Data
11. Display the total number of employees in each department.
12. Get the average salary for each department.
13. List departments with more than 1 employee (use HAVING).
Assessment/ Lab Questions:
1. 1. Write a query to find the total number of employees in the 'HR' and 'IT' departments
combined.
SELECT COUNT (*) AS total employees FROM employee WHERE department IN
(‘HR’,’IT’);
2. Write a query to display names of employees earning more than the average salary.
SELECT name FROM employees WHERE salary > 68000;
3. Write a query to list the departments sorted by their average salary in descending order.
SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY
department ORDER BY avg_salary DESC;
4. Write a query to count how many employees were hired before the year 2020.
SELECT COUNT(*) AS total_employee_before_2020 FROM employees WHERE
hire_date < ‘2020-01-01’;
5. Explain the difference between WHERE and HAVING clauses.
The WHERE clause is used to filter individual rows before any grouping is done, while
the HAVING clause is used to filter groups of rows after they have been grouped using
GROUP BY.