SlideShare a Scribd company logo
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

PDF
Sql wksht-1
PPTX
SQL Functions
PPTX
Aggregate function
PDF
SQL JOINS
PPTX
SQL - DML and DDL Commands
PDF
Sql wksht-6
PDF
Chapter8 my sql revision tour
PPTX
Lab2 ddl commands
Sql wksht-1
SQL Functions
Aggregate function
SQL JOINS
SQL - DML and DDL Commands
Sql wksht-6
Chapter8 my sql revision tour
Lab2 ddl commands

What's hot (20)

PPTX
Sql joins
PPTX
Sql Constraints
PPTX
introdution to SQL and SQL functions
PPTX
SQL JOIN
PPTX
Date and time functions in mysql
PDF
Sql wksht-3
PDF
Sql ch 5
PPTX
SQL - Structured query language introduction
PDF
RMySQL Tutorial For Beginners
PDF
Set methods in python
PPTX
Inheritance in Java
PPTX
Interfaces and abstract classes
PDF
Introduction to SQL
PPT
Using the set operators
PPT
Class & Object - User Defined Method
PPT
Introduction to-sql
PPT
SQL subquery
PPTX
8. sql
PPTX
DDL And DML
PDF
PL/SQL TRIGGERS
Sql joins
Sql Constraints
introdution to SQL and SQL functions
SQL JOIN
Date and time functions in mysql
Sql wksht-3
Sql ch 5
SQL - Structured query language introduction
RMySQL Tutorial For Beginners
Set methods in python
Inheritance in Java
Interfaces and abstract classes
Introduction to SQL
Using the set operators
Class & Object - User Defined Method
Introduction to-sql
SQL subquery
8. sql
DDL And DML
PL/SQL TRIGGERS
Ad

Similar to Chapter9 more on database and sql (20)

PPTX
Day-2 SQL Theory_V1.pptx
PDF
Structured query language(sql)
PPT
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
PDF
MA3696 Lecture 9
PPT
Sql query [select, sub] 4
DOC
Sql Queries
PPTX
Unit 2 web technologies
PDF
Simple Spreadsheet Tips
PPT
Ms sql server ii
PDF
Manual for Troubleshooting Formulas & Functions in Excel
PPTX
Ex 8 Array notes study material1234.pptx
PPTX
Practical Tutorial about the PostgreSQL Database
PDF
Bt0075 rdbms with mysql 2
PDF
Introduction to oracle functions
PDF
An Introduction To Array Functions
PDF
5. Group Functions
PDF
Assignment 4
PPTX
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT
SQL200.2 Module 2
PPTX
Lab4 join - all types listed
Day-2 SQL Theory_V1.pptx
Structured query language(sql)
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
MA3696 Lecture 9
Sql query [select, sub] 4
Sql Queries
Unit 2 web technologies
Simple Spreadsheet Tips
Ms sql server ii
Manual for Troubleshooting Formulas & Functions in Excel
Ex 8 Array notes study material1234.pptx
Practical Tutorial about the PostgreSQL Database
Bt0075 rdbms with mysql 2
Introduction to oracle functions
An Introduction To Array Functions
5. Group Functions
Assignment 4
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
SQL200.2 Module 2
Lab4 join - all types listed
Ad

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.
01-Introduction-to-Information-Management.pdf
Final Presentation General Medicine 03-08-2024.pptx
Microbial diseases, their pathogenesis and prophylaxis
Abdominal Access Techniques with Prof. Dr. R K Mishra
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Renaissance Architecture: A Journey from Faith to Humanism
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Classroom Observation Tools for Teachers
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table

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);