You are on page 1of 5

Mark Angelo C.

Vacal

IT3A

ACTIVITY#1 – JOINS

1. CREATING A DATABASE QUERY :


- CREATE DATABASE EMPLOYEE;

2. CREATING TABLES QUERY :

CREATE TABLE EMPLOYEES.EMP(ID INT NOT NULL,


First_Name VARCHAR(45)NULL,
Last_Name VARCHAR (45)NULL,
Dept_Name VARCHAR(45)NULL,
Dept_ID INT NULL,
PRIMARY KEY(ID),
FOREIGN KEY (Dept_ID) REFERENCES department (ID));
CREATE TABLE EMPLOYEES.DEPARTMENT(ID INT NOT NULL,
Dept_Name VARCHAR(45)NULL,
Dept_Location VARCHAR(45)NULL,
Dept_Course VARCHAR(45)NULL,
Dept_NO VARCHAR(45)NULL,
PRIMARY KEY (ID));

3. INSERTING RECORDS IN TABLE QUERY :

INSERT INTO EMP VALUES

(1, "Lucy", "Heartfilia", "College of Art and Sciences", 2),


(2, "Natsu", "Dragneel", "College of Engineering", 1),
(3, "Grey", "Fullbuster", "College of Business", 4),
(4, "Erza", "Scarlet", "College of Education And Human Development", 5),
(5, "Wendy", "Marvel", "College of Fine Arts And Communications", 3);
(6, "Mirajane", "Strauss", “College of Education And Human Development ", 5);
(7, "Laxus", "Dreyar", “College of Engineering", 1);

INSERT INTO department VALUES


(1, "College of Engineering", "Topaz Bldg", "Mechanical Engineering", 1),
(2, "College of Art and Sciences", "Ruby Bldg", "Chemistry and BioChemistry", 2),
(3, "College of Fine Arts and Communication", "Diamond Bldg", "Arts and Design", 3),
(4, "College of Business", "Emerald Bldg", "Accountancy", 4),
(5, "College of Education And Human Development", "Pearl Bldg", "Education", 5);

4. USING UNIONS
QUERY

UNION

DISTINCT

SELECT First_Name
FROM emp
UNION
SELECT Dept_Name
FROM emp;

If some First name have the same Department Name, each Department Name will only be
listed once, because UNION selects only distinct values
UNION DISTINCT

SELECT First_Name, Last_Name


FROM emp
UNION
SELECT Dept_Name, Dept_ID
FROM emp

As you can see First name and Last Name have the same Department Name and ID each
Department Name And ID will only be listed once, because UNION selects only distinct
values.

UNION ALL

SELECT First_Name
FROM emp
UNION ALL
SELECT Dept_Name
FROM emp;

UNION ALL command combines the result set of two or more SELECT statements (allows
duplicate values).
UNION ALL

SELECT First_Name, Last_Name


FROM emp
UNION ALL
SELECT Dept_Name, Dept_ID
FROM emp;

The UNION ALL command aggregates the output of two or more SELECT queries into a single
data structure (allows duplicate values).

You might also like