Unit1
Unit1
Case Study: Design ER Model for any real time application and convert the same
into tables on paper
1.Google - Bigtable, MariaDB
2.Facebook - MariaDB, MySQL, HBase, Cassandra
3.Youtube - Bigtable, MariaDB
4.Yahoo - PostgreSQL, HBase, Cassandra, MongoDB
5.Amazon - DynamoDB, RDS/Aurora, Redshift
6.Twitter - MySQL
7.Linkedin - Venice
8.Netflix - NMDB PostgreSQL
9.MSN - Microsoft SQL Server
10.Wikipedia - MariaDB
In Gate Exam - 15-18% weightage
Introduction to Database Management Systems
Database:
The database is a collection of interrelated data which is used to retrieve, insert and delete the data
efficiently. It is also used to organize the data in the form of a table, schema, views, and reports, etc.
Using the database, you can easily retrieve, insert, and delete the information.
Database Management System :
● Database management system is a software which is used to manage the database. For example:
MySQL, Oracle, etc are a very popular commercial database which is used in different applications.
● DBMS provides an interface to perform various operations like database creation, storing data in it,
updating data, creating a table in the database and a lot more.It provides protection and security to
the database. In the case of multiple users, it also maintains data consistency.
Applications
● Banking: transactions
● Airlines: reservations, schedules
● Universities: registration, grades
● Sales: customers, products, purchases
● Online retailers: order tracking, customized recommendations
● Manufacturing: production, inventory, orders, supply chain
● Human resources: employee records, salaries, tax deductions
Purpose of Database Management System
In the early days, database applications were built directly on top of file systems
● Data isolation
In the rail route reservation framework, the information base is needed to store the record or
information of ticket appointments, status about train’s appearance, and flight. Additionally, if trains
get late, individuals become acquainted with it through the information base update.
We all utilization of online media sites to associate with companions and to impart our perspectives to the world.
Every day, many people group pursue these online media accounts like Pinterest, Facebook, Twitter, and Google
in addition to. By the utilization of the data set administration framework, all the data of clients are put away in the
information base and, we become ready to interface with others.
Databases are used to support internal operations of organizations and to underpin online interactions
with customers and suppliers.
Databases are used to hold administrative information and more specialized data, such as
engineering data or economic models.
Databases touch all aspects of our lives. Some of the major areas of application are as follows:
● Universities
● Banking
● Airlines
● Human resources
Data abstraction allow developers to keep complex data structures away from the users.
The developers achieve this by hiding the complex data structures through levels of
abstraction.
Links
https://www.youtube.com/watch?v=XZmGGAbHqa0
https://www.youtube.com/watch?v=0eKVizvYSUQ
https://www.youtube.com/watch?v=KG-mqHoXOXY
Data Abstraction
Data abstraction is hiding the complex data structure in order to simplify the user’s interface of
the system.
It is done because many of the users interacting with the database system are not that much computer
trained to understand the complex data structures of the database system.
Developers or database application programmers decide how to store data in the database.
It is complex to understand.
Logical or Conceptual Level
● It explains what data is stored in the database and how those data are related.
● It seeks to explain the complete or entire data by describing what tables should be
constructed and what the linkages between those tables should be.
Example: The database consists of information about a set of customers and accounts in a bank and the
Physical Data Independence – the ability to modify the physical schema without changing the logical schema
In general, the interfaces between the various levels and components should be well defined so that
pattern.
● Using the DDL statements, you can create the skeleton of the database.
● Data definition language is used to store the information of metadata like the number
of tables and schemas, their names, indexes, columns in each table, constraints, etc.
DML stands for Data Manipulation Language. It is used for accessing and manipulating data in
a database. It handles user requests.
Select, Insert, Update, Delete, Merge
Data Control Language (DCL)
DCL stands for Data Control Language. It is used to retrieve the stored or saved data.
Grant, Revoke
Transaction Control Language (TCL)
TCL is used to run the changes made by the DML statement. TCL can be grouped into a logical
transaction.
Commit, Rollback
SQL
● The most widely used commercial language
higher-level language
sent to a database
Database Applications
● Query processing
● Transaction manager
Storage Management
Storage manager is a program module that provides the interface between the
low-level data stored in the database and the application programs and
queries submitted to the system.
Interaction with the OS file manager Efficient storing, retrieving and updating of
data
● Authorization Manager: It ensures role-based access control, i.e,. checks whether the particular
person is privileged to perform the requested operation or not.
● Integrity Manager: It checks the integrity constraints when the database is modified.
● Transaction Manager: It controls concurrent access by performing the operations in a scheduled way
that it receives the transaction. Thus, it ensures that the database remains in the consistent state before
and after the execution of a transaction.
● File Manager: It manages the file space and the data structure used to represent information in the
database.
● Buffer Manager: It is responsible for cache memory and the transfer of data between the secondary
storage and main memory.
Query Processor:
It interprets the requests (queries) received from end user via an application program into instructions. It
also executes the user request which is received from the DML compiler.
Query Processor contains the following components –
● DML Compiler: It processes the DML statements into low level instruction (machine language),
so that they can be executed.
● DDL Interpreter: It processes the DDL statements into a set of table containing meta data (data
about data).
● Embedded DML Pre-compiler: It processes DML statements embedded in an application
program into procedural calls.
● Query Optimizer: It executes the instruction generated by DML Compiler.
Transaction Management (Consistent-Durable-Recovery-Concurrent)
Therefore, there are following four data models used for understanding the structure of the
database:
Relational Data Model:
This type of model designs the data in the form of rows and columns within a table.
Thus, a relational model uses tables for representing data and in-between relationships.
The relational data model is the widely used model which is primarily used by commercial data
processing applications.
Entity-Relationship Data Model:
An ER model is the logical representation of data as objects and relationships among them.
These objects are known as entities, and relationship is an association among these entities.
This model was designed by Peter Chen and published in 1976 papers.
A set of the same type of entities is known as an 'Entity set', and the set of the same type of
relationships is known as 'relationship set'.
Example
Object-based Data Model:
An extension of the ER model with notions of functions, encapsulation, and object identity, as
well.
This model supports a rich type system that includes structured and collection types.
Thus, in 1980s, various database systems following the object-oriented approach were
developed. Here, the objects are nothing but the data carrying its properties.
Semistructured Data Model:
This type of data model is different from the other three data models
The semistructured data model allows the data specifications at places where the individual data
items of the same type may have different attributes sets.
The Extensible Markup Language, also known as XML, is widely used for representing the
semistructured data.
Although XML was initially designed for including the markup information to the text document, it
gains importance because of its application in the exchange of data.
E-R Diagram
An Entity Relationship Diagram (ER Diagram) pictorially explains the
relationship between entities to be stored in a database.
The relationship described between student and course is many-to-many, as a course can be opted
by several students, and a student can opt for more than one course.
Many models were presented and discussed, but none were suitable.
The data structure diagrams offered by Charles Bachman also inspired his model.
Use ER Diagrams in DBMS?
● ER Diagram helps you conceptualize the database and lets you know which fields
need to be embedded for a particular entity
● ER Diagram gives a better understanding of the information to be stored in a
database
● It reduces complexity and allows database designers to build databases quickly
● It helps to describe elements using Entity-Relationship models
● It allows users to get a preview of the logical structure of the database
Symbols Used in ER Diagrams
For example, in a student study course, both the student and the course are entities.
Weak Entity
An entity that makes reliance over another entity is called a weak entity
In the example below, school is a strong entity because it has a primary key attribute - school number.
Unlike school, the classroom is a weak entity because it does not have any primary key and the room
number here acts only as a discriminator.
Attribute
An attribute exhibits the properties of an entity.
For example: For a student entity, the roll number can uniquely identify a student from a set of students.
Composite Attribute
An oval showcases the composite attribute, and the composite attribute oval is further connected with
other ovals.
Multivalued Attribute
Some attributes can possess over one value, those attributes are called multivalued attributes.
An attribute that can be derived from other attributes of the entity is known as a derived attribute.
In the example below, both the student and the course are entities, and study is the relationship between
them.
One-to-One Relationship
When a single element of an entity is associated with a single element of another entity, it is called a one-to-one
relationship.
For example, a student has only one identification card and an identification card is given to one person.
One-to-Many Relationship
When a single element of an entity is associated with more than one element of another entity, it is called a
one-to-many relationship
For example, a customer can place many orders, but an order cannot be placed by many customers.
Many-to-One Relationship
When more than one element of an entity is related to a single element of another entity, then it is called a
many-to-one relationship.
For example, students have to opt for a single course, but a course can have many students.
Many-to-Many Relationship
When more than one element of an entity is associated with more than one element of another entity, this
is called a many-to-many relationship.
For example, you can assign an employee to many projects and a project can have many employees.
How to Draw an ER Diagram?
● First, identify all the Entities. Embed all the entities in a rectangle and label them properly.
● Identify relationships between entities and connect them using a diamond in the middle,
● Make sure your ER Diagram supports all the data provided to design the database.
You need to study the files, forms, reports, data currently maintained by the organization to
identify attributes.
You can also conduct interviews with various stakeholders to identify entities. Initially, it’s
important to identify the attributes without mapping them to a particular entity.
Once, you have a list of Attributes, you need to map them to the identified entities. Ensure an
attribute is to be paired with exactly one entity.
If you think an attribute should belong to more than one entity, use a modifier to make it unique.
Once the mapping is done, identify the primary Keys. If a unique key is not readily available,
create one.
For Course Entity, attributes could be Duration, Credits, Assignments, etc. For the sake of ease
we have considered just one attribute.
Step 5) Create the ERD Diagram
Here are some best practice or example for Developing Effective ER Diagrams.
For example, ID is used as a key in the Student table because it is unique for each student.
In the PERSON table, passport_number, license_number, SSN are keys since they are unique
for each person.
Types of keys:
1. Primary key
○ It is the first key used to identify one and only one instance of an entity uniquely.
An entity can contain multiple keys, as we saw in the PERSON table. The key
which is most suitable from those lists becomes a primary key.
○ In the EMPLOYEE table, ID can be the primary key since it is unique for each
employee. In the EMPLOYEE table, we can even select License_Number and
Passport_Number as primary keys since they are also unique.
○ For each entity, the primary key selection is based on requirements and
developers.
2. Candidate key
For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of the
attributes, like SSN, Passport_Number, License_Number, etc., are considered a candidate
key.
Super Key
Super key is an attribute set that can uniquely identify a tuple. A super key is a superset of a
candidate key.
EMPLOYEE table, for(EMPLOEE_ID, EMPLOYEE_NAME), the name of two employees can be the same, but their EMPLYEE_ID can't be
the same. Hence, this combination can also be a key.
There may be one or more attributes or a combination of attributes that uniquely identify each tuple in a relation.
These attributes or combinations of the attributes are called the candidate keys. One key is chosen as the primary
key from these candidate keys, and the remaining candidate key, if it exists, is termed the alternate key.
In other words, the total number of the alternate keys is the total number of candidate keys minus the primary key.
The alternate key may or may not exist.
If there is only one candidate key in a relation, it does not have an alternate key.
For example, employee relation has two attributes, Employee_Id and PAN_No, that act as candidate keys. In this
relation, Employee_Id is chosen as the primary key, so the other candidate key, PAN_No, acts as the Alternate key.
Composite key
Whenever a primary key consists of more than one attribute, it is known as a composite key.
This key is also known as Concatenated Key.
in employee relations, we assume that an employee may be assigned multiple roles, and an employee
may work on multiple projects simultaneously.
So the primary key will be composed of all three attributes, namely Emp_ID, Emp_role, and Proj_ID in
combination.
So these attributes act as a composite key since the primary key comprises more than one attribute.
Artificial key
The key created using arbitrarily assigned data are known as artificial keys. These keys are
created when a primary key is large and complex and has no relationship with many other
relations. The data values of the artificial keys are usually numbered in a serial order.
● Generalization
● Specialization
● Aggregation
Generalization
○ Generalization is like a bottom-up approach in which two or more entities of lower level
combine to form a higher level entity if they have some attributes in common.
○ In generalization, an entity of a higher level can also combine with the entities of the lower level
to form a further higher level entity.
○ Generalization is more like subclass and superclass system, but the only difference is the
approach. Generalization uses the bottom-up approach.
○ In generalization, entities are combined to form a more generalized entity, i.e., subclasses are
combined to make a superclass.
Faculty and Student entities can be generalized and create a higher level entity
Person.
Specialization
In the database, every entity set or relationship set can be represented in tabular form.
Rules
○ Entity type becomes a table.
a. In the given ER diagram, LECTURE, STUDENT, SUBJECT and COURSE forms individual tables.
○ All single-valued attribute becomes a column for the table.
a. In the STUDENT entity, STUDENT_NAME and STUDENT_ID form the column of STUDENT table.
Similarly, COURSE_NAME and COURSE_ID form the column of COURSE table and so on.
○ A key attribute of the entity type represented by the primary key.
a. In the given ER diagram, COURSE_ID, STUDENT_ID, SUBJECT_ID, and LECTURE_ID are the key attribute
of the entity.
○ The multivalued attribute is represented by a separate table.
a. In the student table, a hobby is a multivalued attribute. So it is not possible to represent multiple values
in a single column of STUDENT table. Hence we create a table STUD_HOBBY with column name
STUDENT_ID and HOBBY. Using both the column, we create a composite key.
○ Composite attribute represented by components.
a. In the given ER diagram, student address is a composite attribute. It contains CITY, PIN, DOOR#,
STREET, and STATE. In the STUDENT table, these attributes can merge as an individual column.
○ Derived attributes are not considered in the table.
a. In the STUDENT table, Age is the derived attribute. It can be calculated at any point of time by
calculating the difference between current date and Date of Birth.
Table Structure
Case Study
Suppose you are given the following requirements for a simple database for the National Hockey League (NHL):
each player has a name, a position (such as left wing or goalie), a skill level, and a set of injury records,
a game is played between two teams (referred to as host_team and guest_team) and has a date (such as May 11th,
1999) and a score (such as 4 to 2).
It is used for managing data in relational database management system which stores data in the
form of tables and relationship between data is also stored in the form of tables.
SQL works with database programs like DB2, MySQL, PostgreSQL, Oracle, SQLite, SQL Server,
Sybase, MS Access and much more.
There are many different versions of the SQL language, but to be in compliance with the ANSI
standard, they support the major keyword such as SELECT, UPDATE, DELETE, INSERT, WHERE,
and others.
History of SQL
Easy to learn
SQL is an extremely practical and user-friendly language. Even if you have no prior experience with technology,
you can learn the basics of the language. SQL has a syntax that is remarkably close to English, resulting in a
smooth learning curve.
SQL supports a wide variety of commands such as DDL (Data Definition Language) commands like CREATE,
DROP, ALTER; DML (Data Manipulation Language) commands like INSERT, UPDATE, DELETE; DCL (Data
Control Language) commands like GRANT, REVOKE; TCL (Transaction Control Language) commands like
COMMIT, ROLLBACK, and DQL (Data Query Language) commands like SELECT.
Stored Procedures
A stored procedure is a piece of SQL code that you can save and reuse any number of times.
Stored Procedures are used to carry out one or more DML operations on a database. It’s just a
collection of SQL statements that take some input in the form of arguments, perform some work,
and may or may not return a result
Portable Language
DQL provides portability of data definitions and applications. That is, applications can be moved
from one machine to another.
Joins
SQL supports join which is a command that joins two sets of data together (i.e. two or more
tables). INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN are the joins supported by SQL.
Union
UNION command can be used to join the results of two or more SELECT statements in SQL.
In large database systems, SQL provides performance programming capabilities for highly
transactional heavy workloads.
SQL is a scalable and flexible database. It’s simple to build new tables, and tables that have
already been created or those which are no longer in use can be dropped or deleted from a
database. SQL is capable of handling big data sets and several transactions.
Transactions
Security
SQL allows setting permissions in views, tables, and procedures. This ensures the safety of
data in sensitive databases. Also, constraints can be specified in SQL. These are rules that
limit the types of data that can be entered into a table column.
SQL Data Types
SQL Commands
SQL Commands - DDL - Create
The SELECT query is used when we want to retrieve the data from all the
columns of the table.
Now, if we want to access all the data stored in the table, we will make use of the
SELECT command.
WHERE condition;
Example
UPDATE student_details
SET MARKS = 92
WHERE ROLL_NO = 4;
UPDATE student_details
WHERE ROLL_NO = 4
Delete
DELETE FROM table_name;
SELECT expressions
FROM tables
WHERE conditions;
All Fields
SELECT * FROM customers;
Example
Queries
SELECT * FROM Students
WHERE stu_fees < 3500;
SELECT SUM(stu_fees), stu_class
FROM Students
GROUP BY stu_class;
GROUP BY clause is used to group the rows of the students based on the student class column.
The total fees in an individual class are summed up, and the grouped rows are displayed in the table.
SELECT * FROM Students ORDER BY stu_fees;
The ORDER BY clause is applied to the column stu_fees to sort the final result based on the fees of
the students.
SELECT * FROM Students GROUP BY stu_id HAVING
stu_age < 17;
The HAVING clause is used to fetch the records of students under 17. Also, The GROUP BY clause is
mandatory if you are using the HAVING clause in SQL.
SQL Join
Inner Join
An INNER JOIN returns rows when the join condition is satisfied in both tables.
In other words, it returns only those records that match the join condition in both
tables.
This is the most common type of SQL JOIN. It’s also the default when you don’t
specify the type of JOIN.
Example : Inner Join
color and shoes are table shown below:
The color table stores an ID number and a name for each color. The shoes table stores
an ID number for each pair of shoes, the shoe size, and an ID number that refers to a
color in the color table.
INNER JOIN joins records from the shoes table with records from the color table
through the color_id column from the shoes table. The values in this column are
the same as those in the id column of the color table, so it produces the intended
result set.
SELECT * FROM shoes
The SELECT statement takes all records from the table listed after the FROM clause – in this
case, the shoes table.
Then there is an INNER JOIN with the name of the table we want to match records with (i.e. the
color table).
The ON predicate states the matching condition, which records from both tables must have.
Here the condition is that the id field from the color table and the color_id field from the
shoes table must have matching values.
We can see the color of each pair of shoes, thanks to the INNER JOIN.
But notice that the shoes record with NULL in the color_id column is not shown: it has
not matched any of the records in the color table.
INNER JOIN on Multiple Tables
Query
SELECT * FROM shoes
only shoes with non-NULL records in the color_id and material_id columns are shown in the result set.
Outer Join
An OUTER JOIN returns all the rows from one table and some or all of the rows from another table.
● LEFT OUTER JOIN returns every record in the left table and all matching records from the
right table. If there’s no match found, a NULL is shown next to the unmatched record.
● RIGHT OUTER JOIN returns every record in the right table and all matching records from the
left table. If there’s no match found, a NULL is shown next to the unmatched record.
● FULL OUTER JOIN returns all records from both tables. All unmatched records are paired
with NULLs.
The “shirt” table only has one field, “color_shirt ”
Just imagine them as two parts of an outfit: the color of your pants and the color of your
shirt. The idea is to find shirts and pants with identical colors.
The left table is the first table listed and is found after the FROM clause.
The right table is the second table listed and is found after the JOIN clause.
You can usually omit the OUTER keyword in any OUTER JOIN — FULL JOIN , LEFT
JOIN , and RIGHT JOIN will work just as well in many databases
The LEFT OUTER JOIN retrieves all records from the first (left) table and matches
them to records from the second (right) table.
Any non-matching records from the left table are also selected, but with NULL
values where the right table records would be.
SELECT color_shirt, color_pants
FROM shirt
This condition is that the values in the “shirt”.“color_shirt” field and those in the
“pants”.“color_pants” field must match.
If there is no match, records from the “shirt” table will be shown, but a NULL value
is set where the matching “pants” record would be.
The matching green and blue outfits are
together.
The yellow shirt has no pants because
the “pants” table does not have any fields
with a “yellow” value.
The RIGHT OUTER JOIN works like the LEFT JOIN , but with one major difference:
it selects all records from the right table (in this case, “pants”).
The records from the left table (“shirt”) will only be shown if they match.
It shows all records from both tables. If possible, it will match the records;
if not, a NULL will be shown where the matching record would be.
INCREMENT BY increment_value
CYCLE|NOCYCLE ;
sequence_name: Name of the sequence.
initial_value: starting value from where the sequence starts.
Initial_value should be greater than or equal
to minimum value and less than equal to maximum value.
increment_value: Value by which sequence will increment itself.
Increment_value can be positive or negative.
minimum_value: Minimum value of the sequence.
maximum_value: Maximum value of the sequence.
cycle: When sequence reaches its set_limit
it starts from beginning.
nocycle: An exception will be thrown
if sequence exceeds its max_value.
sequence query creating sequence in ascending order
Example
CREATE SEQUENCE sequence_1
start with 1
increment by 1 query will create a sequence named sequence_1.Sequence will start from 1
and will be incremented by 1 having maximum value 100. Sequence will
minvalue 0 repeat itself from start value after exceeding 100.
maxvalue 100
cycle;
sequence query creating sequence in descending order.
CREATE SEQUENCE sequence_2
start with 100
increment by -1
minvalue 1
maxvalue 100
cycle;
Above query will create a sequence named sequence_2.Sequence will start from 100 and
should be less than or equal to maximum value and will be incremented by -1 having
minimum value 1.
create a table named students with columns as id and name.
CREATE TABLE students
(
ID number(10),
NAME char(20)
);
It can reduce disk I/O(input/output) by using a rapid path access method to locate data
quickly.
An index helps to speed up select queries and where clauses, but it slows down data input,
with the update and the insert statements.
Indexes can be created or dropped with no effect on the data. In this article, we will see how
to create, delete, and uses the INDEX in the database.
If you want to reference all pages in a book that discusses a certain topic, you first refer to
the index, which lists all the topics alphabetically and is then referred to one or more specific
page numbers.
Creating Index:
ON TABLE column;
where the index is the name given to that index and TABLE is the name of the table on which that
index is created and column is the name of that column for which it is applied.
For multiple columns:
Syntax:
CREATE INDEX index
ON TABLE column;
When should indexes be created:
ON TableName REBUILD;
Confirming Indexes: You can check the different indexes present in a particular table given
by the user or the server itself and their uniqueness.
Syntax:
select * from USER_INDEXES;
It will show you all the indexes present in the server, in which you can locate your own tables
too.
Renaming an index: You can use the system-stored procedure sp_rename to rename any
index in the database.
Syntax:
EXEC sp_rename
index_name,
new_index_name,
N'INDEX';
Views
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a
real table in the database.
We can create a view by selecting fields from one or more tables present in the database.
A View can either have all the rows of a table or specific rows based on certain condition.
CREATING VIEWS
We can create View using CREATE VIEW statement. A View can be created from a single table or
multiple tables. Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
FROM StudentDetails
To see the data in the View, we can query the view in the same manner as we query a
table.
1. Union
2. UnionAll
3. Intersect
4. Minus
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.
UNION
UNION
Syntax:
Syntax
SELECT column_name FROM table1
INTERSECT
INTERSECT
○ 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:
MINUS
MINUS
○ 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.
COUNT(*)
or
10 WHERE RATE>=20;
Output: 10
Output : 7
Example: COUNT() with GROUP BY
Example: COUNT() with DISTINCT
SELECT COMPANY, COUNT(*)
SELECT COUNT(DISTINCT COMPANY)
FROM PRODUCT_MAST
FROM PRODUCT_MAST;
GROUP BY COMPANY;
Output : 3
Output:
Com1 5
Com2 3
Com3 2
COUNT() with HAVING
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>2;
Output :
Com1 5
Com2 3
SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields only.
Syntax
SUM()
or
Example: SUM()
SELECT SUM(COST)
FROM PRODUCT_MAST;
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
AVG()
or
Example:
SELECT AVG(COST)
FROM PRODUCT_MAST;
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
MAX()
or
Example:
SELECT MAX(RATE)
FROM PRODUCT_MAST;
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
MIN()
or
Example:
SELECT MIN(RATE)
FROM PRODUCT_MAST;
Nested Queries
A nested query in SQL contains a query inside another query. The outer query will use the
result of the inner query. For instance, a nested query can have two SELECT statements,
one on the inner query and the other on the outer query.
What are the Types of Nested Queries in SQL?
Nested queries in SQL can be classified into two different types:
In independent nested queries, the execution order is from the innermost query to the outer query. An outer
query won't be executed until its inner query completes its execution. The outer query uses the result of the
inner query. Operators such as IN, NOT IN, ALL, and ANY are used to write independent nested queries.
● The IN operator checks if a column value in the outer query's result is present in the inner
query's result. The final result will have rows that satisfy the IN condition.
● The NOT IN operator checks if a column value in the outer query's result is not present in the
inner query's result. The final result will have rows that satisfy the NOT IN condition.
● The ALL operator compares a value of the outer query's result with all the values of the inner
query's result and returns the row if it matches all the values.
● The ANY operator compares a value of the outer query's result with all the inner query's result
values and returns the row if there is a match with any value.
Co-related Nested Queries
In co-related nested queries, the inner query uses the values from the outer query to execute the
inner query for every row processed by the outer query. The co-related nested queries run slowly
because the inner query is executed for every row of the outer query's result.
How to Write Nested Query in SQL?
We can write a nested query in SQL by nesting a SELECT statement within another SELECT
statement. The outer SELECT statement uses the result of the inner SELECT statement for
processing.
The SELECT query inside the brackets () is the inner query, and the SELECT query outside the brackets is
the outer query. The outer query uses the result of the inner query.
Examples of Nested Query in SQL
We will use the Employees and Awards table below to understand independent and co-related
nested queries. We will be using Oracle SQL syntax in our queries.
Example 1: IN
Select all employees who won an award.
);
The developer with id 5 earns (50000) more than all the managers: 2 (10000) and 4 (40000)
Example 4: ANY
Select all Developers who earn more than any Manager
SELECT * FROM employees
);
The developers with id 3 and 5 earn more than any manager:
● The developer with id 3 earns (30000) more than the manager with id 2 (10000)
● The developer with id 5 earns (50000) more than the managers with id 2 (10000)
and 4 (40000)
Co-related Nested Queries
Select all employees whose salary is above the average salary of employees in their role.
SELECT AVG(salary)
);
The manager with id 4 earns more than the average salary of all managers (25000),
and the developer with id 5 earns more than the average salary of all developers
(30000). The inner query is executed for all rows fetched by the outer query. The
inner query uses the role value (emp1.role) of every outer query's row (emp1.role =
emp2.role).
We can find the average salary of managers and developers using the below query: