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

Practical No. 3 DBMS

DBMS practiacl

Uploaded by

hrushikeshj775
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Practical No. 3 DBMS

DBMS practiacl

Uploaded by

hrushikeshj775
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Title: SQL Queries Demonstrating Joins, Sub-Queries, and Views

Objective:

The objective of this lab is to write SQL queries demonstrating the use of different types of
joins (INNER, LEFT, RIGHT, FULL OUTER), sub-queries, and views using suitable
database tables. This will help students understand how to retrieve and manipulate data from
relational databases using SQL DML (Data Manipulation Language) statements.

Pre-requisites:

 Basic understanding of SQL and relational databases.


 Familiarity with different types of joins, sub-queries, and views in SQL.
 Access to a SQL environment like MySQL, Oracle, or SQL Server.

Theory

SQL (Structured Query Language) is used for managing and manipulating relational
databases. In SQL, Joins, Sub-Queries, and Views are crucial concepts that allow for
powerful data retrieval and manipulation.

1. Joins

Joins are used to combine rows from two or more tables based on a related column between
them. The most common types of joins are:

 INNER JOIN: Returns records that have matching values in both tables.
 LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matched records
from the right table. Records from the left table with no match in the right table will have
NULL values for columns from the right table.
 RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matched
records from the left table. Records from the right table with no match in the left table will
have NULL values for columns from the left table.
 FULL OUTER JOIN: Returns all records when there is a match in either the left or the right
table. If there is no match, the result is NULL on the side that does not have a match.

Example Scenario:

 Consider two tables: Employees and Departments.


o Employees table has columns: EmpID, EmpName, and DeptID.
o Departments table has columns: DeptID and DeptName.
By joining these tables, we can get information about employees along with their
corresponding department names.

2. Sub-Queries

A Sub-Query (or nested query) is a query within another SQL query. It provides results to
the main query, allowing for more complex filtering, aggregations, or calculations.

 Single-row Sub-Query: Returns a single row, often used with comparison operators like =, <,
or >.
 Multiple-row Sub-Query: Returns multiple rows and is used with operators like IN, ANY, or
ALL.
 Correlated Sub-Query: The sub-query references columns from the outer query, making it
dependent on the outer query’s results.

Example Scenario:

 We can use a sub-query to find employees whose salary is above the average salary of their
department.

3. Views

A View is a virtual table created by a SQL query that joins and/or filters data from one or
more tables. It doesn’t store data itself but provides a way to save complex queries for easy
access and reuse.

 Advantages of Views:
o Simplifies complex queries.
o Provides data security by limiting access to specific columns or rows.
o Makes database management easier by encapsulating frequently used logic.

Example Scenario:

 We can create a view that shows employees along with their project names. This allows us
to easily retrieve employee-project details without joining the tables manually every time.

Database Schema

We'll use the following sample tables for this lab:

1. Employees

EmpID EmpName DeptID Salary


1 John Doe 101 5000
2 Jane Smith 102 6000
EmpID EmpName DeptID Salary
3 Mike Brown 101 5500
4 Emily Clark 103 7000
5 Alice Jones 102 8000

2. Departments

DeptID DeptName
101 HR
102 IT
103 Finance
104 Marketing

3. Projects

ProjectID ProjectName EmpID


201 Project Alpha 1
202 Project Beta 2
203 Project Gamma 3
204 Project Delta 4
205 Project Epsilon 5

Step-by-Step Procedure

Step 1: Creating the Database and Tables

Create the above tables in your SQL environment:

CREATE TABLE Employees (


EmpID INT PRIMARY KEY,
EmpName VARCHAR(50),
DeptID INT,
Salary DECIMAL(10, 2)
);

CREATE TABLE Departments (


DeptID INT PRIMARY KEY,
DeptName VARCHAR(50)
);

CREATE TABLE Projects (


ProjectID INT PRIMARY KEY,
ProjectName VARCHAR(50),
EmpID INT
);

Step 2: Inserting Sample Data


INSERT INTO Employees (EmpID, EmpName, DeptID, Salary)
VALUES (1, 'John Doe', 101, 5000),
(2, 'Jane Smith', 102, 6000),
(3, 'Mike Brown', 101, 5500),
(4, 'Emily Clark', 103, 7000),
(5, 'Alice Jones', 102, 8000);

INSERT INTO Departments (DeptID, DeptName)


VALUES (101, 'HR'),
(102, 'IT'),
(103, 'Finance'),
(104, 'Marketing');

INSERT INTO Projects (ProjectID, ProjectName, EmpID)


VALUES (201, 'Project Alpha', 1),
(202, 'Project Beta', 2),
(203, 'Project Gamma', 3),
(204, 'Project Delta', 4),
(205, 'Project Epsilon', 5);

Step 3: Executing SQL Queries

1. INNER JOIN

Query to retrieve all employees and their department names:

SELECT e.EmpID, e.EmpName, d.DeptName


FROM Employees e
INNER JOIN Departments d ON e.DeptID = d.DeptID;

2. LEFT JOIN

Query to list all departments and their employees, including departments with no employees:

SELECT d.DeptName, e.EmpName


FROM Departments d
LEFT JOIN Employees e ON d.DeptID = e.DeptID;

3. RIGHT JOIN

Query to list all employees and their department names, including employees not assigned to
any department:

SELECT e.EmpName, d.DeptName


FROM Employees e
RIGHT JOIN Departments d ON e.DeptID = d.DeptID;

4. FULL OUTER JOIN (if supported by your database)

Query to list all employees and all departments, even if there are no matches:

SELECT e.EmpName, d.DeptName


FROM Employees e
FULL OUTER JOIN Departments d ON e.DeptID = d.DeptID;
5. Self-Join

Query to find pairs of employees working in the same department:

SELECT e1.EmpName AS Employee1, e2.EmpName AS Employee2, d.DeptName


FROM Employees e1
JOIN Employees e2 ON e1.DeptID = e2.DeptID AND e1.EmpID < e2.EmpID
JOIN Departments d ON e1.DeptID = d.DeptID;

6. Sub-Query (Single Row)

Query to find the employee with the highest salary:

SELECT EmpName, Salary


FROM Employees
WHERE Salary = (SELECT MAX(Salary) FROM Employees);

7. Sub-Query (Multiple Rows)

Query to find all employees who work in departments where the department name starts with
'I':

SELECT EmpName, DeptID


FROM Employees
WHERE DeptID IN (SELECT DeptID FROM Departments WHERE DeptName LIKE 'I%');

8. Correlated Sub-Query

Query to find employees whose salary is above the average salary of their respective
department:

SELECT EmpName, Salary, DeptID


FROM Employees e
WHERE Salary > (SELECT AVG(Salary)
FROM Employees
WHERE DeptID = e.DeptID);

9. Creating a View

Create a view to show employee details along with their project names:

CREATE VIEW EmployeeProjectView AS


SELECT e.EmpID, e.EmpName, p.ProjectName
FROM Employees e
LEFT JOIN Projects p ON e.EmpID = p.EmpID;

Query to retrieve data from the view:

SELECT * FROM EmployeeProjectView;

10. Using View with Aggregation

Query to find the number of projects assigned to each employee using the view:
sql
Copy code
SELECT EmpName, COUNT(ProjectName) AS ProjectCount
FROM EmployeeProjectView
GROUP BY EmpName;

Step 4: Testing the Queries

1. Run each of the queries one by one and verify the results.
2. Observe the output to understand how each type of join, sub-query, and view works.

Exercises:

1. Write a query using a cross join to list all possible pairs of employees and
departments.
2. Create a view to show department details along with the total salary expense for each
department.
3. Write a query to find employees working on more than one project using the view
EmployeeProjectView.

You might also like