This document discusses aggregate functions and nested subqueries in SQL. It defines aggregate functions as functions that take a collection of values as input and return a single value. It provides examples of the sum, avg, count, min, and max aggregate functions. It also discusses using the group by clause to apply aggregates to groups. Finally, it defines nested subqueries as subqueries nested within other queries, commonly used to perform set membership tests and comparisons.
This document discusses aggregate functions and nested subqueries in SQL. It defines aggregate functions as functions that take a collection of values as input and return a single value. It provides examples of the sum, avg, count, min, and max aggregate functions. It also discusses using the group by clause to apply aggregates to groups. Finally, it defines nested subqueries as subqueries nested within other queries, commonly used to perform set membership tests and comparisons.
This document discusses aggregate functions and nested subqueries in SQL. It defines aggregate functions as functions that take a collection of values as input and return a single value. It provides examples of the sum, avg, count, min, and max aggregate functions. It also discusses using the group by clause to apply aggregates to groups. Finally, it defines nested subqueries as subqueries nested within other queries, commonly used to perform set membership tests and comparisons.
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Review of Previous Class Topic
• Additional Basic Operations – Set Operations – Null
Values
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Topic Outcome
• Aggregate Functions – Nested Sub queries
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Aggregate Functions
Aggregate functions are functions that take a collection (a set
or multiset) of values as input and return a single value. SQL offers five built-in aggregate functions:
The input to sum and avg must be a
collection of numbers, but the other operators can operate on collections of nonnumeric data types, such as strings, as well.
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Basic Aggregation
Consider the query “Find the average salary of instructors in
the Computer Science department.” We write this query as follow
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Aggregation with Grouping
There are circumstances where we would like to apply the
aggregate function not only to a single set of tuples, but also to a group of sets of tuples; we specify this wish in SQL using the group by clause. The attribute or attributes given in the group by clause are used to form groups. Tuples with the same value on all attributes in the group by clause are placed in one group.
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Nested Subqueries
SQL provides a mechanism for nesting subqueries. A subquery
is a select-from where expression that is nested within another query. A common use of sub queries is to perform tests for set membership, make set comparisons, and determine set cardinality, by nesting subqueries in the where clause.
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Nested Subqueries
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Try it yourself
• Create table department with following fields
department name, building, budget.
• In this table department name should be
primary key
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Solution
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Try it yourself
• Create table course with following field course
id, title, department name, credits.
• In this table Course id should be primary key
• And also, department name should be foreign key.
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Solution
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Question
Expand DML __________________
a. Data-Definition Language b. Data-Manipulation Language c. Data Maintain Language d. All of these
What is a foreign key?
A. A foreign key is a primary key of a relation which is an attribute in another relation B. A foreign key is a superkey of a relation which is an attribute in more than one other relations C. A foreign key is an attribute of a relation that is a primary key of another relation.. D. A foreign key is the primary key of a relation that does not occur anywhere else in the schema
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Evaluation Questionnaires
1. What is SQL? 2. What is DML?
3. What is Primary Key?
4. What foreign Key?
5. Define Check Constraints.
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23
Next topic for Lect. 11
• Modification of Database – Join
Expressions – Views – Integrity Constraints
21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23