Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Lab Manual - USCS103

Skill Enhancement: Database Systems


Roll No. CS21019 Date: 19/03/2022

Practical No. 6
Aim:
Write queries to implement joins on multiple tables.
Note: Create a database named as my_joins

Objective:
A. Create the following tables:
1. employee

2. department
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

3. branch

DEPTNO is a foreign key


B_ID is a foreign key

B. Fire the following queries on the tables:


1. Display the names and department names of all employees.
2. Display the experience of all employees with their names and department
name.
3. Display the employee name with their manager name.
4. A query that retrieves the number and name of each employee, and
calculates their annual income.
5. Display the location of SMITH.
6. Display all employees working in Accounting Department.
7. Display the name, location and branch name of all employee.
8. Display the name, location and branch name of all employee working in
“head office”.
9. Display the name, location of employees having salary between 1500 to
2000.
10. Display the name and branch_name of employees having salary between
1500 to 2000.
11. Display the location and branch of Scott.
12. Display all employees working in SALES Department.
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

A.Create the following tables


1. employee
Note: The HIREDATE column has been explicitly converted to a DATE type rather
than using a VARCHAR to store this data, as it allows us more flexibility when
querying data from this table. The required formatting can be achieved with
MySQL functions.

Query:
CREATE TABLE employee (
EMPNO INT NOT NULL,
ENAME VARCHAR(32) NOT NULL,
JOB VARCHAR(16) NOT NULL,
HIREDATE DATE NOT NULL,
MGR INT,
SAL INT NOT NULL,
COMM INT,
DEPTNO INT NOT NULL,
PRIMARY KEY (EMPNO)
);

INSERT INTO employee VALUES


(7369, "SMITH", "CLERK", "1980-12-17", 7902, 800, NULL,
20),
(7499, "ALLEN", "SALESMAN", "1981-02-20", 7698, 1600,
300, 30),
(7521, "WARD", "SALESMAN", "1981-02-22", 7698, 1250, 500,
30),
(7566, "JONES", "MANAGER", "1981-04-02", 7839, 2975,
NULL, 20),
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

(7654, "MARTIN", "SALESMAN", "1981-09-28", 7698, 1250,


1400, 30),
(7698, "BLAKE", "MANAGER", "1981-05-01", 7839, 2850,
NULL, 30),
(7782, "CLARK", "MANAGER", "1981-06-09", 7839, 2450,
NULL, 10),
(7788, "SCOTT", "ANALYST", "1987-04-19", 7566, 3000,
NULL, 20),
(7839, "KING", "PRESIDENT", "1981-11-17", NULL, 5000,
NULL, 10),
(7844, "TURNER", "SALESMAN", "1981-09-08", 7698, 1500, 0,
30);

INSERT INTO employee VALUES


(7876, "ADAMS", "CLERK", "1987-05-23", 7788, 1100, NULL,
20),
(7900, "JAMES", "CLERK", "1981-12-03", 7698, 950, NULL,
30),
(7902, "FORD", "ANALYST", "1981-12-03", 7566, 3000, NULL,
20),
(7934, "MILLER", "CLERK", "1982-01-23", 7782, 1300, NULL,
10);
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

2. department

Query:
CREATE TABLE department (
DEPTNO INT NOT NULL,
DNAME VARCHAR(32) NOT NULL,
LOC VARCHAR(32) NOT NULL,
B_ID INT NOT NULL,
PRIMARY KEY (DEPTNO)
);

INSERT INTO department VALUES


(10, "ACCOUNTING", "NEW YORK", 2),
(20, "RESEARCH", "DALLAS", 2),
(30, "SALES", "CHICAGO", 3),
(40, "OPERATIONS", "BOSTON", 1);

ALTER TABLE employee ADD FOREIGN KEY (DEPTNO) REFERENCES


department(DEPTNO);

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

3. branch

Query:
CREATE TABLE branch (
B_ID INT NOT NULL,
BRANCH_NAME VARCHAR(32) NOT NULL,
PRIMARY KEY (B_ID)
);

INSERT INTO branch VALUES


(1, "HEAD OFFICE"),
(2, "SUB OFFICE"),
(3, "MAIN BRANCH");

ALTER TABLE department ADD FOREIGN KEY (B_ID) REFERENCES


branch(B_ID);

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

B.Fire the following queries on the tables


1. Display the names and department names of all employees

Query:
SELECT e.ENAME, d.DNAME FROM employee e LEFT JOIN
department d ON e.DEPTNO = d.DEPTNO;

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

2. Display the experience of all employees with their names and


department name

Query:
SELECT e.ENAME, d.DNAME, FLOOR(DATEDIFF(NOW(),
e.HIREDATE) / 365.25) AS EXPERIENCE FROM employee e
LEFT JOIN department d ON e.DEPTNO = d.DEPTNO;

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

3. Display the employee name with their manager name

Query:
SELECT e.ENAME AS EMPLOYEE_NAME, m.ENAME AS MANAGER_NAME
FROM employee e LEFT JOIN employee m ON e.MGR =
m.EMPNO;

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

4. A query that retrieves the number and name of each employee, and
calculates their annual income

Query:
SELECT EMPNO, ENAME, (SAL * 12) AS ANNUAL_SALARY FROM
employee;

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

5. Display the location of SMITH

Query:
SELECT e.ENAME, d.LOC AS LOCATION FROM employee e INNER
JOIN department d ON e.DEPTNO = d.DEPTNO WHERE e.ENAME
= "SMITH";

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

6. Display all employees working in Accounting Department

Query:
SELECT e.EMPNO, e.ENAME FROM employee e INNER JOIN
department d ON e.DEPTNO = d.DEPTNO WHERE d.DNAME =
"ACCOUNTING";

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

7. Display the name, location and branch name of all employee

Query:
SELECT e.ENAME AS NAME, d.LOC AS LOCATION, b.BRANCH_NAME
AS BRANCH_NAME FROM employee e LEFT JOIN department d
ON e.DEPTNO = d.DEPTNO LEFT JOIN branch b ON d.B_ID =
b.B_ID;

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

8. Display the name, location and branch name of all employee


working in “HEAD OFFICE”

Adding Test Data:


INSERT INTO employee VALUES
(7940, "ALAN", "HR", "1987-05-23", 7788, 1100, NULL, 40),
(7941, "JACK", "HR", "1981-12-03", 7698, 950, NULL, 40),
(7942, "RIN", "HR", "1981-12-03", 7566, 3000, NULL, 40),
(7943, "MATT", "HR", "1982-01-23", 7782, 1300, NULL, 40);

Query:
SELECT e.ENAME AS NAME, d.LOC AS LOCATION, b.BRANCH_NAME
AS BRANCH_NAME FROM employee e LEFT JOIN department d
ON e.DEPTNO = d.DEPTNO LEFT JOIN branch b ON d.B_ID =
b.B_ID WHERE b.BRANCH_NAME = "HEAD OFFICE";

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

9. Display the name, location of employees having salary between 1500


to 2000

Query:
SELECT e.ENAME AS NAME, d.LOC AS LOCATION FROM employee e
LEFT JOIN department d ON e.DEPTNO = d.DEPTNO WHERE
e.SAL > 1500 AND e.SAL < 2000;

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

10. Display the name and branch_name of employees having salary


between 1500 to 2000

Query:
SELECT e.ENAME AS NAME, b.BRANCH_NAME AS BRANCH_NAME FROM
employee e LEFT JOIN department d ON e.DEPTNO =
d.DEPTNO LEFT JOIN branch b ON d.B_ID = b.B_ID WHERE
e.SAL > 1500 AND e.SAL < 2000;

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

11. Display the location and branch of Scott

Query:
SELECT e.ENAME AS NAME, d.LOC AS LOCATION, b.BRANCH_NAME
AS BRANCH_NAME FROM employee e LEFT JOIN department d
ON e.DEPTNO = d.DEPTNO LEFT JOIN branch b ON d.B_ID =
b.B_ID WHERE e.ENAME = "SCOTT";

Output:
Lab Manual - USCS103
Skill Enhancement: Database Systems
Roll No. CS21019 Date: 19/03/2022

12. Display all employees working in SALES Department

Query:
SELECT e.EMPNO, e.ENAME FROM employee e LEFT JOIN
department d ON e.DEPTNO = d.DEPTNO WHERE d.DNAME =
"SALES";

Output:

You might also like