SlideShare a Scribd company logo
UNIT III-SYSTEM
ARCHITECTURE
THE SQL GROUP BY STATEMENT
 The GROUP BY statement groups rows that have
the same values into summary rows, like "find the
number of customers in each country".
 The GROUP BY statement is often used with
aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group
the result-set by one or more columns.
 GROUP BY Syntax
 SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
EXAMPLE 1
 SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
EXAMPLE 2
 SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
EXAMPLE 3
 SELECT Shippers.ShipperName, COUNT(Order
s.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers ON Orders.ShipperID =
Shippers.ShipperID
GROUP BY ShipperName;
THE SQL HAVING CLAUSE
 The HAVING clause was added to SQL because
the WHERE keyword cannot be used with
aggregate functions.
 HAVING Syntax
 SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SQL HAVING EXAMPLES
 The following SQL statement lists the number of
customers in each country. Only include
countries with more than 5 customers:
 Example
 SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
EXAMPLE 1
 SELECT Employees.LastName, COUNT(Orders.
OrderID) AS NumberOfOrders
FROM (Orders
INNER JOIN Employees ON Orders.EmployeeID
= Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;
THE SQL EXISTS OPERATOR
 The EXISTS operator is used to test for the
existence of any record in a subquery.
 The EXISTS operator returns TRUE if the
subquery returns one or more records.
 EXISTS Syntax
 SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHE
RE condition);
EXAMPLE 2
 SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM
Products WHERE Products.SupplierID =
Suppliers.supplierID AND Price = 22);
WHAT IS A STORED PROCEDURE?
 A stored procedure is a prepared SQL code that
you can save, so the code can be reused over and
over again.
 So if you have an SQL query that you write over
and over again, save it as a stored procedure, and
then just call it to execute it.
 You can also pass parameters to a stored
procedure, so that the stored procedure can act
based on the parameter value(s) that is passed
STORED PROCEDURE SYNTAX
 CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
 (Or)
Delimiter
Create procedure shoppy
(IN article int)
Begin
Select article from shop where article=article;
end
STORED PROCEDURE EXAMPLE
 The following SQL statement creates a stored
procedure named "SelectAllCustomers" that
selects all records from the "Customers" table:
 Example
 CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;
EXAMPLE 2
Delimiter
Create procedure getproductdesc_with parameters
(IN PID INT)
Begin
Select
P.productID,P.ProductName,PD.productDescript
ion from product P INNER JOIN
productDescription PD on
p.productID=PD.ProductID Where
P.Products=PID;
END
EXEC
 Execute the stored procedure above as follows:
 Example
 EXEC SelectAllCustomers;
SQL CREATE INDEX STATEMENT
 The CREATE INDEX statement is used to create
indexes in tables.
 Indexes are used to retrieve data from the
database more quickly than otherwise. The users
cannot see the indexes, they are just used to
speed up searches/queries.
CREATE INDEX SYNTAX
 Creates an index on a table. Duplicate values are
allowed:
 CREATE INDEX index_name
ON table_name (column1, column2, ...);
 CREATE UNIQUE INDEX Syntax
 Creates a unique index on a table. Duplicate
values are not allowed:
 CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ...);
CREATE INDEX EXAMPLE
 The SQL statement below creates an index
named "idx_lastname" on the "LastName"
column in the "Persons" table:
 CREATE INDEX idx_lastname
ON Persons (LastName);
 If you want to create an index on a combination
of columns, you can list the column names within
the parentheses, separated by commas:
 CREATE INDEX idx_pname
ON Persons (LastName, FirstName);
DROP INDEX STATEMENT
 The DROP INDEX statement is used to delete an
index in a table.
 MS Access:
 DROP INDEX index_name ON table_name;
INTRODUCTION ON TRIGGERS
 A trigger is a set of actions that are run
automatically when a specified change operation
(SQL INSERT, UPDATE, or DELETE statement)
is performed on a specified table. Triggers are
useful for tasks such as enforcing business rules,
validating input data, and keeping an audit trail
USES FOR TRIGGERS:
 Enforce business rules
 Validate input data
 Generate a unique value for a newly-inserted row
in a different file.
 Write to other files for audit trail purposes
 Query from other files for cross-referencing
purposes
 Access system functions
 Replicate data to different files to achieve data
consistency
INSERT TRIGGER
Example: consider stuff table & stuff_pf table
Query:
Create trigger After_insert_stuff
After insert on stuff for each row
Insert into stuff_pf(stuff_id,pf) values
(new.id,new.salary*0.10);
UPDATE TRIGGER
Example: consider table 1 as batch and table
2 as rooms
Query:
Create trigger After_update_batch
After update on batch for each row
Update rooms
Set status=“assigned”
Where room_id=new.room_id
DELETE TRIGGER
MySQL BEFORE DELETE Trigger:
BEFORE DELETE Trigger in MySQL is invoked
automatically whenever a delete operation is fired on
the table. In this article, we are going to learn how to
create a before delete trigger with its syntax and
example.
Syntax
The following is the syntax to create a BEFORE
DELETE trigger in MySQL:
CREATE TRIGGER trigger_name
BEFORE DELETE
ON table_name FOR EACH ROW
Trigger_body ;
DELETE TRIGGER
Example: consider table 1 as batch and table
2 as rooms
Query:
Create trigger After_delete_batch
After delete on batch for each row
Delete from batch
Where room_id=old.room_id

More Related Content

PPTX
SQL.pptx for the begineers and good know
PPTX
PDF
Database Management System 1
PDF
kupdf.net_sql-ebook-w3schoolscom.pdf
PPTX
Database Overview
PPTX
Sql – pocket guide
PPTX
SQL command practical power point slides, which help you in learning sql.pptx
SQL.pptx for the begineers and good know
Database Management System 1
kupdf.net_sql-ebook-w3schoolscom.pdf
Database Overview
Sql – pocket guide
SQL command practical power point slides, which help you in learning sql.pptx

Similar to unit 3nit unit 1_unit1_unit1_.unit 1_unit1_unit1_ (20)

PPTX
Database management system by Neeraj Bhandari ( Surkhet.Nepal )
PDF
full detailled SQL notesquestion bank (1).pdf
PPT
Introduction to Structured Query Language (SQL).ppt
PPT
INTRODUCTION TO SQL QUERIES REALTED BRIEF
PPTX
Database COMPLETE
PDF
Database Architecture and Basic Concepts
PPTX
Sql slid
PPTX
12. Basic SQL Queries (2).pptx
PDF
PPTX
about-SQL AND ETC.pptx
PPTX
SQL Query
PPTX
SQL Sort Notes
PPTX
Structure Query Language Advance Training
PPTX
session_2_sqlpptxfhfhfhfdhfdhkkfdhfdhfdh
PDF
Intruduction to SQL.Structured Query Language(SQL}
PDF
Chapter – 6 SQL Lab Tutorial.pdf
PPTX
MYSql manage db
PDF
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
PPTX
PPT SQL CLASS.pptx
Database management system by Neeraj Bhandari ( Surkhet.Nepal )
full detailled SQL notesquestion bank (1).pdf
Introduction to Structured Query Language (SQL).ppt
INTRODUCTION TO SQL QUERIES REALTED BRIEF
Database COMPLETE
Database Architecture and Basic Concepts
Sql slid
12. Basic SQL Queries (2).pptx
about-SQL AND ETC.pptx
SQL Query
SQL Sort Notes
Structure Query Language Advance Training
session_2_sqlpptxfhfhfhfdhfdhkkfdhfdhfdh
Intruduction to SQL.Structured Query Language(SQL}
Chapter – 6 SQL Lab Tutorial.pdf
MYSql manage db
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
PPT SQL CLASS.pptx
Ad

Recently uploaded (20)

PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
GDM (1) (1).pptx small presentation for students
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Institutional Correction lecture only . . .
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Module 4: Burden of Disease Tutorial Slides S2 2025
GDM (1) (1).pptx small presentation for students
O7-L3 Supply Chain Operations - ICLT Program
202450812 BayCHI UCSC-SV 20250812 v17.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Anesthesia in Laparoscopic Surgery in India
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Institutional Correction lecture only . . .
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Types and Its function , kingdom of life
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
VCE English Exam - Section C Student Revision Booklet
Microbial disease of the cardiovascular and lymphatic systems
STATICS OF THE RIGID BODIES Hibbelers.pdf
Ad

unit 3nit unit 1_unit1_unit1_.unit 1_unit1_unit1_

  • 2. THE SQL GROUP BY STATEMENT  The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".  The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.  GROUP BY Syntax  SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s);
  • 3. EXAMPLE 1  SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
  • 4. EXAMPLE 2  SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ORDER BY COUNT(CustomerID) DESC;
  • 5. EXAMPLE 3  SELECT Shippers.ShipperName, COUNT(Order s.OrderID) AS NumberOfOrders FROM Orders LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID GROUP BY ShipperName;
  • 6. THE SQL HAVING CLAUSE  The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.  HAVING Syntax  SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
  • 7. SQL HAVING EXAMPLES  The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers:  Example  SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
  • 8. EXAMPLE 1  SELECT Employees.LastName, COUNT(Orders. OrderID) AS NumberOfOrders FROM (Orders INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID) GROUP BY LastName HAVING COUNT(Orders.OrderID) > 10;
  • 9. THE SQL EXISTS OPERATOR  The EXISTS operator is used to test for the existence of any record in a subquery.  The EXISTS operator returns TRUE if the subquery returns one or more records.  EXISTS Syntax  SELECT column_name(s) FROM table_name WHERE EXISTS (SELECT column_name FROM table_name WHE RE condition);
  • 10. EXAMPLE 2  SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price = 22);
  • 11. WHAT IS A STORED PROCEDURE?  A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again.  So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it.  You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed
  • 12. STORED PROCEDURE SYNTAX  CREATE PROCEDURE procedure_name AS sql_statement GO;  (Or) Delimiter Create procedure shoppy (IN article int) Begin Select article from shop where article=article; end
  • 13. STORED PROCEDURE EXAMPLE  The following SQL statement creates a stored procedure named "SelectAllCustomers" that selects all records from the "Customers" table:  Example  CREATE PROCEDURE SelectAllCustomers AS SELECT * FROM Customers GO;
  • 14. EXAMPLE 2 Delimiter Create procedure getproductdesc_with parameters (IN PID INT) Begin Select P.productID,P.ProductName,PD.productDescript ion from product P INNER JOIN productDescription PD on p.productID=PD.ProductID Where P.Products=PID; END
  • 15. EXEC  Execute the stored procedure above as follows:  Example  EXEC SelectAllCustomers;
  • 16. SQL CREATE INDEX STATEMENT  The CREATE INDEX statement is used to create indexes in tables.  Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries.
  • 17. CREATE INDEX SYNTAX  Creates an index on a table. Duplicate values are allowed:  CREATE INDEX index_name ON table_name (column1, column2, ...);  CREATE UNIQUE INDEX Syntax  Creates a unique index on a table. Duplicate values are not allowed:  CREATE UNIQUE INDEX index_name ON table_name (column1, column2, ...);
  • 18. CREATE INDEX EXAMPLE  The SQL statement below creates an index named "idx_lastname" on the "LastName" column in the "Persons" table:  CREATE INDEX idx_lastname ON Persons (LastName);  If you want to create an index on a combination of columns, you can list the column names within the parentheses, separated by commas:  CREATE INDEX idx_pname ON Persons (LastName, FirstName);
  • 19. DROP INDEX STATEMENT  The DROP INDEX statement is used to delete an index in a table.  MS Access:  DROP INDEX index_name ON table_name;
  • 20. INTRODUCTION ON TRIGGERS  A trigger is a set of actions that are run automatically when a specified change operation (SQL INSERT, UPDATE, or DELETE statement) is performed on a specified table. Triggers are useful for tasks such as enforcing business rules, validating input data, and keeping an audit trail
  • 21. USES FOR TRIGGERS:  Enforce business rules  Validate input data  Generate a unique value for a newly-inserted row in a different file.  Write to other files for audit trail purposes  Query from other files for cross-referencing purposes  Access system functions  Replicate data to different files to achieve data consistency
  • 22. INSERT TRIGGER Example: consider stuff table & stuff_pf table Query: Create trigger After_insert_stuff After insert on stuff for each row Insert into stuff_pf(stuff_id,pf) values (new.id,new.salary*0.10);
  • 23. UPDATE TRIGGER Example: consider table 1 as batch and table 2 as rooms Query: Create trigger After_update_batch After update on batch for each row Update rooms Set status=“assigned” Where room_id=new.room_id
  • 24. DELETE TRIGGER MySQL BEFORE DELETE Trigger: BEFORE DELETE Trigger in MySQL is invoked automatically whenever a delete operation is fired on the table. In this article, we are going to learn how to create a before delete trigger with its syntax and example. Syntax The following is the syntax to create a BEFORE DELETE trigger in MySQL: CREATE TRIGGER trigger_name BEFORE DELETE ON table_name FOR EACH ROW Trigger_body ;
  • 25. DELETE TRIGGER Example: consider table 1 as batch and table 2 as rooms Query: Create trigger After_delete_batch After delete on batch for each row Delete from batch Where room_id=old.room_id