SlideShare a Scribd company logo
Lecture 8:
Advanced SQL
ISOM3260, Spring 2014
2
Where we are now
• Database environment
– Introduction to database
• Database development process
– steps to develop a database
• Conceptual data modeling
– entity-relationship (ER) diagram; enhanced ER
• Logical database design
– transforming ER diagram into relations; normalization
• Physical database design
– technical specifications of the database
• Database implementation
– Structured Query Language (SQL), Advanced SQL
• Advanced topics
– data and database administration
3
Database development activities during SDLC
4
Advanced SQL
• Joins
• Subqueries
• Ensuring Transaction Integrity
5
Processing Multiple Tables – Joins
• Join
– a relational operation that causes two or more tables with a common domain
to be combined into a single table or view
– the common columns in joined tables are usually the primary key of the
dominant table and the foreign key of the dependent table
• Equi-join
– a join in which the joining condition is based on equality between values in
the common columns; common columns appear redundantly in the result
table
• Natural join
– an equi-join in which one of the duplicate columns is eliminated in the result
table
• Outer join
– a join in which rows that do not have matching values in common columns
are nevertheless included in the result table (as opposed to inner join, in
which rows must have matching values in order to appear in the result table)
• Union join
– includes all columns from each table in the join, and an instance for each row
of each table
6
Figure 7-1: Sample Pine Valley Furniture data
Customer_T Order_T
Order_Line_T
Product_T
7
Example: Equi-join
• based on equality between values in the common columns
If WHERE clause is omitted, the query will return all combinations of customers
and orders (10 orders * 15 customers=150 rows).
8
• same as equi-join except that one of the duplicate columns is
eliminated in the result table
• most commonly used form of join operation
• For each customer who has placed an order, what is the customer’s
name and order number?
SELECT Customer_T.Customer_ID, Customer_Name, Order_ID
FROM Customer_T, Order_T
WHERE Customer_T.Customer_ID = Order_T.Customer_ID
Join involves multiple tables in
FROM clause
Example: Natural Join
WHERE clause performs the equality check
for common columns of the two tables
It must be specified from which table the
DBMS should pick Customer_ID
9
• Different syntax with same outcome
SELECT Customer_T.Customer_ID,Customer_Name,Order_ID
FROM Customer_T INNER JOIN Order_T
ON Customer_T.Customer_ID = Order_T.Customer_ID;
SELECT Customer_T.Customer_ID,Customer_Name,Order_ID
FROM Customer_T NATURAL JOIN Order_T
ON Customer_T.Customer_ID = Order_T.Customer_ID;
Example: Natural Join
10
• row in one table does not have a matching row in the other table
• null values appear in columns where there is no match between tables
• List customer name, ID number, and order number for all customers.
Include customer information even for customers that do not have an
order.
SELECT Customer_T.Customer_ID, Customer_Name, Order_ID
FROM Customer_T LEFT OUTER JOIN Order_T
ON Customer_T.Customer_ID = Order_T.Customer_ID;
Example: Outer Join
LEFT OUTER JOIN syntax will cause customer data
to appear even if there is no corresponding order data
11
Example: Outer Join
12
• The results table will contain all of the columns from each table and
will contain an instance for each row of data included from each table.
• Example:
– Customer_T has 15 customers and 6 attributes
– Order_T has 10 orders and 3 attributes
• Result Table
– 25 rows and 9 columns
– each customer row will contain 3 attributes with assigned null values
– each order row will contain 6 attributes with assigned null values
Example: Union Join
13
• Query: Assemble all information necessary to create an
invoice for order number 1006.
SELECT Customer_T.Customer_ID, Customer_Name, Customer_Address,
City, State, Postal_Code, Order_T.Order_ID, Order_Date, Quantity,
Product_Name, Unit_Price, (Quantity * Unit_Prrice)
FROM Customer_T, Order_T, Order_Line_T, Product_T
WHERE Customer_T.Customer_ID = Order_T.Customer_ID
AND Order_T.Order_ID = Order_Line_T.Order_ID
AND Order_Line_T.Product_ID = Product_T.Product_ID
AND Order_T.Order_ID = 1006;
Four tables involved in this join
Example: Multiple Join of Four Tables
Each pair of tables requires an equality-check
condition in the WHERE clause, matching
primary keys against foreign keys
14
From CUSTOMER_T table
From
ORDER_T table
From PRODUCT_T table
From
ORDER_LINE_T table
Expression
Figure 7-4: Results from a four-table join
15
Subqueries
• Subquery
– placing an inner query (SELECT statement) inside an outer
query
– result table display data from the table in the outer query only
• Options:
– In a condition of the WHERE clause
– As a “table” of the FROM clause
– Within the HAVING clause
• Subqueries can be:
– Noncorrelated
 execute inner query once for the entire outer query
– Correlated
 execute inner query once for each row returned by the outer query
16
Example 1: Subqueries
• Two equivalent queries
– one using a join
– one using a subquery
17
• Which customers have placed orders?
SELECT Customer_Name
FROM Customer_T
WHERE Customer_ID IN
(SELECT DISTINCT Customer_ID FROM Order_T);
Example 2: Subquery
Subquery is embedded in parentheses. In this case it returns a
list that will be used in the WHERE clause of the outer query
The IN operator will test to see
if the Customer_ID value of a
row is included in the list
returned from the subquery
18
Figure 7-8(a):
Processing a
noncorrelated
subquery
No reference to data
in outer query, so
subquery executes
once only
19
Example 3: Subquery
• The qualifier NOT may be used in front of IN; while ANY and
ALL with logical operators such as =, >, and <.
A join can be used
in an inner query
Note: Inner query return list of customers who had ordered computer desk.
Outer query list customers who were not in the list returned by inner query.
20
Correlated vs. Noncorrelated
Subqueries
• Noncorrelated subqueries
– do not depend on data from the outer query
– execute once for the entire outer query
• Correlated subqueries
– do make use of data from the outer query
– execute once for each row of the outer query
– can make use of the EXISTS operator
21
• What are the order numbers that include furniture finished
in natural ash?
SELECT DISTINCT Order_ID FROM Order_Line_T
WHERE EXISTS
(SELECT * FROM Product_T
WHERE Product_ID = Order_Line_T.Product_ID
AND Product_Finish = ‘Natural Ash’);
Example 4: Correlated Subquery
The subquery is testing for a value
that comes from the outer query .
The EXISTS operator will return a
TRUE value if the subquery resulted
in a non-empty set, otherwise it
returns a FALSE.
22
Figure 7-8(b):
Processing a
correlated
subquery
Subquery refers to outer-query data,
so executes once for each row
of outer query
ORDER_ID
1001
1002
1003
1006
1007
1008
1009
Result:
23
Example 5: Correlated Subqueries
24
• Which products have a standard price that is higher that the
average standard price?
SELECT Product_Description, Standard_Price, AVGPRICE
FROM
(SELECT AVG(Standard_Price) AVGPRICE FROM Product_T),
Product_T
WHERE Standard_Price > AVGPRICE;
Example 6: Subquery as Derived Table
The WHERE clause normally cannot include aggregate functions, but because the
aggregate is performed in the subquery, its result can be used in the outer query’s
WHERE clause
One column of the subquery is
an aggregate function that has an
alias name. That alias can then be
referred to in the outer query
Subquery forms the derived
table used in the FROM clause
of the outer query
25
Ensuring Transaction Integrity
• Transaction
– a discrete unit of work that must be completely processed or
not processed at all
– may involve multiple DML commands
 INSERT, DELETE, UPDATE
– if any command fails, then all other changes must be cancelled
• SQL commands for transactions
– BEGIN TRANSACTION/END TRANSACTION
 marks boundaries of a transaction
– COMMIT
 makes all changes permanent
– ROLLBACK
 cancels changes since the last COMMIT
26
Figure 7-12: An SQL Transaction sequence (in pseudocode)
27
Review Questions
• What are the 4 types of join?
• What are subqueries?
• What is a correlated subquery?
• What is a noncorrelated subquery?
• What is a transaction?

More Related Content

What's hot

joins and subqueries in big data analysis
joins and subqueries in big data analysisjoins and subqueries in big data analysis
joins and subqueries in big data analysis
SanSan149
 
Advanced SQL
Advanced SQLAdvanced SQL
Advanced SQL
Sabiha M
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
Ankit Rai
 
The Relational Database Model
The Relational Database ModelThe Relational Database Model
The Relational Database Model
Shishir Aryal
 
joins in database
 joins in database joins in database
joins in database
Sultan Arshad
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
Ritwik Das
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
Vikas Gupta
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
Syed Zaid Irshad
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
Punjab University
 
Triggers
TriggersTriggers
Triggers
Pooja Dixit
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
Lakshman Basnet
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
Md. Mahedee Hasan
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
Nilt1234
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
Swapnali Pawar
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
raj upadhyay
 
Types of keys dbms
Types of keys dbmsTypes of keys dbms
Types of keys dbms
Surkhab Shelly
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query Optimization
Brian Gallagher
 

What's hot (20)

joins and subqueries in big data analysis
joins and subqueries in big data analysisjoins and subqueries in big data analysis
joins and subqueries in big data analysis
 
Advanced SQL
Advanced SQLAdvanced SQL
Advanced SQL
 
Joins And Its Types
Joins And Its TypesJoins And Its Types
Joins And Its Types
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
The Relational Database Model
The Relational Database ModelThe Relational Database Model
The Relational Database Model
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
joins in database
 joins in database joins in database
joins in database
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
 
Triggers
TriggersTriggers
Triggers
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
 
Types of keys dbms
Types of keys dbmsTypes of keys dbms
Types of keys dbms
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query Optimization
 

Viewers also liked

Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Beat Signer
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
Hammad Rasheed
 
Hasil tes UKG sebagai cermin dari kualitas pendidikan guru di Indonesia
Hasil tes UKG sebagai cermin dari kualitas pendidikan guru di IndonesiaHasil tes UKG sebagai cermin dari kualitas pendidikan guru di Indonesia
Hasil tes UKG sebagai cermin dari kualitas pendidikan guru di Indonesia
Vina Serevina
 
Master of Computer Application (MCA) – Semester 4 MC0077
Master of Computer Application (MCA) – Semester 4  MC0077Master of Computer Application (MCA) – Semester 4  MC0077
Master of Computer Application (MCA) – Semester 4 MC0077
Aravind NC
 
KG2 D 2013-2014 Learning Together
KG2 D 2013-2014 Learning TogetherKG2 D 2013-2014 Learning Together
KG2 D 2013-2014 Learning Together
megansnow927
 
Revision sheet grade kg2
Revision sheet grade kg2Revision sheet grade kg2
Revision sheet grade kg2
Moh Azs
 
Revision sheet grade kg1
Revision sheet grade kg1Revision sheet grade kg1
Revision sheet grade kg1
Moh Azs
 
Oracle sql & plsql
Oracle sql & plsqlOracle sql & plsql
Oracle sql & plsql
Sid Xing
 
My sql explain cheat sheet
My sql explain cheat sheetMy sql explain cheat sheet
My sql explain cheat sheetAchievers Tech
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
mohdoracle
 
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query ConceptsMS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
DataminingTools Inc
 
GraphTalks Rome - The Italian Business Graph
GraphTalks Rome - The Italian Business GraphGraphTalks Rome - The Italian Business Graph
GraphTalks Rome - The Italian Business Graph
Neo4j
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
1keydata
 
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick LearningOracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
eVideoTuition
 
Webinar: RDBMS to Graphs
Webinar: RDBMS to GraphsWebinar: RDBMS to Graphs
Webinar: RDBMS to Graphs
Neo4j
 

Viewers also liked (18)

Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Hasil tes UKG sebagai cermin dari kualitas pendidikan guru di Indonesia
Hasil tes UKG sebagai cermin dari kualitas pendidikan guru di IndonesiaHasil tes UKG sebagai cermin dari kualitas pendidikan guru di Indonesia
Hasil tes UKG sebagai cermin dari kualitas pendidikan guru di Indonesia
 
Master of Computer Application (MCA) – Semester 4 MC0077
Master of Computer Application (MCA) – Semester 4  MC0077Master of Computer Application (MCA) – Semester 4  MC0077
Master of Computer Application (MCA) – Semester 4 MC0077
 
Math Foundations
Math FoundationsMath Foundations
Math Foundations
 
KG2 D 2013-2014 Learning Together
KG2 D 2013-2014 Learning TogetherKG2 D 2013-2014 Learning Together
KG2 D 2013-2014 Learning Together
 
Revision sheet grade kg2
Revision sheet grade kg2Revision sheet grade kg2
Revision sheet grade kg2
 
Revision sheet grade kg1
Revision sheet grade kg1Revision sheet grade kg1
Revision sheet grade kg1
 
Oracle sql & plsql
Oracle sql & plsqlOracle sql & plsql
Oracle sql & plsql
 
My sql explain cheat sheet
My sql explain cheat sheetMy sql explain cheat sheet
My sql explain cheat sheet
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query ConceptsMS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
 
GraphTalks Rome - The Italian Business Graph
GraphTalks Rome - The Italian Business GraphGraphTalks Rome - The Italian Business Graph
GraphTalks Rome - The Italian Business Graph
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick LearningOracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
 
Webinar: RDBMS to Graphs
Webinar: RDBMS to GraphsWebinar: RDBMS to Graphs
Webinar: RDBMS to Graphs
 

Similar to advanced sql(database)

The Database Environment Chapter 8
The Database Environment Chapter 8The Database Environment Chapter 8
The Database Environment Chapter 8
Jeanie Arnoco
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql execution
xKinAnx
 
Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptxServer Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptx
auzee32
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
Tony Wong
 
SQL200.2 Module 2
SQL200.2 Module 2SQL200.2 Module 2
SQL200.2 Module 2
Dan D'Urso
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
Vibrant Technologies & Computers
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
SherinRappai
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
SherinRappai1
 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data   SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
Vibrant Technologies & Computers
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index TuningManikanda kumar
 
Data Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfData Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdf
howto4ucontact
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
Ahmed Farag
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptx
ssuser6bf2d1
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
TechandMate
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
Best SEO Tampa
 
Chapter9 more on database and sql
Chapter9 more on database and sqlChapter9 more on database and sql
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 

Similar to advanced sql(database) (20)

The Database Environment Chapter 8
The Database Environment Chapter 8The Database Environment Chapter 8
The Database Environment Chapter 8
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Sql wksht-6
Sql wksht-6Sql wksht-6
Sql wksht-6
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql execution
 
Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptxServer Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptx
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
SQL(database)
SQL(database)SQL(database)
SQL(database)
 
SQL200.2 Module 2
SQL200.2 Module 2SQL200.2 Module 2
SQL200.2 Module 2
 
Sql ch 5
Sql ch 5Sql ch 5
Sql ch 5
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data   SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
Data Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfData Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdf
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptx
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
 
Chapter9 more on database and sql
Chapter9 more on database and sqlChapter9 more on database and sql
Chapter9 more on database and sql
 

More from welcometofacebook

Quantitative exercise-toasty oven
Quantitative exercise-toasty ovenQuantitative exercise-toasty oven
Quantitative exercise-toasty ovenwelcometofacebook
 
EVC exercise-novel motor oil
EVC exercise-novel motor oilEVC exercise-novel motor oil
EVC exercise-novel motor oilwelcometofacebook
 
cltv calculation-calyx corolla
cltv calculation-calyx corolla cltv calculation-calyx corolla
cltv calculation-calyx corolla welcometofacebook
 
competing in a global market(4210)
competing in a global market(4210)competing in a global market(4210)
competing in a global market(4210)welcometofacebook
 
distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)welcometofacebook
 
distribution strategies(4210)
distribution strategies(4210)distribution strategies(4210)
distribution strategies(4210)welcometofacebook
 
product and brand strategies(4210)
product and brand strategies(4210)product and brand strategies(4210)
product and brand strategies(4210)welcometofacebook
 
overview of marketing strategy(4210)
overview of marketing strategy(4210)overview of marketing strategy(4210)
overview of marketing strategy(4210)welcometofacebook
 
Class+3+ +quantitative+analysis+exercise+answer+key
Class+3+ +quantitative+analysis+exercise+answer+keyClass+3+ +quantitative+analysis+exercise+answer+key
Class+3+ +quantitative+analysis+exercise+answer+keywelcometofacebook
 

More from welcometofacebook (20)

Quantitative exercise-toasty oven
Quantitative exercise-toasty ovenQuantitative exercise-toasty oven
Quantitative exercise-toasty oven
 
EVC exercise-novel motor oil
EVC exercise-novel motor oilEVC exercise-novel motor oil
EVC exercise-novel motor oil
 
jones blair calculations
jones blair calculationsjones blair calculations
jones blair calculations
 
EVC exercise-odi case
EVC exercise-odi caseEVC exercise-odi case
EVC exercise-odi case
 
cltv calculation-calyx corolla
cltv calculation-calyx corolla cltv calculation-calyx corolla
cltv calculation-calyx corolla
 
consumer behavior(4210)
consumer behavior(4210)consumer behavior(4210)
consumer behavior(4210)
 
competing in a global market(4210)
competing in a global market(4210)competing in a global market(4210)
competing in a global market(4210)
 
promotion strategies(4210)
promotion strategies(4210)promotion strategies(4210)
promotion strategies(4210)
 
pricing strategies(4210)
pricing strategies(4210)pricing strategies(4210)
pricing strategies(4210)
 
Pharmasim
PharmasimPharmasim
Pharmasim
 
distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)
 
distribution strategies(4210)
distribution strategies(4210)distribution strategies(4210)
distribution strategies(4210)
 
the birth of swatch(4210)
the birth of swatch(4210)the birth of swatch(4210)
the birth of swatch(4210)
 
product and brand strategies(4210)
product and brand strategies(4210)product and brand strategies(4210)
product and brand strategies(4210)
 
stp case jones blair(4210)
stp case jones blair(4210)stp case jones blair(4210)
stp case jones blair(4210)
 
stp(4210)
stp(4210)stp(4210)
stp(4210)
 
situational analysis(4210)
situational analysis(4210)situational analysis(4210)
situational analysis(4210)
 
quantitative analysis(4210)
quantitative analysis(4210)quantitative analysis(4210)
quantitative analysis(4210)
 
overview of marketing strategy(4210)
overview of marketing strategy(4210)overview of marketing strategy(4210)
overview of marketing strategy(4210)
 
Class+3+ +quantitative+analysis+exercise+answer+key
Class+3+ +quantitative+analysis+exercise+answer+keyClass+3+ +quantitative+analysis+exercise+answer+key
Class+3+ +quantitative+analysis+exercise+answer+key
 

Recently uploaded

ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 

Recently uploaded (20)

ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 

advanced sql(database)

  • 2. 2 Where we are now • Database environment – Introduction to database • Database development process – steps to develop a database • Conceptual data modeling – entity-relationship (ER) diagram; enhanced ER • Logical database design – transforming ER diagram into relations; normalization • Physical database design – technical specifications of the database • Database implementation – Structured Query Language (SQL), Advanced SQL • Advanced topics – data and database administration
  • 4. 4 Advanced SQL • Joins • Subqueries • Ensuring Transaction Integrity
  • 5. 5 Processing Multiple Tables – Joins • Join – a relational operation that causes two or more tables with a common domain to be combined into a single table or view – the common columns in joined tables are usually the primary key of the dominant table and the foreign key of the dependent table • Equi-join – a join in which the joining condition is based on equality between values in the common columns; common columns appear redundantly in the result table • Natural join – an equi-join in which one of the duplicate columns is eliminated in the result table • Outer join – a join in which rows that do not have matching values in common columns are nevertheless included in the result table (as opposed to inner join, in which rows must have matching values in order to appear in the result table) • Union join – includes all columns from each table in the join, and an instance for each row of each table
  • 6. 6 Figure 7-1: Sample Pine Valley Furniture data Customer_T Order_T Order_Line_T Product_T
  • 7. 7 Example: Equi-join • based on equality between values in the common columns If WHERE clause is omitted, the query will return all combinations of customers and orders (10 orders * 15 customers=150 rows).
  • 8. 8 • same as equi-join except that one of the duplicate columns is eliminated in the result table • most commonly used form of join operation • For each customer who has placed an order, what is the customer’s name and order number? SELECT Customer_T.Customer_ID, Customer_Name, Order_ID FROM Customer_T, Order_T WHERE Customer_T.Customer_ID = Order_T.Customer_ID Join involves multiple tables in FROM clause Example: Natural Join WHERE clause performs the equality check for common columns of the two tables It must be specified from which table the DBMS should pick Customer_ID
  • 9. 9 • Different syntax with same outcome SELECT Customer_T.Customer_ID,Customer_Name,Order_ID FROM Customer_T INNER JOIN Order_T ON Customer_T.Customer_ID = Order_T.Customer_ID; SELECT Customer_T.Customer_ID,Customer_Name,Order_ID FROM Customer_T NATURAL JOIN Order_T ON Customer_T.Customer_ID = Order_T.Customer_ID; Example: Natural Join
  • 10. 10 • row in one table does not have a matching row in the other table • null values appear in columns where there is no match between tables • List customer name, ID number, and order number for all customers. Include customer information even for customers that do not have an order. SELECT Customer_T.Customer_ID, Customer_Name, Order_ID FROM Customer_T LEFT OUTER JOIN Order_T ON Customer_T.Customer_ID = Order_T.Customer_ID; Example: Outer Join LEFT OUTER JOIN syntax will cause customer data to appear even if there is no corresponding order data
  • 12. 12 • The results table will contain all of the columns from each table and will contain an instance for each row of data included from each table. • Example: – Customer_T has 15 customers and 6 attributes – Order_T has 10 orders and 3 attributes • Result Table – 25 rows and 9 columns – each customer row will contain 3 attributes with assigned null values – each order row will contain 6 attributes with assigned null values Example: Union Join
  • 13. 13 • Query: Assemble all information necessary to create an invoice for order number 1006. SELECT Customer_T.Customer_ID, Customer_Name, Customer_Address, City, State, Postal_Code, Order_T.Order_ID, Order_Date, Quantity, Product_Name, Unit_Price, (Quantity * Unit_Prrice) FROM Customer_T, Order_T, Order_Line_T, Product_T WHERE Customer_T.Customer_ID = Order_T.Customer_ID AND Order_T.Order_ID = Order_Line_T.Order_ID AND Order_Line_T.Product_ID = Product_T.Product_ID AND Order_T.Order_ID = 1006; Four tables involved in this join Example: Multiple Join of Four Tables Each pair of tables requires an equality-check condition in the WHERE clause, matching primary keys against foreign keys
  • 14. 14 From CUSTOMER_T table From ORDER_T table From PRODUCT_T table From ORDER_LINE_T table Expression Figure 7-4: Results from a four-table join
  • 15. 15 Subqueries • Subquery – placing an inner query (SELECT statement) inside an outer query – result table display data from the table in the outer query only • Options: – In a condition of the WHERE clause – As a “table” of the FROM clause – Within the HAVING clause • Subqueries can be: – Noncorrelated  execute inner query once for the entire outer query – Correlated  execute inner query once for each row returned by the outer query
  • 16. 16 Example 1: Subqueries • Two equivalent queries – one using a join – one using a subquery
  • 17. 17 • Which customers have placed orders? SELECT Customer_Name FROM Customer_T WHERE Customer_ID IN (SELECT DISTINCT Customer_ID FROM Order_T); Example 2: Subquery Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query The IN operator will test to see if the Customer_ID value of a row is included in the list returned from the subquery
  • 18. 18 Figure 7-8(a): Processing a noncorrelated subquery No reference to data in outer query, so subquery executes once only
  • 19. 19 Example 3: Subquery • The qualifier NOT may be used in front of IN; while ANY and ALL with logical operators such as =, >, and <. A join can be used in an inner query Note: Inner query return list of customers who had ordered computer desk. Outer query list customers who were not in the list returned by inner query.
  • 20. 20 Correlated vs. Noncorrelated Subqueries • Noncorrelated subqueries – do not depend on data from the outer query – execute once for the entire outer query • Correlated subqueries – do make use of data from the outer query – execute once for each row of the outer query – can make use of the EXISTS operator
  • 21. 21 • What are the order numbers that include furniture finished in natural ash? SELECT DISTINCT Order_ID FROM Order_Line_T WHERE EXISTS (SELECT * FROM Product_T WHERE Product_ID = Order_Line_T.Product_ID AND Product_Finish = ‘Natural Ash’); Example 4: Correlated Subquery The subquery is testing for a value that comes from the outer query . The EXISTS operator will return a TRUE value if the subquery resulted in a non-empty set, otherwise it returns a FALSE.
  • 22. 22 Figure 7-8(b): Processing a correlated subquery Subquery refers to outer-query data, so executes once for each row of outer query ORDER_ID 1001 1002 1003 1006 1007 1008 1009 Result:
  • 24. 24 • Which products have a standard price that is higher that the average standard price? SELECT Product_Description, Standard_Price, AVGPRICE FROM (SELECT AVG(Standard_Price) AVGPRICE FROM Product_T), Product_T WHERE Standard_Price > AVGPRICE; Example 6: Subquery as Derived Table The WHERE clause normally cannot include aggregate functions, but because the aggregate is performed in the subquery, its result can be used in the outer query’s WHERE clause One column of the subquery is an aggregate function that has an alias name. That alias can then be referred to in the outer query Subquery forms the derived table used in the FROM clause of the outer query
  • 25. 25 Ensuring Transaction Integrity • Transaction – a discrete unit of work that must be completely processed or not processed at all – may involve multiple DML commands  INSERT, DELETE, UPDATE – if any command fails, then all other changes must be cancelled • SQL commands for transactions – BEGIN TRANSACTION/END TRANSACTION  marks boundaries of a transaction – COMMIT  makes all changes permanent – ROLLBACK  cancels changes since the last COMMIT
  • 26. 26 Figure 7-12: An SQL Transaction sequence (in pseudocode)
  • 27. 27 Review Questions • What are the 4 types of join? • What are subqueries? • What is a correlated subquery? • What is a noncorrelated subquery? • What is a transaction?