You are on page 1of 16

#LifeKoKaroLift

AI-ML

1
Module Name: Database
Design and Introduction to
MySQL

Course : DBMS
Edit Master text styles
Lecture On : Database
Design and Introduction to
MySQL - Day - 4
Instructor :

2
Today’s Agenda

● Revision
● Nested Queries
● Views
● Venn Diagrams
● Inner and Outer Joins
● Left and Right Join
● Key Takeaways

Data Science Certification 3


Revision

In our previous sessions on the Introduction to MySQL, we covered the


following topics:

● Date-Time Functions
● Ordering
● Regular Expressions
Nested Queries

● Nested queries refer to a query inside another query. The subquery, which performs
an operation and uses its output to perform additional operations, is known as the
inner query.

● Let's take a look at some examples of how nested queries can be used in code
demonstrations. Below are some examples of nested queries:
Nested Queries

● In the example, the inner query groups customer IDs and sorts them by the
count of each customer in descending order.

● The customer ID of the most frequent customer is then returned.

● This value is used to find the customer whose Cust_id in the cust_dimen
table matches that value.
Nested Queries

● In this example, the subquery returns the Prod_id sorted by its count. It
should be noted that the count value is only displayed for explanatory
purposes and is not included in the actual subquery result.

● In the outer query, the


Product_Category and
Product_Sub_Categor
y for the Prod_id
values are retrieved
and limited to three.
Views

● As mentioned in nested queries, writing the same code multiple times can be a
tedious task.

● A view is a virtual table that can be created from one or multiple tables, and can be
treated as a normal table.

● This can help in avoiding redundant coding. Additionally, views can be used to
provide data security by hiding confidential data and only showing specific columns
to authorized users.

● There is no separate storage layer for a view, making it a useful tool in data
management.
Venn Diagrams
● Venn diagrams can be used to represent the relationship between multiple sets. The
following relationships can be illustrated using Venn diagrams for sets A and B.
Inner and Outer Joins

● There are two types of joins:


○ Inner join
○ Outer join.

● Inner join matches the rows between two tables based on the matching
condition.

● On the other hand, outer join returns a table containing all rows from both
tables, and if there is a missing match, it fills the null value in the table.
Inner and Outer Joins
● To understand the use cases of Inner and Outer Joins, refer to the diagram below.
Both tables, ORDER TABLE and CUSTOMER TABLE, are joined by matching the
CUST_ID attributes.

● The Inner join returns only the record for ORD1, as this value appears in both
tables. The Outer join, however, returns all rows from both tables and NULL values
wherever the data is missing.
Left and Right Joins
● To explain left and right joins, let's consider this example:

● LEFT JOIN preserves all the rows of the left table and attaches only those rows
from the right table whose values match the corresponding column of the left table.

● In this example, all the rows of the ORDER TABLE are preserved, and only the rows
of the CUSTOMER TABLE whose CUST_ID values match those in the ORDER
TABLE are attached.
Left and Right Joins
● To explain left and right joins, let's consider this example:

● RIGHT JOIN preserves all the rows of the right table and attaches only those rows
from the left table whose values match the corresponding column of the right table.

● In this example, all the rows of the CUSTOMER TABLE are preserved, and only
those rows from the ORDER TABLE whose CUST_ID values match those in the
CUSTOMER TABLE are attached. When the data is missing, the table shows a
NULL value.
Poll Question
Q. What is the purpose of a view in SQL?

A) To join two tables along all rows and return a table containing rows from both tables
B) To show the relationship between two or more sets using Venn diagrams
C) To store the result of an inner query and use it in multiple occasions
D) To diagrammatically display the relationship between two or more tables
Key Takeaway

● Nested Queries
● Views
● Venn Diagrams
● Inner and Outer Joins
● Left and Right Join

Data Science Certification 15


#LifeKoKaroLift

Thank You!

16

You might also like