Practical No. 3 DBMS
Practical No. 3 DBMS
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:
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:
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
1. Employees
2. Departments
DeptID DeptName
101 HR
102 IT
103 Finance
104 Marketing
3. Projects
Step-by-Step Procedure
1. INNER JOIN
2. LEFT JOIN
Query to list all departments and their employees, including departments with no employees:
3. RIGHT JOIN
Query to list all employees and their department names, including employees not assigned to
any department:
Query to list all employees and all departments, even if there are no matches:
Query to find all employees who work in departments where the department name starts with
'I':
8. Correlated Sub-Query
Query to find employees whose salary is above the average salary of their respective
department:
9. Creating a View
Create a view to show employee details along with their project names:
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;
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.