DBMS Unit - 2
DBMS Unit - 2
For better understanding watch this youtube playlist along with the
topics covered in this written material--
https://youtube.com/playlist?list=PLxCzCOWd7aiFAN6I8CuViBuCdJgiOkT2Y
Relational Algebra
Relational algebra is a procedural query language. It gives a step by step process to
obtain the result of the query. It uses operators to perform queries.
1. Select Operation:
1. Notation: σ p(r)
Where:
https://cgccollegespace.live
r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR
and NOT. These relations can be used as relational operators like =, ≠, ≥, <, >, ≤.
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
● This operation shows the list of those attributes that we wish to appear in the
https://cgccollegespace.live
● It is denoted by ∏.
Where
Input:
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
https://cgccollegespace.live
Brooks Brooklyn
3. Union Operation:
● Suppose there are two tuples R and S. The union operation contains all the
1. Notation: R ∪ S
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
https://cgccollegespace.live
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
https://cgccollegespace.live
Williams
Mayes
4. Set Intersection:
● Suppose there are two tuples R and S. The set intersection operation contains
● It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
● Suppose there are two tuples R and S. The set intersection operation contains
1. Notation: R - S
Input:
https://cgccollegespace.live
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
● The Cartesian product is used to combine each row in one table with each row
● It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
https://cgccollegespace.live
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho
(ρ).
1. ρ(STUDENT1, STUDENT)
https://cgccollegespace.live
Note: Apart from these common operations Relational algebra can be used in Join
operations
Relational Calculus
● Relational calculus is a non-procedural query language. In the non-procedural
query language, the user is concerned with the details of how to obtain the
end results.
● The relational calculus tells what to do but never explains how to do.
https://cgccollegespace.live
Notation:
Where
For example:
OUTPUT: This query selects the tuples from the AUTHOR relation. It returns a tuple
with 'name' from Author who has written an article on 'database'.
TRC (tuple relational calculus) can be quantified. In TRC, we can use Existential (∃)
and Universal Quantifiers (∀).
For example:
Output: This query will yield the same result as the previous one.
● Domain relational calculus uses the same operators as tuple calculus. It uses
● It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable.
Notation:
Where
For example:
https://cgccollegespace.live
1. {< article, page, subject > | ∈ javatpoint ∧ subject = 'database'}
Output: This query will yield the article, page, and subject from the relational
javatpoint, where the subject is a database
https://cgccollegespace.live
SQL
SQL (Structured Query Language) is used to perform operations on the records
stored in the database such as updating records, deleting records, creating and
SQL is just a query language; it is not a database. To perform SQL queries, you
need to install any database, for example, Oracle, MySQL, MongoDB, PostGreSQL,
What is SQL
● SQL stands for Structured Query Language.
system (RDBMS).
All DBMS like MySQL, Oracle, MS Access, Sybase, Informix, PostgreSQL, and SQL
https://cgccollegespace.live
● To delete records from a database
statements.
● With SQL, a user can access data from a relational database management
system.
● It allows the user to define the data in the database and manipulate it when
needed.
database.
SQL Commands
● SQL commands are instructions. It is used to communicate with the database.
● SQL can perform various tasks like create a table, add data to tables, drop
https://cgccollegespace.live
1. Data Definition Language (DDL)
● DDL changes the structure of the table like creating a table, deleting a table,
● All the command of DDL are auto-committed that means it permanently save
● CREATE
● ALTER
● DROP
● TRUNCATE
Syntax:
https://cgccollegespace.live
Example:
DOB DATE);
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax
1. DROP TABLE ;
Example
c. ALTER: It is used to alter the structure of the database. This change could be
either to modify the characteristics of an existing attribute or probably to add a new
attribute.
Syntax:
EXAMPLE
d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.
Syntax:
Example:
https://cgccollegespace.live
2. Data Manipulation Language
● DML commands are used to modify the database. It is responsible for all form
save all the changes in the database. They can be rolled back.
● INSERT
● UPDATE
● DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the
row of a table.
Syntax:
Or
For example:
b. UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax:
For example:
https://cgccollegespace.live
1. UPDATE students
Syntax:
For example:
2. WHERE Author="Sonoo";
● Grant
● Revoke
Example
Example
These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them.
https://cgccollegespace.live
Here are some commands that come under TCL:
● COMMIT
● ROLLBACK
● SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:
1. COMMIT;
Example:
3. COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax:
1. ROLLBACK;
Example:
3. ROLLBACK;
Syntax:
1. SAVEPOINT SAVEPOINT_NAME;
https://cgccollegespace.live
● SELECT
Syntax:
1. SELECT expressions
2. FROM TABLES
3. WHERE conditions;
For example:
1. SELECT emp_name
2. FROM employee
https://cgccollegespace.live
The cost of commercial software
02 The cost of open source software
varies from moderate to
. is free.
expensive.
https://cgccollegespace.live
08 Here in open source software But in commercial software mainly
. users can customize. vendors offer customization.
1. MySQL :
MySQL is an open-source relational database management system (RDBMS)
based on Structured Query Language (SQL). It is developed and managed by
oracle corporation and initially released on 23 May, 1995. It is widely being
https://cgccollegespace.live
used in many small and large scale industrial applications and capable of
handling a large volume of data.
2. IBM Db2 :
IBM Db2 is a family of data management products, including database
servers, developed by IBM. It is a Relational Database Management System
(RDBMS) which supports object-oriented features and non-relational structure
with XML. Db2 is designed to store, analyze and retrieve the data efficiently. It
was initially released in 1983 and is written in C, C++, Java and Assembly
language.
3. Oracle :
Oracle is a relational database management system (RDBMS). It was
developed by Oracle Corporation in 1980. It is the first database designed for
grid computing that provides the most flexible and cost-effective way to
manage information and application. It runs on major platforms like Windows,
Unix, Linux, and macOS. It is a relational database in which data is accessed
by user through an application or query language called SQL.
https://cgccollegespace.live
https://www.geeksforgeeks.org/difference-between-oracle-and-mysql/
https://www.geeksforgeeks.org/difference-between-mysql-and-ms-sql-server/
https://www.geeksforgeeks.org/difference-between-mysql-and-ibm-db2/
Relational database design (RDD) models information and data into a set of tables
with rows and columns. Each row of a relation/table represents a record, and each
column represents an attribute of data. The Structured Query Language (SQL) is
used to manipulate relational databases. The design of a relational database is
composed of four stages, where the data are modeled into a set of related tables.
The stages are:
● Define relations/attributes
● Define primary keys
● Define relationships
● Normalization
https://cgccollegespace.live
Domain
Functional Dependency
The functional dependency is a relationship that exists between two attributes. It
typically exists between the primary key and non-key attribute within a table.
1. X → Y
The left side of FD is known as a determinant, the right side of the production is
known as a dependent.
For example:
https://cgccollegespace.live
Assume we have an employee table with attributes: Emp_Id, Emp_Name,
Emp_Address.
Here Emp_Id attribute can uniquely identify the Emp_Name attribute of the
employee table because if we know the Emp_Id, we can tell that employee name
associated with it.
1. Emp_Id → Emp_Name
Example:
dependency as
https://cgccollegespace.live
4. Also, Employee_Id → Employee_Id and Employee_Name →
Example:
1. ID → Name,
2. Name → DOB
relational database.
● Using the inference rule, we can derive additional functional dependency from
1. If X ⊇ Y then X → Y
Example:
1. X = {a, b, c, d, e}
https://cgccollegespace.live
2. Y = {a, b, c}
1. If X → Y then XZ → YZ
Example:
1. If X → Y and Y → Z then X → Z
1. If X → Y and X → Z then X → YZ
Proof:
1. X → Y (given)
2. X → Z (given)
3. X → XY (using IR2 on 1 by augmentation with X. Where XX = X)
https://cgccollegespace.live
1. If X → YZ then X → Y and X → Z
Proof:
1. X → YZ (given)
2. YZ → Y (using IR1 Rule)
1. If X → Y and YZ → W then XZ → W
Proof:
1. X → Y (given)
2. WY → Z (given)
3. WX → WY (using IR2 on 1 by augmenting with W)
4. WX → Z (using IR3 on 3 and 2)
Integrity Constraints
● Integrity constraints are a set of rules. It is used to maintain the quality of
information.
● Integrity constraints ensure that the data insertion, updating, and other
affected.
database.
https://cgccollegespace.live
1. Domain constraints
● Domain constraints can be defined as the definition of a valid set of values for
an attribute.
● The data type of domain includes string, character, integer, time, date,
corresponding domain.
Example:
● The entity integrity constraint states that primary key value can't be null.
https://cgccollegespace.live
● This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify those
rows.
● A table can contain a null value other than the primary key field.
Example:
Primary Key of Table 2, then every value of the Foreign Key in Table 1 must
Example:
https://cgccollegespace.live
4. Key constraints
● Keys are the entity set that is used to identify an entity within its entity set
uniquely.
● An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and null value in the
relational table.
Example:
Normalization
● Normalization is the process of organizing the data in the database.
● Normalization divides the larger table into the smaller table and links them
using relationship.
● The normal form is used to reduce redundancy from the database table.
https://cgccollegespace.live
Norma Description
l Form
2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are
exists.
4NF A relation will be in 4NF if it is in Boyce Codd normal form and has no
multivalued dependency.
5NF A relation is in 5NF if it is in 4NF and does not contain any join
https://cgccollegespace.live
● It states that an attribute of a table cannot hold multiple values. It must hold
EMPLOYEE table:
14 John 7272826385, UP
9064738238
8589830302
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385 UP
14 John 9064738238 UP
https://cgccollegespace.live
Second Normal Form (2NF)
● In the second normal form, all non-key attributes are fully functional
Example: Let's assume, a school can store the data of teachers and the subjects
they teach. In a school, a teacher can teach more than one subject.
TEACHER table
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
TEACHER_SUBJECT table:
https://cgccollegespace.live
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
● A relation will be in 3NF if it is in 2NF and not contain any transitive partial
dependency.
● 3NF is used to reduce the data duplication. It is also used to achieve data
integrity.
A relation is in third normal form if it holds at least one of the following conditions
for every non-trivial functional dependency X → Y.
1. X is a super key.
Example:
EMPLOYEE_DETAIL table:
https://cgccollegespace.live
444 Lan 60007 US Chicago
EMP_ZIP}....so on
3.
EMPLOYEE_ZIP table:
https://cgccollegespace.live
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
the table.
● For BCNF, the table should be in 3NF, and for every FD, LHS is super key.
Example: Let's assume there is a company where employees work in more than
one department.
EMPLOYEE table:
1. EMP_ID → EMP_COUNTRY
https://cgccollegespace.live
Candidate key: {EMP-ID, EMP-DEPT}
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
EMP_COUNTRY table:
EMP_ID EMP_COUNTRY
264 India
264 India
EMP_DEPT table:
EMP_DEPT_MAPPING table:
EMP_ID EMP_DEPT
D394 283
D394 300
D283 232
D283 549
Functional dependencies:
1. EMP_ID → EMP_COUNTRY
https://cgccollegespace.live
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
Now, this is in BCNF because the left side part of both the functional dependencies is
a key.
multivalued dependency.
Example
STUDENT
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
The given STUDENT table is in 3NF, but the COURSE and HOBBY are two
independent entity. Hence, there is no relationship between COURSE and HOBBY.
https://cgccollegespace.live
In the STUDENT relation, a student with STU_ID, 21 contains two courses,
Computer and Math and two hobbies, Dancing and Singing. So there is a
Multi-valued dependency on STU_ID, which leads to unnecessary repetition of data.
So to make the above table into 4NF, we can decompose it into two tables:
STUDENT_COURSE
STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
● A relation is in 5NF if it is in 4NF and does not contain any join dependency
https://cgccollegespace.live
● 5NF is satisfied when all the tables are broken into as many tables as possible
Example
In the above table, John takes both Computer and Math class for Semester 1 but he
doesn't take Math class for Semester 2. In this case, combination of all these fields
required to identify a valid data.
Suppose we add a new Semester as Semester 3 but do not know about the subject
and who will be taking that subject so we leave Lecturer and Subject as NULL. But
all three columns together acts as a primary key, so we can't leave other two
columns blank.
So to make the above table into 5NF, we can decompose it into three relations P1,
P2 & P3:
P1
SEMESTER SUBJECT
Semester 1 Computer
Semester 1 Math
Semester 1 Chemistry
https://cgccollegespace.live
Semester 2 Math
P2
SUBJECT LECTURER
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen
P3
SEMESTER LECTURER
Semester 1 Anshika
Semester 1 John
Semester 1 John
Semester 2 Akash
Semester 1 Praveen
Dependency Preserving:
● It is an important constraint of the database.
every dependency.
https://cgccollegespace.live
● If a relation R is decomposed into relation R1 and R2, then the dependencies
relation R1(ABC).
Lossless Decomposition:
Decomposition is lossless if it is feasible to reconstruct relation R from decomposed
tables using Joins. This is the preferred choice. The information will not lose from the
relation when decomposed. The join would result in the same original relation.
Let us see an example −
<EmpInfo>
<EmpDetails>
https://cgccollegespace.live
E003 Tom 22 Texas
<DeptDetails>
Therefore, the above relation had lossless decomposition i.e. no loss of information.
Lossy Decomposition
As the name suggests, when a relation is decomposed into two or more relational
schemas, the loss of information is unavoidable when the original relation is
retrieved.
https://cgccollegespace.live
<EmpDetails>
<DeptDetails>
Dept_ID Dept_Name
Dpt1 Operations
Dpt2 HR
Dpt3 Finance
Now, you won’t be able to join the above tables, since Emp_ID isn’t part of the
DeptDetails relation.
Therefore, the above relation has lossy decomposition.
2. Optimization
3. Evaluation
https://cgccollegespace.live
-optimizing transformations takes place. Thus before processing a query, a computer
system needs to translate the query into a human-readable and understandable
language. Consequently, SQL or Structured Query Language is the best suitable
choice for humans. But, it is not perfectly suitable for the internal representation of
the query to the system. Relational algebra is well suited for the internal
representation of a query. The translation process in query processing is similar to
the parser of a query. When a user executes any query, for generating the internal
form of the query, the parser in the system checks the syntax of the query, verifies
the name of the relation in the database, the tuple, and finally the required attribute
value. The parser creates a tree of the query, known as 'parse-tree.' Further,
translate it into the form of relational algebra. With this, it evenly replaces all the
use of the views when used in the query.
https://cgccollegespace.live
Suppose a user executes a query. As we have learned that there are various
methods of extracting the data from the database. In SQL, a user wants to fetch the
records of the employees whose salary is greater than or equal to 10000. For doing
this, the following query is undertaken:
Thus, to make the system understand the user query, it needs to be translated in
the form of relational algebra. We can bring this query in the relational algebra form
as:
After translating the given query, we can execute each relational algebra operation
by using different algorithms. So, in this way, query processing begins working.
https://cgccollegespace.live
Evaluation
For this, with addition to the relational algebra translation, it is required to annotate
the translated relational algebra expression with the instructions used for specifying
and evaluating each operation. Thus, after translating the user query, the system
executes a query evaluation plan.
evaluation plan.
● The annotations in the evaluation plan may refer to the algorithms to be used
Primitives. The evaluation primitives carry the instructions needed for the
for evaluating a query. The query evaluation plan is also referred to as the
given query. It takes the query execution plan, executes it, and finally makes
Optimization
● The cost of the query evaluation can vary for different types of queries.
Although the system is responsible for constructing the evaluation plan, the
which minimizes its cost. This type of task is performed by the database
https://cgccollegespace.live
● For optimizing a query, the query optimizer should have an estimated cost
Finally, after selecting an evaluation plan, the system evaluates the query and
produces the output of the query.
This rule states the deconstruction of the conjunctive selection operations into a
sequence of individual selections. Such a transformation is known as a cascade of
σ.
σθ1 (σθ2
(E)) = σ θ2
(σθ1
(E))
https://cgccollegespace.live
However, in the case of theta join, the equivalence rule does not work if the order of
attributes is considered. Natural join is a special case of Theta join, and natural join
is also commutative.
However, in the case of theta join, the equivalence rule does not work if the order of
attributes is considered. Natural join is a special case of Theta join, and natural join
is also commutative.
This rule states that we only need the final operations in the sequence of the
projection operations, and other operations are omitted. Such a transformation is
referred to as a cascade of ∏.
Rule 4: We can combine the selections with Cartesian products as well as theta
joins
Rule 4: We can combine the selections with Cartesian products as well as theta
joins
2. σθ1 (E1 ⋈ θ2
E2 )
= E1 ᴧθ2 E2
⋈ θ1
(E1 ⋈ θ1
E2 ) ᴧθ3 E3 = E1 ⋈ θ1
⋈ θ2 ᴧθ3 (E2 ⋈ θ2
E3 )
In the theta associativity, θ2 involves the attributes from E2 and E3 only. There may
be chances of empty conditions, and thereby it concludes that Cartesian Product is
also associative.
Note: The equivalence rules of associativity and commutatively of join operations are
essential for join reordering in query optimization.
Rule 6: Distribution of the Selection operation over the Theta join.
Under two following conditions, the selection operation gets distributed over the
theta-join operation:
https://cgccollegespace.live
a) When all attributes in the selection condition θ0 include only attributes of one of
the expressions which are being joined.
b) When the selection condition θ1 involves the attributes of E1 only, and θ2 includes
the attributes of E2 only.
Rule 7: Distribution of the projection operation over the theta join.
Under two following conditions, the selection operation gets distributed over the
theta-join operation:
a) Assume that the join condition θ includes only in L1 υ L2 attributes of E1 and E2
Then, we get the following expression:
∏L1υL2
(E1 ⋈ θ
E2) = (∏L1 (E1)) ⋈ θ (∏L2
(E2 ))
b) Assume a join as E1 ⋈ E2. Both expressions E1 and E2 have sets of attributes as L1
and L2. Assume two attributes L3 and L4 where L3 be attributes of the expression E1,
involved in the θ join condition but not in L1 υ L2 Similarly, an L4 be attributes of the
expression E2 involved
only in the θ join condition and not in L1 υ L2 attributes.
Thus,
we get the following expression:
∏L1υL2
(E1 ⋈ θ
E2) = ∏L1υL2 ((∏L1υL3
(E1)) ⋈ θ ((∏L2υL4 (E2
)))
Rule 8: The union and intersection set operations are commutative.
Rule 9: The union and intersection set operations are associative.
(E1 E2 )
E3 = E1 (E2 E3 )
Rule 10: Distribution of selection operation on the intersection, union, and set
difference operations.
The below expression shows the distribution performed over the set difference
operation.
https://cgccollegespace.live
σp (E1 − E2)
= σp(E
)
1 −E2
Rule 11: Distribution of the projection operation over the union operation.
This rule states that we can distribute the projection operation on the union
operation for the given expressions.
Apart from these discussed equivalence rules, there are various other equivalence
rules also.
SQL JOIN
As the name shows, JOIN means to combine something. In case of SQL, JOIN means
"to combine two or more tables".
In SQL, JOIN clause is used to combine the records from two or more tables in a
database.
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
Sample Table
https://cgccollegespace.live
EMPLOYEE
PROJECT
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
https://cgccollegespace.live
In SQL, INNER JOIN selects records that have matching values in both tables as long
as the condition is satisfied. It returns the combination of all rows from both the
tables where the condition satisfies.
Syntax
2. FROM table1
4. ON table1.matching_column = table2.matching_column;
Query
2. FROM EMPLOYEE
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table and the matching values from
the right table. If there is no matching join value, it will return NULL.
Syntax
https://cgccollegespace.live
1. SELECT table1.column1, table1.column2, table2.column1,....
2. FROM table1
4. ON table1.matching_column = table2.matching_column;
Query
2. FROM EMPLOYEE
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of right
table and the matched values from the left table. If there is no matching in both
tables, it will return NULL.
https://cgccollegespace.live
Syntax
2. FROM table1
4. ON table1.matching_column = table2.matching_column;
Query
2. FROM EMPLOYEE
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right outer join.
Join tables have all the records from both tables. It puts NULL on the place of
matches not found.
Syntax
2. FROM table1
https://cgccollegespace.live
3. FULL JOIN table2
4. ON table1.matching_column = table2.matching_column;
Query
2. FROM EMPLOYEE
4. ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
https://cgccollegespace.live
UNIT - 2 COMPLETED
https://cgccollegespace.live