You are on page 1of 18

18csc303j -Database Management Systems

Nested Queries on sample exercise


Construction of Relational Table from the ER Diagram
S-14-15 SLO-1 & SLO-2 :
Nested SubQueries
 SQL provides a mechanism for the nesting of subqueries.
 A subquery is a select-from-where expression that is nested within
another query.
 A common use of subqueries is to perform tests for set membership, set
comparisons, and set cardinality.
S-14-15 SLO-1 & SLO-2 :
Nested SubQueries

✔Guidelines for Subqueries


 Enclose subqueries in parentheses.
 Place subqueries on the right side of the comparison operator.
 Do not add an ORDER BY clause to a subquery.
 Use single-row operators with single-row subqueries.
 Use multiple-row operators with multiple-row subqueries
✔Single-Row Subqueries
 Return only one row
 Use single-row comparison operators (ie; relational operators)
✔Multiple-Row Subqueries
 Return more than one row
 Use multiple-row comparison operators
S-14-15 SLO-1 & SLO-2 :
Nested SubQueries
Subqueries with the SELECT Statement
 SQL subqueries are most frequently used with the Select statement.
 Consider the EMPLOYEE table have the following records

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00
2 Stephan 26 Dubai 1500.00
3 David 27 Bangkok 2000.00
4 Alina 29 UK 6500.00
5 Kathrin 34 Bangalore 8500.00
6 Harry 42 China 4500.00
7 Jackson 25 Mizoram 10000.00
Select * From Employee Where Id In (Select Id From Employee Where Salary > 4500);  
 This would produce the following result

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00
Before 
2 Stephan 26 Dubai 1500.00
3 David 27 Bangkok 2000.00
4 Alina 29 UK 6500.00
5 Kathrin 34 Bangalore 8500.00
6 Harry 42 China 4500.00
7 Jackson 25 Mizoram 10000.00

ID NAME AGE ADDRESS SALARY

4 Alina 29 UK 6500.00
After  5 Kathrin 34 Bangalore 8500.00
7 Jackson 25 Mizoram 10000.00
S-14-15 SLO-1 & SLO-2 :
Nested SubQueries
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.

Insert Into Employee_bkp Select * From Employee Where Id In (Select Id From Employee);  

 Copy the complete EMPLOYEE table into the EMPLOYEE_BKP table.


S-14-15 SLO-1 & SLO-2 :
Nested SubQueries
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.
 Let's assume we have an EMPLOYEE_BKP table available which is
backup of 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.

Update Employee  Set Salary = Salary * 0.25  Where Age In Select Age From Customers_bkp
Where Age >= 29); 
Update Employee  Set Salary = Salary * 0.25  Where Age In
Select Age From Customers_bkp Where Age >= 29); 

Before  After 

ID NAME AGE ADDRESS SALARY ID NAME AGE ADDRESS SALARY


1 John 20 US 2000.00 1 John 20 US 2000.00
2 Stephan 26 Dubai 1500.00 2 Stephan 26 Dubai 1500.00
3 David 27 Bangkok 2000.00 3 David 27 Bangkok 2000.00
4 Alina 29 UK 6500.00 4 Alina 29 UK 1625.00
5 Kathrin 34 Bangalore 8500.00 5 Kathrin 34 Bangalore 2125.00
6 Harry 42 China 4500.00 6 Harry 42 China 1125.00
7 Jackson 25 Mizoram 10000.00 7 Jackson 25 Mizoram 10000.00
S-14-15 SLO-1 & SLO-2 :
Nested SubQueries
Subqueries with the DELET Statement
 The subquery of SQL can be used in conjunction with the Delete statement
just like any other statements mentioned above.
 Let's assume we have an EMPLOYEE_BKP table available which is backup of
EMPLOYEE table.
 The given example deletes the records from the EMPLOYEE table for all
EMPLOYEE whose AGE is greater than or equal to 29.

DELETE FROM EMPLOYEE WHERE AGE IN (SELECT AGE FROM EMPLOYEE_BKP 
 
      WHERE AGE >= 29 );  
S-14-15 SLO-1 & SLO-2 :
Nested SubQueries
DELETE FROM EMPLOYEE WHERE AGE IN (SELECT AGE FROM EMPLOYEE_BKP  
      WHERE AGE >= 29 );  

 This would impact three rows, and finally, the EMPLOYEE table would have the following records.

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

7 Jackson 25 Mizoram 10000.00


S-14-15 SLO-1 & SLO-2 :
construction of relational table from the ER diagram in DBMS
 Entity relationship diagram is the graphical representation of entities and
relationships among those entities in the database.
Example
S-14-15 SLO-1 & SLO-2 :
construction of relational table from the ER diagram in DBMS
Conversion of ER diagrams to tables
 Follow the steps given below for the conversion of the ER diagrams to
tables in the database management system (DBMS)

Step 1 − Conversion of strong entities


 For each strong entity create a separate table with the same name.
 Includes all attributes, if there is any composite attribute divided into
simple attributes and has to be included.
 Ignore multivalued attributes at this stage.
 Select the p key for the table.
S-14-15 SLO-1 & SLO-2 :
construction of relational table from the ER diagram in DBMS
Step 2 − Conversion of weak entity

 For each weak entity create a separate table with the same name.
 Include all attributes.
 Include the P key of a strong entity as foreign key is the weak entity.
 Declare the combination of foreign key and decimator attribute as P key
from the weak entity.
S-14-15 SLO-1 & SLO-2 :
construction of relational table from the ER diagram in DBMS
Step 3 − Conversion of one-to-one relationship

 For each one to one relation, say A and B modify either A side or B side to
include the P key of the other side as a foreign key.
 If A or B is having total participation, then that should be a modified table.
 If a relationship consists of attributes, include them also in the modified
table.
S-14-15 SLO-1 & SLO-2 :
construction of relational table from the ER diagram in DBMS
Step 4 − Conversion of one-to-many relationship
 For each one to many relationships, modify the M side to include the P key
of one side as a foreign key.
 If relationships consist of attributes, include them as well.

Step 5 − Conversion of many-many relationship


 For each many-many relationship, create a separate table including the P
key of M side and N side as foreign keys in the new table.
 Declare the combination of foreign keys as P for the new table.
 If relationships consist of attributes, include them also in the new table.
S-14-15 SLO-1 & SLO-2 :
construction of relational table from the ER diagram in DBMS
Step 6 − Conversion of multivalued attributes
 For each multivalued attribute create a separate table and include the P
key of the present table as foreign key.
 Declare the combination of foreign key and multivalued attribute as P
keys.

Step 7 − Conversion of n-ary relationship


 For each n-ary relationship create a separate table and include the P key
of all entities as foreign key.
 Declare the combination of foreign keys as P key.
S-14-15 SLO-1 & SLO-2 :
construction of relational table from the ER diagram in DBMS
Table
 After successful conversion, the result will be as follows 
THANK YOU

You might also like