CS-232 DBMS Lab #2 Spring 2025
Lab 2 –Clauses and Basic Operations on PostgreSQL Data
This session focuses on essential PostgreSQL query operations, including the WHERE clause for filtering
data, performing arithmetic operations within queries, utilizing logical and comparison operators, handling
NULL values, defining column aliases, and applying the DISTINCT clause and IS NULL operator.
Additionally, it covers executing UPDATE queries to modify records and differentiating between DELETE,
DROP, and TRUNCATE commands for efficient data and schema management.
Create a sample table named employees with the following structure and data:
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary NUMERIC(10, 2),
manager_id INT,
hire_date DATE
);
INSERT INTO employees (first_name, last_name, department, salary, manager_id, hire_date)
VALUES
('John', 'Doe', 'Sales', 50000, NULL, '2020-01-15'),
('Jane', 'Smith', 'HR', 60000, 1, '2019-03-22'),
('Alice', 'Johnson', 'Sales', 55000, 1, '2021-07-10'),
('Bob', 'Brown', 'IT', 70000, NULL, '2018-11-05'),
('Charlie', 'Davis', 'IT', 65000, 4, '2022-02-18'),
('Eva', 'White', 'HR', 62000, 1, '2020-09-30');
Task 1: Basic SELECT with WHERE Clause
1. Retrieve all employees from the Sales department.
2. Find employees with a salary greater than 60000.
3. List employees hired after 2020-01-01.
Task 2: Arithmetic Operations in WHERE Clause
1. Calculate the annual bonus (10% of salary) for each employee and display their first_name, last_name,
and bonus.
2. Find employees whose total earnings (salary + 10% bonus) exceed 70000.
Task 3: Logical and Comparison Operators
1. Retrieve employees from the IT department with a salary between 60000 and 70000.
2. Find employees who are either in the HR department or have a salary greater than 65000.
3. List employees who do not have a manager (manager_id is NULL).
Task 4: Null Values, Column Aliases, and DISTINCT Clause
1. Display the unique departments in the employees table.
2. Rename the salary column as Monthly Salary in the output.
3. Find employees whose manager_id is NULL.
Task 5: UPDATE Query
1. Increase the salary of all HR department employees by 10%.
2. Update the manager_id of Charlie Davis to 1.
Task 6: DELETE, DROP, and TRUNCATE
1. Delete all employees who were hired before 2020-01-01.
2. Create a copy of the employees table named employees_backup.
3. Truncate the employees_backup table.
4. Drop the employees_backup table.