Setting up for exercise
Refer to the details here and get the setup ready before testing the samples
Fix Invalid DB2 SQL
INSERT INTO EMP (EMP_ID, NAME, DEPARTMENT, PHONE, SALARY) VALUES (1001, 'John Doe', 'IT', '1234ABCD56', 50000);
SELECT * FROM EMP WHERE SALARY = NULL;
INSERT INTO EMP (EMP_ID, NAME, DEPARTMENT, PHONE, SALARY) VALUES (1002, NULL, 'HR', '9876543210', 45000);
INSERT INTO EMP (EMP_ID, NAME, DEPARTMENT, PHONE, SALARY) VALUES (1001, 'Jane Smith', 'Finance', '1122334455', 70000);
SELECT * FROM EMPLOYEE WHERE DEPARTMENT = 'IT';
SELECT * FROM EMP WHERE JOB_TITLE = 'Manager';
DELETE EMP WHERE EMP_ID = 1003;
UPDATE EMP SET SALARY = SALARY * 1.1 WHERE EMP_ID 1003;
- Multiple inserts
INSERT INTO MENTOR (MENTOR_ID, EMP_ID, SKILLS) VALUES (1000, 1001, 'Java');
INSERT INTO MENTOR (MENTOR_ID, EMP_ID, SKILLS) VALUES (1000, 1002, 'Java');
ORDER BY NAME;
SELECT * FROM EMP WHERE NAME LIKE John%;
INSERT INTO EMP (EMP_ID, NAME, DEPARTMENT, PHONE, SALARY) VALUES (EMPLOYEE_SEQ.NEXTVAL, 'Mike Ross', 'IT', '5557779999', 48000);
SELECT * FROM EMP WHERE COUNT(SALARY) > 1;
SELECT DEPARTMENT, NAME, AVG(SALARY) FROM EMP GROUP BY DEPARTMENT;
Create SQL Insert
- Insert an employee with EMP_ID = 1003, Name = 'Bob Carter', Department = 'IT', Phone = '1234567890', Salary = 52000.
- Insert another employee using EMP_SEQ.NEXTVAL to generate the EMP_ID.
- Insert an employee with a NULL phone number. Does it work? Why or why not?
- Insert an employee with EMP_ID = 1005 and a negative salary. Does it work?
- Insert an employee without specifying the SALARY. What happens?
- Insert a mentor linked to EMP_ID 1002 with a skill of 'Python'.
- Insert another mentor but set EMP_ID to NULL. Does it work?
- Insert an employee with a name longer than 20 characters. What happens?
- Insert an employee with an invalid department name that exceeds 10 characters.
- Insert an employee with a phone number containing letters. What happens?
Create SQL Update
- Update employee EMP_ID = 1002 to change their salary to 62000.
- Update the department of all employees inHR to ADMIN.
- Update the phone number of EMP_ID = 1003 to '1112223333'.
- Increase the salary of all employees in IT by 10%.
- Set the phone number to NULL for EMP_ID = 1004. Does it work?
- Change the department of all employees with salary < 50000 to "JUNIOR".
- Update an employee's MENTOR_ID to a value that doesn’t exist. Does it work?
- Update an employee’s salary to 29000. What happens?
- Update the NAME column to NULL for an employee. Does it work?
- Try updating a nonexistent EMP_ID. What happens?
Create SQL Delete
- Delete employee withEMP_ID = 1002. What happens if they are a mentor?
- Delete all employees with a salary less than 35000. What happens to the mentor records?
- Delete a mentor withMENTOR_ID = 1001.
- Delete all mentors where SKILLS = 'Java'.
- Try to delete an employee whose EMP_ID is referenced in MENTOR but without ON DELETE CASCADE. What happens?
- Delete all employees in theIT department.
- Delete all mentors whose EMP_ID is NULL.
- Delete an employee using a nonexistent EMP_ID. Does it generate an error?
- Delete all employees except those earning more than 60000.
- Delete all data from the EMP table but keep the table structure.
Create SQL Query
- Retrieve all employees earning more than 50,000.
- Get the names and departments of employees who are also mentors.
- Find the employees who do not have mentors.
- Count the number of employees in each department.
- Display all employees and their mentor skills(if they have a mentor).
- Get the highest salary in each department.
- Retrieve all employees sorted by salary in descending order.
- Show the total salary expense foreach department.
- Find all employees who have the same salary as someone else.
- Get the names of employees who earn more than their mentor's mentees.