SlideShare a Scribd company logo
1 of 17
Download to read offline
Chapter 9
MORE ON DATABASE AND SQL
Single Row functions - Single row functions are the one who work on single
row and return one output per row. For example, length and case conversion
functions are single row functions.
Multiple Row functions - Multiple row functions work upon group of rows
and return one result for the complete set of rows. They are also known as
Group Functions.
AGGREGATE (GROUP) FUNCTIONS
Group functions or Aggregate functions work upon groups of rows, rather
than on single rows. That is why, these function are also called multiple row
functions.
1. AVG – This function computes the average of given data.
Example – Calculate average salary of all employees listed in table empl
Q- to find the average salary in empl for job of salesman
2. COUNT – This function is used to counts the number of values in a
column. While counting it ignore the NULL values.
Count(*) – Returns the number of rows satisfying the condition, if any in
the table.
Count (Column Name) – Return Non-NULL values in the column.
Example 1 – Count number of records in table empl.
Example 2 – Count number of jobs in table empl.
Example 3 – Count number of commission in table empl.
Note: Ignore the null values while counting.
Example 4 – How many distinct jobs are listed in table empl?
3. MAX – This function returns the maximum value from a given column or
expression.
Example 1 – Display maximum salary from empl.
Q1 –
Display maximum salary in empl for job salesman.
Q2- Write the output of following query:
4. MIN - This function returns the minimum value from a given column or
expression.
Example 1 – Display the joining date of seniormost employee.
Q 1. Display minimum commission from empl.
5. SUM – This function returns the sum of value in given column or
expression.
Example 1 – Display total salary of all employees listed in table empl.
Q1. Display total salary for job salesman.
Aggregate functions and NULL values:
All aggregate functions ignore NULL values. The * is the only argument that
includes NULLs when it is used with COUNT function.
Example 1 –
Indicate that there are 5 records in the table empl.
Example 2 –
Output indicates that there are 3 values in the comm Column of empl table.
This feature of aggregate functions ensures that NULLs don’t play any role
in actual calculations. For example
The average comm has been calculated for all the 3 non NULL values.
GROUPING RESULT – GROUP BY
The GROUP BY clause combines all those records that have the same values
in a particular field or a group of fields. This statement divides the table into
groups.
Example 1 : Calculate the number of employees in each grade form table
empl
Example 2 :Display total salary department wise
Q1- Count total number of employees in each department.
Grouping on Multiple Columns
With GROUP BY we can create groups within groups. Such type of grouping is
called Nested Grouping.
Example – 1 : Count the number of employees in each department jobwise.
HAVING Clause : HAVING clause is used to specify condition with GROUP BY
clause . HAVING can include aggregate functions also.
Example 1: Count the number of employees belonging to department
number 20.
Example 2: To display the jobs where the number of employees is
less than 3.
Example 3 : To calculate the average commission and total commission
department wise where average commission is more than 500.
.
Example 4 : We can use an aggregate function in the HAVING even if it is not
in the SELECT list.
Example 5 : We can also use IN or BETWEEN operators with HAVING.
JOINS
A join is a query that combines rows from two or more tables.
In a join-query , more than one table are listed in FROM clause. The function
of combine data from multiple tables is called joining.
For example :
SELECT * FORM EMPL, DEPT;
Cartesian Product or Cross Join of Tables :
Def - The Cartesian Product (or Cross Join) is a very basic type of join that
simply matches each row from one table to every row from another table.
If two tables contain 3 rows and 2 rows respectively, then their Cartesian
product will contain 6 = (3 X 2) rows.
The number of columns in the Cartesian product is the sum of the number of
columns in both the tables.
Consider following tables:
Example of Cartesian Product:
SELECT * FROM order_table, product;
Equi Join of tables:
Def - The Join, in which column are compared for equality, is called Equi –
Join.
In Equi-join, all the columns from joining table appear in the output even if
they are identical.
Example 1 : Write query to find out the “Product Name” corresponding to
each Order placed.
SELECT * FROM Order_table, Product
WHERE p_code = code;
Internally the Cartesian product of the table is made. Then based on the
specified condition the meaningful data is extracted from this Cartesian
product and displayed.
Qualified Names
<table name>.<field name> is called qualified name. Qualified field
names are very useful in identifying a filed if the two joining table have
fields with same name.
Example 2 : Write a Query to display supplier name and address
corresponding to each order placed.
Example 3- Write a query to produce the following output:
Ans –
Select Order_no,Product.name “Product”,Supplier.Name “Supplier”
From order_table, Product, Supplier
WHERE order_table.Sup_Code = Supplier.Sup_Code
AND P_Code = Code;
Foreign Key: A non-key attribute, whose values are derived from the
primary key of some other table. A foreign key in a table is used to ensure
referential integrity and to get Equi-Join of two tables.
Code is the Primary Key of Product table. In the order_table, P_Code is a
Foreign Key.
Referential Integrity: This property of a relational database which ensures
that no entry in a foreign key column of a table can be made unless it
matches a primary key value in the corresponding related table is called
Referential Integrity
Union
The UNION operator is used to combine the result-set of two or more SELECT
statements.
Result-set can be combined only if each SELECT statement within the UNION
must have the same number of columns. The columns must also have similar
data types. Also, the columns in each SELECT statement must be in the same
order.
Syntax :
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Suppose a Database contains two tables: XIISC and XIICOM. If it is required
to produce a combined list of all the students, then it can be done as follows:
SELECT rollno,name,class from xiisc
UNION
SELECT rollno,name,class from xiicom;
SQL Constraints
Def – A Constraint is a condition or check applicable on a field or set of fields.
Types of constraint
1. Not Null – Makes sure that NULLs are not accepted in the specified
column. Example
CREATE TABLE CUSTOMER
( SID INTEGER NOT NULL,
FIRST_NAME VARCHAR(30) NOT NULL,
LAST_NAME VARCHAR(30) );
2. DEFAULT Constraint – The DEFAULT constraint provides a default value
to a column when the INSERT INTO statement does not provide a specific
value. Example
CREATE TABLE CUSTOMER
( SID INTEGER
FIRST_NAME VARCHAR(30) ,
LAST_NAME VARCHAR(30)
SCORE INTEGER DEFAULT 80);
and execute the following statement,
INSERT INTO CUSTOMER
(SID,FIRST_NAME,LAST_NAME)
VALUES (1, ’AJAY’, ’SHARMA’);
Then output will be
SID FIRST_NAME LAST_NAME SCORE
1 AJAY SHARMA 80
3. UNIQUE Constraint – Makes sure that duplicate values in the specified
column are not accepted. Example
4. PRIMARY KEY Constraint –
1. Defining Primary Key through Create Table Command
2. Composite Primary Key – When multiple fields are used as a primary
key, they are called a composite primary key.
CREATE TABLE CUSTOMER
( BRANCH INTEGER,
SID INTEGER ,
FIRST_NAME VARCHAR(30) ,
LAST_NAME VARCHAR(30)
PRIMARY KEY (BRANCH, SID));
1. Defining Primary Key through Alter Table Command
ALTER TABLE CUSTOMER
ADD PRIMARY KEY (SID);

More Related Content

What's hot

Functional dependencies in Database Management System
Functional dependencies in Database Management SystemFunctional dependencies in Database Management System
Functional dependencies in Database Management SystemKevin Jadiya
 
Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Abou Bakr Ashraf
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)Sabana Maharjan
 
Functional Dependency
Functional DependencyFunctional Dependency
Functional DependencyAlaanoor94
 
Dbms interview questions
Dbms interview questionsDbms interview questions
Dbms interview questionsambika93
 
MS excel - match function
MS excel - match functionMS excel - match function
MS excel - match functionVincent Segondo
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functionsfarwa waqar
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of joinraj upadhyay
 
Database Keys & Relationship
Database Keys & RelationshipDatabase Keys & Relationship
Database Keys & RelationshipBellal Hossain
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL CommandsShrija Madhu
 
Relational Algebra-Database Systems
Relational Algebra-Database SystemsRelational Algebra-Database Systems
Relational Algebra-Database Systemsjakodongo
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2Mohd Tousif
 

What's hot (20)

Sql and Sql commands
Sql and Sql commandsSql and Sql commands
Sql and Sql commands
 
Functional dependencies in Database Management System
Functional dependencies in Database Management SystemFunctional dependencies in Database Management System
Functional dependencies in Database Management System
 
Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Visula C# Programming Lecture 1
Visula C# Programming Lecture 1
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
Functional Dependency
Functional DependencyFunctional Dependency
Functional Dependency
 
Array in c++
Array in c++Array in c++
Array in c++
 
Dbms interview questions
Dbms interview questionsDbms interview questions
Dbms interview questions
 
MS excel - match function
MS excel - match functionMS excel - match function
MS excel - match function
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
SQL
SQLSQL
SQL
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
 
Database Keys & Relationship
Database Keys & RelationshipDatabase Keys & Relationship
Database Keys & Relationship
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Relational Algebra-Database Systems
Relational Algebra-Database SystemsRelational Algebra-Database Systems
Relational Algebra-Database Systems
 
Array
ArrayArray
Array
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
 
Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
 

Similar to Chapter9 more on database and sql

Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxuzmasulthana3
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)Huda Alameen
 
Sql Queries
Sql QueriesSql Queries
Sql Querieswebicon
 
Simple Spreadsheet Tips
Simple Spreadsheet TipsSimple Spreadsheet Tips
Simple Spreadsheet TipsInside Access
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
Manual for Troubleshooting Formulas & Functions in Excel
Manual for Troubleshooting Formulas & Functions in ExcelManual for Troubleshooting Formulas & Functions in Excel
Manual for Troubleshooting Formulas & Functions in ExcelChristopher Ward
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Techglyphs
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functionsNitesh Singh
 
An Introduction To Array Functions
An Introduction To Array FunctionsAn Introduction To Array Functions
An Introduction To Array Functionsposterro
 
Assignment 4
Assignment 4Assignment 4
Assignment 4SneaK3
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQueryAbhishek590097
 
SQL200.2 Module 2
SQL200.2 Module 2SQL200.2 Module 2
SQL200.2 Module 2Dan D'Urso
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL UsedTheVerse1
 

Similar to Chapter9 more on database and sql (20)

Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
Unit 2 web technologies
Unit 2 web technologiesUnit 2 web technologies
Unit 2 web technologies
 
Simple Spreadsheet Tips
Simple Spreadsheet TipsSimple Spreadsheet Tips
Simple Spreadsheet Tips
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Manual for Troubleshooting Formulas & Functions in Excel
Manual for Troubleshooting Formulas & Functions in ExcelManual for Troubleshooting Formulas & Functions in Excel
Manual for Troubleshooting Formulas & Functions in Excel
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
An Introduction To Array Functions
An Introduction To Array FunctionsAn Introduction To Array Functions
An Introduction To Array Functions
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
Assignment 4
Assignment 4Assignment 4
Assignment 4
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
 
SQL200.2 Module 2
SQL200.2 Module 2SQL200.2 Module 2
SQL200.2 Module 2
 
Lab4 join - all types listed
Lab4   join - all types listedLab4   join - all types listed
Lab4 join - all types listed
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
 
WEB TECHNOLOGIES Unit 2
WEB TECHNOLOGIES Unit 2WEB TECHNOLOGIES Unit 2
WEB TECHNOLOGIES Unit 2
 

Recently uploaded

How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 

Recently uploaded (20)

How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 

Chapter9 more on database and sql

  • 1. Chapter 9 MORE ON DATABASE AND SQL Single Row functions - Single row functions are the one who work on single row and return one output per row. For example, length and case conversion functions are single row functions. Multiple Row functions - Multiple row functions work upon group of rows and return one result for the complete set of rows. They are also known as Group Functions. AGGREGATE (GROUP) FUNCTIONS Group functions or Aggregate functions work upon groups of rows, rather than on single rows. That is why, these function are also called multiple row functions. 1. AVG – This function computes the average of given data. Example – Calculate average salary of all employees listed in table empl Q- to find the average salary in empl for job of salesman 2. COUNT – This function is used to counts the number of values in a column. While counting it ignore the NULL values. Count(*) – Returns the number of rows satisfying the condition, if any in the table. Count (Column Name) – Return Non-NULL values in the column. Example 1 – Count number of records in table empl.
  • 2. Example 2 – Count number of jobs in table empl. Example 3 – Count number of commission in table empl. Note: Ignore the null values while counting. Example 4 – How many distinct jobs are listed in table empl?
  • 3. 3. MAX – This function returns the maximum value from a given column or expression. Example 1 – Display maximum salary from empl. Q1 – Display maximum salary in empl for job salesman. Q2- Write the output of following query: 4. MIN - This function returns the minimum value from a given column or expression. Example 1 – Display the joining date of seniormost employee. Q 1. Display minimum commission from empl. 5. SUM – This function returns the sum of value in given column or expression. Example 1 – Display total salary of all employees listed in table empl. Q1. Display total salary for job salesman.
  • 4. Aggregate functions and NULL values: All aggregate functions ignore NULL values. The * is the only argument that includes NULLs when it is used with COUNT function. Example 1 – Indicate that there are 5 records in the table empl. Example 2 – Output indicates that there are 3 values in the comm Column of empl table. This feature of aggregate functions ensures that NULLs don’t play any role in actual calculations. For example The average comm has been calculated for all the 3 non NULL values.
  • 5. GROUPING RESULT – GROUP BY The GROUP BY clause combines all those records that have the same values in a particular field or a group of fields. This statement divides the table into groups. Example 1 : Calculate the number of employees in each grade form table empl Example 2 :Display total salary department wise Q1- Count total number of employees in each department. Grouping on Multiple Columns With GROUP BY we can create groups within groups. Such type of grouping is called Nested Grouping. Example – 1 : Count the number of employees in each department jobwise.
  • 6. HAVING Clause : HAVING clause is used to specify condition with GROUP BY clause . HAVING can include aggregate functions also. Example 1: Count the number of employees belonging to department number 20. Example 2: To display the jobs where the number of employees is less than 3.
  • 7. Example 3 : To calculate the average commission and total commission department wise where average commission is more than 500. . Example 4 : We can use an aggregate function in the HAVING even if it is not in the SELECT list. Example 5 : We can also use IN or BETWEEN operators with HAVING.
  • 8. JOINS A join is a query that combines rows from two or more tables. In a join-query , more than one table are listed in FROM clause. The function of combine data from multiple tables is called joining. For example : SELECT * FORM EMPL, DEPT; Cartesian Product or Cross Join of Tables : Def - The Cartesian Product (or Cross Join) is a very basic type of join that simply matches each row from one table to every row from another table. If two tables contain 3 rows and 2 rows respectively, then their Cartesian product will contain 6 = (3 X 2) rows. The number of columns in the Cartesian product is the sum of the number of columns in both the tables. Consider following tables:
  • 9. Example of Cartesian Product: SELECT * FROM order_table, product;
  • 10. Equi Join of tables: Def - The Join, in which column are compared for equality, is called Equi – Join. In Equi-join, all the columns from joining table appear in the output even if they are identical. Example 1 : Write query to find out the “Product Name” corresponding to each Order placed. SELECT * FROM Order_table, Product WHERE p_code = code;
  • 11. Internally the Cartesian product of the table is made. Then based on the specified condition the meaningful data is extracted from this Cartesian product and displayed. Qualified Names <table name>.<field name> is called qualified name. Qualified field names are very useful in identifying a filed if the two joining table have fields with same name.
  • 12. Example 2 : Write a Query to display supplier name and address corresponding to each order placed.
  • 13. Example 3- Write a query to produce the following output: Ans – Select Order_no,Product.name “Product”,Supplier.Name “Supplier” From order_table, Product, Supplier WHERE order_table.Sup_Code = Supplier.Sup_Code AND P_Code = Code;
  • 14. Foreign Key: A non-key attribute, whose values are derived from the primary key of some other table. A foreign key in a table is used to ensure referential integrity and to get Equi-Join of two tables. Code is the Primary Key of Product table. In the order_table, P_Code is a Foreign Key. Referential Integrity: This property of a relational database which ensures that no entry in a foreign key column of a table can be made unless it matches a primary key value in the corresponding related table is called Referential Integrity Union The UNION operator is used to combine the result-set of two or more SELECT statements. Result-set can be combined only if each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order. Syntax : SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;
  • 15. Suppose a Database contains two tables: XIISC and XIICOM. If it is required to produce a combined list of all the students, then it can be done as follows: SELECT rollno,name,class from xiisc UNION SELECT rollno,name,class from xiicom;
  • 16. SQL Constraints Def – A Constraint is a condition or check applicable on a field or set of fields. Types of constraint 1. Not Null – Makes sure that NULLs are not accepted in the specified column. Example CREATE TABLE CUSTOMER ( SID INTEGER NOT NULL, FIRST_NAME VARCHAR(30) NOT NULL, LAST_NAME VARCHAR(30) ); 2. DEFAULT Constraint – The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not provide a specific value. Example CREATE TABLE CUSTOMER ( SID INTEGER FIRST_NAME VARCHAR(30) , LAST_NAME VARCHAR(30) SCORE INTEGER DEFAULT 80); and execute the following statement, INSERT INTO CUSTOMER (SID,FIRST_NAME,LAST_NAME) VALUES (1, ’AJAY’, ’SHARMA’); Then output will be SID FIRST_NAME LAST_NAME SCORE 1 AJAY SHARMA 80
  • 17. 3. UNIQUE Constraint – Makes sure that duplicate values in the specified column are not accepted. Example 4. PRIMARY KEY Constraint – 1. Defining Primary Key through Create Table Command 2. Composite Primary Key – When multiple fields are used as a primary key, they are called a composite primary key. CREATE TABLE CUSTOMER ( BRANCH INTEGER, SID INTEGER , FIRST_NAME VARCHAR(30) , LAST_NAME VARCHAR(30) PRIMARY KEY (BRANCH, SID)); 1. Defining Primary Key through Alter Table Command ALTER TABLE CUSTOMER ADD PRIMARY KEY (SID);