0% found this document useful (0 votes)
32 views

Activity3 Subquery Vacal

The document demonstrates how to create databases and tables in HR, insert sample data, and perform various subqueries including insert, update, delete, exist, and not exist subqueries.

Uploaded by

Diego Daniel
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)
32 views

Activity3 Subquery Vacal

The document demonstrates how to create databases and tables in HR, insert sample data, and perform various subqueries including insert, update, delete, exist, and not exist subqueries.

Uploaded by

Diego Daniel
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/ 5

1.

CREATE DATABASE HR;


2. CREATE TABLE HR.Department (dep_id INT PRIMARY KEY,
dep_name varchar(20),
dep_location varchar(15));

CREATE TABLE HR.Salary_grade (grade INT PRIMARY KEY,


min_salary INT,
max_salary INT);

CREATE TABLE HR.employees(emp_id INT PRIMARY KEY ,


emp_name VARCHAR(15),
job_name VARCHAR (10),
manager_id INT,
hire_date DATE,
salary DECIMAL(10,2),
commission DECIMAL (7,2),
dep_id int,
FOREIGN KEY (dep_id) REFERENCES department (dep_id));
3. INSERT INTO department VALUES
(1,"Financial Dept ", "Ruby Bldg"),
(2,"Administrative Dept", "Topaz Bldg"),
(3,"Academic Dept", "Jade Bldg");

INSERT INTO salary_grade VALUES


(1, "15000","30000"),
(2, "20000","40000"),
(3,"25000","50000");

INSERT INTO employees VALUES


(101,"Angelo Chan","Accountant",10010,"2018-03-01","50000.00","25000.00",1),
(201,"Erza Scarlet","Dean",20010,"2019-02-15","40000.00","20000.00",2),
(301,"Kaneki ken","Professor",30010,"2020-01-06","30000.00","15000.00",3)
4. SUBQUERY

INSERT SUB QUERY

INSERT INTO employees_bkp


SELECT * from employees
WHERE emp_id IN (SELECT emp_id from employees);

In this Insert Subquery, I built a back-up table with no insert values, and the data from the
employees table will be duplicated to the back-up table that I made using the Insert Subquery.

INSERT INTO employees_bkp


SELECT * from employees
WHERE job_name ="Dean"

In this Insert Subquery, I inserted only one record the column job_name with connected to the
column with Job name Dean.

UPDATE SUBQUERY

UPDATE department_bkp
SET dep_location ="Topaz Bldg"
WHERE dep_location IN (SELECT dep_location from department)

In this Update Subquery, the column dep_location we set the data to “Topaz Bldg” so now every
department has the same location.
UPDATE employees_bkp
set salary = salary + 500
WHERE emp_id IN(SELECT emp_id FROM employees WHERE emp_id >=2)

As you can see, all the data in salary column is greater than equal to two so it means all the
data in salary column will be updated and added 500 each.

DELETE SUBQUERY

DELETE FROM salary_grade_bkp


WHERE grade IN (SELECT grade from salary_grade WHERE grade >=3)

In this DELETE SUBQUERY, You will see that 1 row is affected and it is deleted from the table

DELETE FROM salary_grade_bkp WHERE grade IN (SELECT grade from salary_grade WHERE
grade >=1 or grade>=2)

As you can see no data left in the columns because if the column grade is greater than equal to 1
or greater than equal to 2 that row will be deleted.

EXIST SUBQUERY
8
In this EXIST SUBQUERY, you will see that the data in column from dep_id and emp_name are
exist from the table employees.

SELECT dep_id,dep_name
from department
WHERE EXISTS (SELECT * FROM employees WHERE employees.dep_id = department.dep_id)

The data in columns Department ID and Department name from the table employees have
been found in this EXIST SUBQUERY.

NOT EXIST SUBQUERY

SELECT dep_id,emp_name
from employees
WHERE NOT EXISTS (SELECT * FROM department WHERE department.dep_id =
employees.dep_id)

In this NOT EXIST SUBQUERY, you will see that the data in column from the dep_id and
emp_name are not existed from the table employees.

SELECT dep_id,dep_name
from department
WHERE NOT EXISTS (SELECT * FROM employees WHERE employees.dep_id =
department.dep_id)

In this EXIST SUBQUERY, you will see that the data in columns of department id and
department name from the table employees do not exist.

You might also like