DBMS Normal Forms Lab (1)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 86

SQL Set Operation

• The SQL Set operation is used to combine the two or more SQL
SELECT statements.

• Types of Set Operation


1.Union
2.UnionAll
3.Intersect
4.Minus
1. Union

• The SQL Union operation is used to combine the result of two or more
SQL SELECT queries.
• In the union operation, all the number of datatype and columns must
be same in both the tables on which UNION operation is being
applied.
• The union operation eliminates the duplicate rows from its resultset.
Syntax
1.SELECT column_name FROM table1
2.UNION
3.SELECT column_name FROM table2;
Example:
The First table
The Second table
Union SQL query will be:
1.SELECT * FROM First
2.UNION
3.SELECT * FROM Second;
The resultset table will look like:
2. Union All

• Union All operation is equal to the Union operation. It returns the set
without removing duplication and sorting the data.
• Syntax:

1.SELECT column_name FROM table1


2.UNION ALL
3.SELECT column_name FROM table2;
• Example: Using the above First and Second table.

• Union All query will be like:

1.SELECT * FROM First


2.UNION ALL
3.SELECT * FROM Second;
The result set table will look like:
3. Intersect

• It is used to combine two SELECT statements. The Intersect operation


returns the common rows from both the SELECT statements.
• In the Intersect operation, the number of datatype and columns must
be the same.
• It has no duplicates and it arranges the data in ascending order by
default.
Syntax
1.SELECT column_name FROM table1
2.INTERSECT
3.SELECT column_name FROM table2;

• Example:
• Using the above First and Second table.
• Intersect query will be:

1.SELECT * FROM First


2.INTERSECT
3.SELECT * FROM Second;
The resultset table will look like:
4. Minus

• It combines the result of two SELECT statements. Minus operator is


used to display the rows which are present in the first query but absent
in the second query.
• It has no duplicates and data arranged in ascending order by default.
Syntax:
1.SELECT column_name FROM table1
2.MINUS
3.SELECT column_name FROM table2;
• Example

• Using the above First and Second table.

• Minus query will be:


1.SELECT * FROM First
2.MINUS
3.SELECT * FROM Second;
The resultset table will look like:
Nested Queries
• A nested query is a query that has another query embedded within it.
The embedded query is called a subquery.
• A subquery typically appears within the WHERE clause of a query. It
can sometimes appear in the FROM clause or HAVING clause.
Important Rule:
• A subquery can be placed in a number of SQL clauses like WHERE
clause, FROM clause, HAVING clause.
• You can use Subquery with SELECT, UPDATE, INSERT, DELETE
statements along with the operators like =, <, >, >=, <=, IN,
BETWEEN, etc.
• A subquery is a query within another query. The outer query is known
as the main query, and the inner query is known as a subquery.
• Subqueries are on the right side of the comparison operator.
• A subquery is enclosed in parentheses.
• In the Subquery, ORDER BY command cannot be used. But GROUP
BY command can be used to perform the same function as ORDER
BY command.
1. Subqueries with the Select Statement

• SQL subqueries are most frequently used with the Select statement.
• Syntax
1.SELECT column_name
2.FROM table_name
3.WHERE column_name expression operator
4.( SELECT column_name from table_name WHERE ... );
Example
• Consider the EMPLOYEE table have the following records:
• The subquery with a SELECT statement will be:

1.SELECT *
2. FROM EMPLOYEE
3. WHERE ID IN (SELECT ID
4. FROM EMPLOYEE
5. WHERE SALARY > 4500);
This would produce the following result:
2. Subqueries with the INSERT Statement

• SQL subquery can also be used with the Insert statement. In the insert
statement, data returned from the subquery is used to insert into
another table.
• In the subquery, the selected data can be modified with any of the
character, date functions.
• Syntax:
1.INSERT INTO table_name (column1, column2, column3....)
2.SELECT *
3.FROM table_name
4.WHERE VALUE OPERATOR
3. Subqueries with the UPDATE Statement

• The subquery of SQL can be used in conjunction with the Update


statement.
• When a subquery is used with the Update statement, then either single
or multiple columns in a table can be updated.
Syntax
1.UPDATE table
2.SET column_name = new_value
3.WHERE VALUE OPERATOR
4. (SELECT COLUMN_NAME
5. FROM TABLE_NAME
6. WHERE condition);
Example
• Let's assume we have an EMPLOYEE table. The given example
updates the SALARY by .25 times in the EMPLOYEE table for all
employee whose AGE is greater than or equal to 29.
1.UPDATE EMPLOYEE
2. SET SALARY = SALARY * 0.25
3. WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
4. WHERE AGE >= 29);
4. Subqueries with the DELETE Statement

• The subquery of SQL can be used in conjunction with the Delete


statement just like any other statements mentioned above.
• Syntax
1.DELETE FROM TABLE_NAME
2.WHERE VALUE OPERATOR
3. (SELECT COLUMN_NAME
4. FROM TABLE_NAME
5. WHERE condition);
Example
• Let's assume we have EMPLOYEE table. The given example deletes
the records from the EMPLOYEE table for all EMPLOYEE whose
AGE is greater than or equal to 29.

1. DELETE FROM EMPLOYEE


2. WHERE AGE IN (SELECT AGE FROM EMPLOYEE
3. WHERE AGE >= 29 );
This would impact three rows, and finally, the
EMPLOYEE table would have the following records.
Aggregate Operators

• SQL aggregation function is used to perform the calculations on


multiple rows of a single column of a table. It returns a single value.
• It is also used to summarize the data.
Types of SQL Aggregation Function
1. COUNT FUNCTION

• COUNT function is used to Count the number of rows in a database


table. It can work on both numeric and non-numeric data types.
• COUNT function uses the COUNT(*) that returns the count of all the
rows in a specified table. COUNT(*) considers duplicate and Null
• Syntax
1.COUNT(*)
2.or
3.COUNT( [ALL|DISTINCT] expression )
Sample table:
• Example: COUNT()
1.SELECT COUNT(*)
2.FROM PRODUCT_MAST;

• Output: 10
• Example: COUNT with WHERE
1.SELECT COUNT(*)
2.FROM PRODUCT_MAST;
3.WHERE RATE>=20;

• Output: 7
• Example: COUNT() with DISTINCT
1.SELECT COUNT(DISTINCT COMPANY)
2.FROM PRODUCT_MAST;

• Output: 3
2. SUM Function

• Sum function is used to calculate the sum of all selected columns. It


works on numeric fields only.
• Syntax
1.SUM()
2.or
3.SUM( [ALL|DISTINCT] expression )
• Example: SUM()
1.SELECT SUM(COST)
2.FROM PRODUCT_MAST;

• Output: 670
• Example: SUM() with WHERE
1.SELECT SUM(COST)
2.FROM PRODUCT_MAST
3.WHERE QTY>3;

• Output: 320
3. AVG function

• The AVG function is used to calculate the average value of the


numeric type. AVG function returns the average of all non-Null values.

• Syntax
1.AVG()
2.or
3.AVG( [ALL|DISTINCT] expression )
• Example:
1.SELECT AVG(COST)
2.FROM PRODUCT_MAST;

• Output: 67
4. MAX Function

• MAX function is used to find the maximum value of a certain column.


This function determines the largest value of all selected values of a
column.

• Syntax
1.MAX()
2.or
3.MAX( [ALL|DISTINCT] expression )
• Example:
1.SELECT MAX(RATE)
2.FROM PRODUCT_MAST;

• Output: 30
5. MIN Function

• MIN function is used to find the minimum value of a certain column.


This function determines the smallest value of all selected values of a
column.

• Syntax
1.MIN()
2.or
3.MIN( [ALL|DISTINCT] expression )
• Example:
1.SELECT MIN(RATE)
2.FROM PRODUCT_MAS

• Output: 10
Normal Forms Lab
• First Normal Form (1NF)
• A relation will be 1NF if it contains an atomic value.
• It states that an attribute of a table cannot hold multiple values. It must
hold only single-valued attribute.
• First normal form disallows the multi-valued attribute, composite
attribute, and their combinations.
• Example: Relation EMPLOYEE is not in 1NF because of multi-
valued attribute EMP_PHONE.
The decomposition of the EMPLOYEE table
into 1NF has been shown below:
Second Normal Form (2NF)

• In the 2NF, relational must be in 1NF.


• In the second normal form, all non-key attributes are fully functional
dependent on the primary key
• 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
• In the given table, non-prime attribute TEACHER_AGE is dependent
on TEACHER_ID which is a proper subset of a candidate key. That's
why it violates the rule for 2NF.
• To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_SUBJECT table:
Third Normal Form (3NF)

• 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 the
data integrity.
• If there is no transitive dependency for non-prime attributes, then the
relation must be in third normal form.
• A relation is in third normal form if it holds atleast one of the following
conditions for every non-trivial function dependency X → Y.
1.X is a super key.
2.Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:
EMPLOYEE_DETAIL table:
Super key in the table above:
• {EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_NAME, EM
P_ZIP}....so on

• Candidate key: {EMP_ID}


• Non-prime attributes: In the given table, all attributes except
EMP_ID are non-prime.
• Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and
EMP_ZIP dependent on EMP_ID. The non-prime attributes
(EMP_STATE, EMP_CITY) transitively dependent on super
key(EMP_ID). It violates the rule of third normal form.

• That's why we need to move the EMP_CITY and EMP_STATE to the


new <EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMPLOYEE_ZIP table:
Fourth normal form (4NF)

• A relation will be in 4NF if it is in Boyce Codd normal form and has


no multi-valued dependency.
• For a dependency A → B, if for a single value of A, multiple values of
B exists, then the relation will be a multi-valued dependency.
Example
• 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.
• 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:
Fifth normal form (5NF)

• A relation is in 5NF if it is in 4NF and not contains any join


dependency and joining should be lossless.
• 5NF is satisfied when all the tables are broken into as many tables as
possible in order to avoid redundancy.
• 5NF is also known as Project-join normal form (PJ/NF).
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:
Boyce Codd normal form (BCNF)

• BCNF is the advance version of 3NF. It is stricter than 3NF.


• A table is in BCNF if every functional dependency X → Y, X is the
super key of 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:
In the above table Functional dependencies
are as follows:
1.EMP_ID → EMP_COUNTRY
2.EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}

• 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:
• Functional dependencies:
1.EMP_ID → EMP_COUNTRY
2.EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
• Candidate keys:
• For the first table: EMP_ID
• For the second table: EMP_DEPT
• For the third table: {EMP_ID, EMP_DEPT}
• Now, this is in BCNF because left side part of both the functional
dependencies is a key.

You might also like