SlideShare a Scribd company logo
Database Management System
Advanced Training
Practices in 2017
Prepared by: Moutasm Tamimi
Using SQL language
Microsoft SQL Server Management Studio
Versions (2008-2010-2012-2014)
Speaker Information
 Moutasm tamimi
Independent consultant , IT Researcher , CEO at ITG7
Instructor of: Project Development.
DBMS.
.NET applications.
Digital marketing.
Email: tamimi@itg7.com
LinkedIn: click here.
Database Management System - SQL Advanced Training
Joins
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Product
Company
Cname StockPrice Country
GizmoWorks 25 USA
Canon 65 Japan
Hitachi 15 Japan
PName Price
SingleTouch $149.99
SELECT PName, Price
FROM Product, Company
WHERE Manufacturer=CName AND Country=‘Japan’
AND Price <= 200
More Joins
Product (pname, price, category, manufacturer)
Company (cname, stockPrice, country)
Find all Chinese companies that manufacture products both in the
‘electronic’ and ‘toy’ categories
SELECT cname
FROM
WHERE
Cross Join
There are two tables A and B
A has a column Id and data (1,2,3)
B has a column Id and data (A,B)
If I put
Select A.Id, B.Id from A,B
This generates output as
A 1
B 1
C 1
A 2
B 2
C 2
Self Join
There is a table called Emp with the following structure:
empid ename mgrid
1 A null
2 B 1
3 C 1
4 D 2
If I want to print all managers using self join, I should write quey as:
select e1.ename from
emp e1,emp e2
where e1.mgrid = e2.empid
Inner Join
I have 2 tables Student(sid,Name) and Marks(Sid,Subject,Score)
If I want to print the marks of all students in the following format,
Name Subject Score
Select Name,Subject,Score from
Student s join Marks m
On s.sid = m.sid
Outer Join
 Right outer Join
 Print all the records in the second table with null values for missing records in the first
table
 Left outer Join
 Print all the records in the first table with null values for missing records in the second
table
 Full outer Join
 Prints all records in both the table with null values for missing records in both the table
Left Outer Join
I have a table Employee (Eid, Ename, Mid) and
a table Machine (Mid,ManufacturerName)
Employee
EidEName Mid
1 ABC1
2 DEF 3
Machine
Mid ManufacturerName
1 Zenith
2 HP
Left Outer Join
I want to print the employee name and machine name.
If I write a query using inner join, then the second employee will
not be displayed as the mid in his record is not avilable with the second
table.
So I go for left outer join. The query is as shown below:
Select Ename, ManufacturerName from Employee e left outer join
Machine m on e.Mid = m.Mid
Right outer Join
Assume data in the tables like this:
Employee
EidEName Mid
1 ABC1
2 DEF
Machine
Mid ManufacturerName
1 Zenith
2 HP
Right Outer Join
If I want to find which machine is unallocated, I can use right outer join.
The query is as follows:
Select Ename, ManufacturerName from Employee e right outer join
Machine m on e.Mid = m.Mid
This yields a result
ABCZenith
HP
Full Outer Join
Assume data in the tables like this:
Employee
EidEName Mid
1 ABC 1
2 DEF
3 GHI 2
Machine
Mid ManufacturerName
1 Zenith
2 HP
3 Compaq
Full Outer Join
If I want to find people who have been un allocated with a system and
machines that are been un allocated, I can go for full outer join.
Query is like this:
Select Ename, ManufacturerName from Employee e full outer join
Machine m on e.Mid = m.Mid
This yields a result
ABC Zenith
DEF
GHIHP
Compaq
Tuple Variables
SELECT DISTINCT pname, address
FROM Person, Company
WHERE worksfor = cname
Which
address ?
Person(pname, address, worksfor)
Company(cname, address)
SELECT DISTINCT Person.pname, Company.address
FROM Person, Company
WHERE Person.worksfor = Company.cname
SELECT DISTINCT x.pname, y.address
FROM Person AS x, Company AS y
WHERE x.worksfor = y.cname
SQL Functions
 SQL Avg()
 SQL Count()
 SQL First()
 SQL Last()
 SQL Max()
 SQL Min()
 SQL Sum()
 SQL Group By
• SQL HavingSQL Ucase()
• SQL Lcase()
• SQL Mid()
• SQL Len()
• SQL Round()
• SQL Now()
• SQL Format()
Aggregation
SELECT count(*)
FROM Product
WHERE year > 1995
Except count, all aggregations apply to a single attribute
SELECT avg(price)
FROM Product
WHERE maker=“Toyota”
SQL supports several aggregation operations:
sum, count, min, max, avg
COUNT applies to duplicates, unless otherwise stated:
SELECT Count(category)
FROM Product
WHERE year > 1995
same as Count(*)
We probably want:
SELECT Count(DISTINCT category)
FROM Product
WHERE year > 1995
Aggregation: Count
Purchase(product, date, price, quantity)
More Examples
SELECT Sum(price * quantity)
FROM Purchase
SELECT Sum(price * quantity)
FROM Purchase
WHERE product = ‘bagel’
What do
they mean ?
Simple Aggregations
Purchase
Product Date Price Quantity
Bagel 10/21 1 20
Banana 10/3 0.5 10
Banana 10/10 1 10
Bagel 10/25 1.50 20
SELECT Sum(price * quantity)
FROM Purchase
WHERE product = ‘bagel’
50 (= 20+30)
Grouping and Aggregation
Purchase(product, date, price, quantity)
SELECT product, Sum(price*quantity) AS TotalSales
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product
Let’s see what this means…
Find total sales after 10/1/2005 per product.
Grouping and Aggregation
1. Compute the FROM and WHERE clauses.
2. Group by the attributes in the GROUPBY
3. Compute the SELECT clause: grouped attributes and aggregates.
1&2. FROM-WHERE-GROUPBY
Product Date Price Quantity
Bagel 10/21 1 20
Bagel 10/25 1.50 20
Banana 10/3 0.5 10
Banana 10/10 1 10
3. SELECT
SELECT product, Sum(price*quantity) AS TotalSales
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product
Product Date Price Quantity
Bagel 10/21 1 20
Bagel 10/25 1.50 20
Banana 10/3 0.5 10
Banana 10/10 1 10
Product TotalSales
Bagel 50
Banana 15
GROUP BY v.s. Nested Quereis
SELECT product, Sum(price*quantity) AS TotalSales
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product
SELECT DISTINCT x.product, (SELECT Sum(y.price*y.quantity)
FROM Purchase y
WHERE x.product = y.product
AND y.date > ‘10/1/2005’)
AS TotalSales
FROM Purchase x
WHERE x.date > ‘10/1/2005’
Another Example
SELECT product,
sum(price * quantity) AS SumSales
max(quantity) AS MaxQuantity
FROM Purchase
GROUP BY product
What does
it mean ?
HAVING Clause
SELECT product, Sum(price * quantity)
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product
HAVING Sum(quantity) > 30
Same query, except that we consider only products that had
at least 100 buyers.
HAVING clause contains conditions on aggregates.
General form of Grouping and
Aggregation
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2
S = may contain attributes a1,…,ak and/or any aggregates but NO OTHER
ATTRIBUTES
C1 = is any condition on the attributes in R1,…,Rn
C2 = is any condition on aggregate expressions
Why ?
General form of Grouping and Aggregation
Evaluation steps:
1. Evaluate FROM-WHERE, apply condition C1
2. Group by the attributes a1,…,ak
3. Apply condition C2 to each group (may have aggregates)
4. Compute aggregates in S and return the result
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2
Advanced SQLizing
1. Getting around INTERSECT and EXCEPT
2. Quantifiers
3. Aggregation v.s. subqueries
1. INTERSECT and EXCEPT:
(SELECT R.A, R.B
FROM R)
INTERSECT
(SELECT S.A, S.B
FROM S)
SELECT R.A, R.B
FROM R
WHERE
EXISTS(SELECT *
FROM S
WHERE R.A=S.A and R.B=S.B)
(SELECT R.A, R.B
FROM R)
EXCEPT
(SELECT S.A, S.B
FROM S)
SELECT R.A, R.B
FROM R
WHERE
NOT EXISTS(SELECT *
FROM S
WHERE R.A=S.A and R.B=S.B)
If R, S have no
duplicates, then can
write without
subqueries
(HOW ?)
INTERSECT and EXCEPT: not in SQL Server
2. Quantifiers
Product ( pname, price, company)
Company( cname, city)
Find all companies that make some products with price < 100
SELECT DISTINCT Company.cname
FROM Company, Product
WHERE Company.cname = Product.company and Product.price < 100
Existential: easy ! 
2. Quantifiers
Product ( pname, price, company)
Company( cname, city)
Find all companies s.t. all of their products have price < 100
Universal: hard ! 
Find all companies that make only products with price < 100
same as:
2. Quantifiers
2. Find all companies s.t. all their products have price < 100
1. Find the other companies: i.e. s.t. some product  100
SELECT DISTINCT Company.cname
FROM Company
WHERE Company.cname IN (SELECT Product.company
FROM Product
WHERE Produc.price >= 100
SELECT DISTINCT Company.cname
FROM Company
WHERE Company.cname NOT IN (SELECT Product.company
FROM Product
WHERE Produc.price >= 100
3. Group-by v.s. Nested Query
 Find authors who wrote  10 documents:
 Attempt 1: with nested queries
SELECT DISTINCT Author.name
FROM Author
WHERE count(SELECT Wrote.url
FROM Wrote
WHERE Author.login=Wrote.login)
> 10
This is
SQL by
a novice
Author(login,name)
Wrote(login,url)
3. Group-by v.s. Nested Query
 Find all authors who wrote at least 10 documents:
 Attempt 2: SQL style (with GROUP BY)
SELECT Author.name
FROM Author, Wrote
WHERE Author.login=Wrote.login
GROUP BY Author.name
HAVING count(wrote.url) > 10
This is
SQL by
an expert
No need for DISTINCT: automatically from GROUP BY
3. Group-by v.s. Nested Query
Find authors with vocabulary  10000 words:
SELECT Author.name
FROM Author, Wrote, Mentions
WHERE Author.login=Wrote.login AND Wrote.url=Mentions.url
GROUP BY Author.name
HAVING count(distinct Mentions.word) > 10000
Author(login,name)
Wrote(login,url)
Mentions(url,word)
Two Examples
Store(sid, sname)
Product(pid, pname, price, sid)
Find all stores that sell only products with price > 100
same as:
Find all stores s.t. all their products have price > 100)
SELECT Store.name
FROM Store, Product
WHERE Store.sid = Product.sid
GROUP BY Store.sid, Store.name
HAVING 100 < min(Product.price)
SELECT Store.name
FROM Store
WHERE Store.sid NOT IN
(SELECT Product.sid
FROM Product
WHERE Product.price <= 100)
SELECT Store.name
FROM Store
WHERE
100 < ALL (SELECT Product.price
FROM product
WHERE Store.sid = Product.sid)
Almost equivalent…
Why both ?
Two Examples
Store(sid, sname)
Product(pid, pname, price, sid)
For each store,
find its most expensive product
Two Examples
SELECT Store.sname, max(Product.price)
FROM Store, Product
WHERE Store.sid = Product.sid
GROUP BY Store.sid, Store.sname
SELECT Store.sname, x.pname
FROM Store, Product x
WHERE Store.sid = x.sid and
x.price >=
ALL (SELECT y.price
FROM Product y
WHERE Store.sid = y.sid)
This is easy but doesn’t do what we want:
Better:
But may
return
multiple
product names
per store
Two Examples
SELECT Store.sname, max(x.pname)
FROM Store, Product x
WHERE Store.sid = x.sid and
x.price >=
ALL (SELECT y.price
FROM Product y
WHERE Store.sid = y.sid)
GROUP BY Store.sname
Finally, choose some pid arbitrarily, if there are many
with highest price:
NULLS in SQL
 Whenever we don’t have a value, we can put a NULL
 Can mean many things:
 Value does not exists
 Value exists but is unknown
 Value not applicable
 Etc.
 The schema specifies for each attribute if can be null (nullable
attribute) or not
 How does SQL cope with tables that have NULLs ?
Null Values
 If x= NULL then 4*(3-x)/7 is still NULL
 If x= NULL then x=“Joe” is UNKNOWN
 In SQL there are three boolean values:
FALSE = 0
UNKNOWN = 0.5
TRUE = 1
Null Values
 C1 AND C2 = min(C1, C2)
 C1 OR C2 = max(C1, C2)
 NOT C1 = 1 – C1
Rule in SQL: include only tuples that yield TRUE
SELECT *
FROM Person
WHERE (age < 25) AND
(height > 6 OR weight > 190)
E.g.
age=20
heigth=NULL
weight=200
Null Values
Unexpected behavior:
Some Persons are not included !
SELECT *
FROM Person
WHERE age < 25 OR age >= 25
Null Values
Can test for NULL explicitly:
 x IS NULL
 x IS NOT NULL
Now it includes all Persons
SELECT *
FROM Person
WHERE age < 25 OR age >= 25 OR age IS NULL
Outerjoins
Explicit joins in SQL = “inner joins”:
Product(name, category)
Purchase(prodName, store)
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
SELECT Product.name, Purchase.store
FROM Product, Purchase
WHERE Product.name = Purchase.prodName
Same as:
But Products that never sold will be lost !
Outerjoins
Left outer joins in SQL:
Product(name, category)
Purchase(prodName, store)
SELECT Product.name, Purchase.store
FROM Product LEFT OUTER JOIN Purchase ON
Product.name = Purchase.prodName
Name Category
Gizmo gadget
Camera Photo
OneClick Photo
ProdName Store
Gizmo Wiz
Camera Ritz
Camera Wiz
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL
Product Purchase
Application
Compute, for each product, the total number of sales in
‘September’
Product(name, category)
Purchase(prodName, month, store)
SELECT Product.name, count(*)
FROM Product, Purchase
WHERE Product.name = Purchase.prodName
and Purchase.month = ‘September’
GROUP BY Product.name
What’s wrong ?
Application
Compute, for each product, the total number of sales in
‘September’
Product(name, category)
Purchase(prodName, month, store)
SELECT Product.name, count(*)
FROM Product LEFT OUTER JOIN Purchase ON
Product.name = Purchase.prodName
and Purchase.month = ‘September’
GROUP BY Product.name
Now we also get the products who sold in 0 quantity
Outer Joins
 Left outer join:
 Include the left tuple even if there’s no match
 Right outer join:
 Include the right tuple even if there’s no match
 Full outer join:
 Include the both left and right tuples even if there’s no match
Modifying the Database
Three kinds of modifications
 Insertions
 Deletions
 Updates
Sometimes they are all called “updates”
Insertions
General form:
Missing attribute  NULL.
May drop attribute names if give them in order.
INSERT INTO R(A1,…., An) VALUES (v1,…., vn)
INSERT INTO Purchase(buyer, seller, product, store)
VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’,
‘The Sharper Image’)
Example: Insert a new purchase to the database:
Insertions
INSERT INTO PRODUCT(name)
SELECT DISTINCT Purchase.product
FROM Purchase
WHERE Purchase.date > “10/26/01”
The query replaces the VALUES keyword.
Here we insert many tuples into PRODUCT
Insertion: an Example
prodName is foreign key in Product.name
Suppose database got corrupted and we need to fix it:
name listPrice category
gizmo 100 gadgets
prodName buyerName price
camera John 200
gizmo Smith 80
camera Smith 225
Task: insert in Product all prodNames from Purchase
Product
Product(name, listPrice, category)
Purchase(prodName, buyerName, price)
Purchase
Insertion: an Example
INSERT INTO Product(name)
SELECT DISTINCT prodName
FROM Purchase
WHERE prodName NOT IN (SELECT name FROM Product)
name listPrice category
gizmo 100 Gadgets
camera - -
Insertion: an Example
INSERT INTO Product(name, listPrice)
SELECT DISTINCT prodName, price
FROM Purchase
WHERE prodName NOT IN (SELECT name FROM Product)
name listPrice category
gizmo 100 Gadgets
camera 200 -
camera ?? 225 ?? -
Depends on the implementation
Deletions
DELETE FROM PURCHASE
WHERE seller = ‘Joe’ AND
product = ‘Brooklyn Bridge’
Factoid about SQL: there is no way to delete only a single
occurrence of a tuple that appears twice
in a relation.
Example:
Updates
UPDATE PRODUCT
SET price = price/2
WHERE Product.name IN
(SELECT product
FROM Purchase
WHERE Date =‘Oct, 25, 1999’);
Example:
Database Management System
Advanced Training
Practices in 2017
Prepared by: Moutasm Tamimi
Using SQL language
Microsoft SQL Server Management Studio
Versions (2008-2010-2012-2014)

More Related Content

What's hot (20)

PPTX
Dbms normalization
Pratik Devmurari
 
PPTX
List and Dictionary in python
Sangita Panchal
 
PPTX
Integrity Constraints
madhav bansal
 
PPTX
Introduction to Database
Siti Ismail
 
PPTX
SQL commands
GirdharRatne
 
PPTX
Introduction to database
Arpee Callejo
 
PPT
Multivalued dependency
avniS
 
PPTX
database recovery techniques
Kalhan Liyanage
 
PPTX
SQL Join Basic
Naimul Arif
 
PPT
Unit01 dbms
arnold 7490
 
PPT
Databases: Normalisation
Damian T. Gordon
 
PPTX
serializability in dbms
Saranya Natarajan
 
PPTX
DATABASE CONSTRAINTS
sunanditaAnand
 
PPT
Database Relationships
wmassie
 
PPTX
Software-Development-Cycle-SDLC and its phases.pptx
mprakaash5
 
PPTX
Relational model
Dabbal Singh Mahara
 
PDF
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
PPTX
Relational Database Design
Archit Saxena
 
PPTX
Database fundamentals(database)
welcometofacebook
 
PPTX
Normalisation and anomalies
baabtra.com - No. 1 supplier of quality freshers
 
Dbms normalization
Pratik Devmurari
 
List and Dictionary in python
Sangita Panchal
 
Integrity Constraints
madhav bansal
 
Introduction to Database
Siti Ismail
 
SQL commands
GirdharRatne
 
Introduction to database
Arpee Callejo
 
Multivalued dependency
avniS
 
database recovery techniques
Kalhan Liyanage
 
SQL Join Basic
Naimul Arif
 
Unit01 dbms
arnold 7490
 
Databases: Normalisation
Damian T. Gordon
 
serializability in dbms
Saranya Natarajan
 
DATABASE CONSTRAINTS
sunanditaAnand
 
Database Relationships
wmassie
 
Software-Development-Cycle-SDLC and its phases.pptx
mprakaash5
 
Relational model
Dabbal Singh Mahara
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
Relational Database Design
Archit Saxena
 
Database fundamentals(database)
welcometofacebook
 

Similar to Database Management System - SQL Advanced Training (20)

PDF
Lecture_4_SQL_Adv_II_hi_HIHdkdjdjdjdjjjj
uuganbayrexe
 
PPTX
1. dml select statement reterive data
Amrit Kaur
 
PPTX
Sql Tuning
MuhammadAbdulRehmanQ
 
PDF
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
HITIKAJAIN4
 
PDF
Dynamic websites lec2
Belal Arfa
 
PPTX
More Complex SQL and Concurrency ControlModule 4.pptx
bgscseise
 
PDF
Structured query language(sql)
Huda Alameen
 
PPTX
SQL BASICS.pptx
JEEVA R
 
PDF
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 
PPTX
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
PPTX
Data
RithikRaj25
 
PPT
Mssql
Janas Khan
 
PPTX
Server Query Language – Getting Started.pptx
auzee32
 
PPTX
Sql server
Fajar Baskoro
 
PPTX
OracleSQLraining.pptx
Rajendra Jain
 
PPTX
DBMS: Week 07 - Advanced SQL Queries in MySQL
RashidFaridChishti
 
PPT
Sql server introduction to sql server
Vibrant Technologies & Computers
 
PPT
MS SQL Server.ppt
QuyVo27
 
PPT
lecture2.ppt
ImXaib
 
Lecture_4_SQL_Adv_II_hi_HIHdkdjdjdjdjjjj
uuganbayrexe
 
1. dml select statement reterive data
Amrit Kaur
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
HITIKAJAIN4
 
Dynamic websites lec2
Belal Arfa
 
More Complex SQL and Concurrency ControlModule 4.pptx
bgscseise
 
Structured query language(sql)
Huda Alameen
 
SQL BASICS.pptx
JEEVA R
 
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Mssql
Janas Khan
 
Server Query Language – Getting Started.pptx
auzee32
 
Sql server
Fajar Baskoro
 
OracleSQLraining.pptx
Rajendra Jain
 
DBMS: Week 07 - Advanced SQL Queries in MySQL
RashidFaridChishti
 
Sql server introduction to sql server
Vibrant Technologies & Computers
 
MS SQL Server.ppt
QuyVo27
 
lecture2.ppt
ImXaib
 
Ad

More from Moutasm Tamimi (17)

PPTX
Software Quality Assessment Practices
Moutasm Tamimi
 
PPTX
Reengineering PDF-Based Documents Targeting Complex Software Specifications
Moutasm Tamimi
 
PPTX
Software Evolution and Maintenance Models
Moutasm Tamimi
 
PPTX
Software evolution and maintenance basic concepts and preliminaries
Moutasm Tamimi
 
PPTX
An integrated security testing framework and tool
Moutasm Tamimi
 
PPTX
Critical Success Factors along ERP life-cycle in Small medium enterprises
Moutasm Tamimi
 
PPTX
Critical Success Factors (CSFs) In International ERP Implementations with que...
Moutasm Tamimi
 
PPTX
Best Practices For Business Analyst - Part 3
Moutasm Tamimi
 
PPTX
Concepts Of business analyst Practices - Part 1
Moutasm Tamimi
 
PPTX
Recovery in Multi database Systems
Moutasm Tamimi
 
PPTX
Software Quality Models: A Comparative Study paper
Moutasm Tamimi
 
PPTX
ISO 29110 Software Quality Model For Software SMEs
Moutasm Tamimi
 
PPTX
Windows form application - C# Training
Moutasm Tamimi
 
PPTX
Asp.net Programming Training (Web design, Web development)
Moutasm Tamimi
 
PPTX
Database Management System - SQL beginner Training
Moutasm Tamimi
 
PPTX
Measurement and Quality in Object-Oriented Design
Moutasm Tamimi
 
PPTX
SQL Injection and Clickjacking Attack in Web security
Moutasm Tamimi
 
Software Quality Assessment Practices
Moutasm Tamimi
 
Reengineering PDF-Based Documents Targeting Complex Software Specifications
Moutasm Tamimi
 
Software Evolution and Maintenance Models
Moutasm Tamimi
 
Software evolution and maintenance basic concepts and preliminaries
Moutasm Tamimi
 
An integrated security testing framework and tool
Moutasm Tamimi
 
Critical Success Factors along ERP life-cycle in Small medium enterprises
Moutasm Tamimi
 
Critical Success Factors (CSFs) In International ERP Implementations with que...
Moutasm Tamimi
 
Best Practices For Business Analyst - Part 3
Moutasm Tamimi
 
Concepts Of business analyst Practices - Part 1
Moutasm Tamimi
 
Recovery in Multi database Systems
Moutasm Tamimi
 
Software Quality Models: A Comparative Study paper
Moutasm Tamimi
 
ISO 29110 Software Quality Model For Software SMEs
Moutasm Tamimi
 
Windows form application - C# Training
Moutasm Tamimi
 
Asp.net Programming Training (Web design, Web development)
Moutasm Tamimi
 
Database Management System - SQL beginner Training
Moutasm Tamimi
 
Measurement and Quality in Object-Oriented Design
Moutasm Tamimi
 
SQL Injection and Clickjacking Attack in Web security
Moutasm Tamimi
 
Ad

Recently uploaded (20)

DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 

Database Management System - SQL Advanced Training

  • 1. Database Management System Advanced Training Practices in 2017 Prepared by: Moutasm Tamimi Using SQL language Microsoft SQL Server Management Studio Versions (2008-2010-2012-2014)
  • 2. Speaker Information  Moutasm tamimi Independent consultant , IT Researcher , CEO at ITG7 Instructor of: Project Development. DBMS. .NET applications. Digital marketing. Email: [email protected] LinkedIn: click here.
  • 4. Joins PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Product Company Cname StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 Japan PName Price SingleTouch $149.99 SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200
  • 5. More Joins Product (pname, price, category, manufacturer) Company (cname, stockPrice, country) Find all Chinese companies that manufacture products both in the ‘electronic’ and ‘toy’ categories SELECT cname FROM WHERE
  • 6. Cross Join There are two tables A and B A has a column Id and data (1,2,3) B has a column Id and data (A,B) If I put Select A.Id, B.Id from A,B This generates output as A 1 B 1 C 1 A 2 B 2 C 2
  • 7. Self Join There is a table called Emp with the following structure: empid ename mgrid 1 A null 2 B 1 3 C 1 4 D 2 If I want to print all managers using self join, I should write quey as: select e1.ename from emp e1,emp e2 where e1.mgrid = e2.empid
  • 8. Inner Join I have 2 tables Student(sid,Name) and Marks(Sid,Subject,Score) If I want to print the marks of all students in the following format, Name Subject Score Select Name,Subject,Score from Student s join Marks m On s.sid = m.sid
  • 9. Outer Join  Right outer Join  Print all the records in the second table with null values for missing records in the first table  Left outer Join  Print all the records in the first table with null values for missing records in the second table  Full outer Join  Prints all records in both the table with null values for missing records in both the table
  • 10. Left Outer Join I have a table Employee (Eid, Ename, Mid) and a table Machine (Mid,ManufacturerName) Employee EidEName Mid 1 ABC1 2 DEF 3 Machine Mid ManufacturerName 1 Zenith 2 HP
  • 11. Left Outer Join I want to print the employee name and machine name. If I write a query using inner join, then the second employee will not be displayed as the mid in his record is not avilable with the second table. So I go for left outer join. The query is as shown below: Select Ename, ManufacturerName from Employee e left outer join Machine m on e.Mid = m.Mid
  • 12. Right outer Join Assume data in the tables like this: Employee EidEName Mid 1 ABC1 2 DEF Machine Mid ManufacturerName 1 Zenith 2 HP
  • 13. Right Outer Join If I want to find which machine is unallocated, I can use right outer join. The query is as follows: Select Ename, ManufacturerName from Employee e right outer join Machine m on e.Mid = m.Mid This yields a result ABCZenith HP
  • 14. Full Outer Join Assume data in the tables like this: Employee EidEName Mid 1 ABC 1 2 DEF 3 GHI 2 Machine Mid ManufacturerName 1 Zenith 2 HP 3 Compaq
  • 15. Full Outer Join If I want to find people who have been un allocated with a system and machines that are been un allocated, I can go for full outer join. Query is like this: Select Ename, ManufacturerName from Employee e full outer join Machine m on e.Mid = m.Mid This yields a result ABC Zenith DEF GHIHP Compaq
  • 16. Tuple Variables SELECT DISTINCT pname, address FROM Person, Company WHERE worksfor = cname Which address ? Person(pname, address, worksfor) Company(cname, address) SELECT DISTINCT Person.pname, Company.address FROM Person, Company WHERE Person.worksfor = Company.cname SELECT DISTINCT x.pname, y.address FROM Person AS x, Company AS y WHERE x.worksfor = y.cname
  • 17. SQL Functions  SQL Avg()  SQL Count()  SQL First()  SQL Last()  SQL Max()  SQL Min()  SQL Sum()  SQL Group By • SQL HavingSQL Ucase() • SQL Lcase() • SQL Mid() • SQL Len() • SQL Round() • SQL Now() • SQL Format()
  • 18. Aggregation SELECT count(*) FROM Product WHERE year > 1995 Except count, all aggregations apply to a single attribute SELECT avg(price) FROM Product WHERE maker=“Toyota” SQL supports several aggregation operations: sum, count, min, max, avg
  • 19. COUNT applies to duplicates, unless otherwise stated: SELECT Count(category) FROM Product WHERE year > 1995 same as Count(*) We probably want: SELECT Count(DISTINCT category) FROM Product WHERE year > 1995 Aggregation: Count
  • 20. Purchase(product, date, price, quantity) More Examples SELECT Sum(price * quantity) FROM Purchase SELECT Sum(price * quantity) FROM Purchase WHERE product = ‘bagel’ What do they mean ?
  • 21. Simple Aggregations Purchase Product Date Price Quantity Bagel 10/21 1 20 Banana 10/3 0.5 10 Banana 10/10 1 10 Bagel 10/25 1.50 20 SELECT Sum(price * quantity) FROM Purchase WHERE product = ‘bagel’ 50 (= 20+30)
  • 22. Grouping and Aggregation Purchase(product, date, price, quantity) SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product Let’s see what this means… Find total sales after 10/1/2005 per product.
  • 23. Grouping and Aggregation 1. Compute the FROM and WHERE clauses. 2. Group by the attributes in the GROUPBY 3. Compute the SELECT clause: grouped attributes and aggregates.
  • 24. 1&2. FROM-WHERE-GROUPBY Product Date Price Quantity Bagel 10/21 1 20 Bagel 10/25 1.50 20 Banana 10/3 0.5 10 Banana 10/10 1 10
  • 25. 3. SELECT SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product Product Date Price Quantity Bagel 10/21 1 20 Bagel 10/25 1.50 20 Banana 10/3 0.5 10 Banana 10/10 1 10 Product TotalSales Bagel 50 Banana 15
  • 26. GROUP BY v.s. Nested Quereis SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT DISTINCT x.product, (SELECT Sum(y.price*y.quantity) FROM Purchase y WHERE x.product = y.product AND y.date > ‘10/1/2005’) AS TotalSales FROM Purchase x WHERE x.date > ‘10/1/2005’
  • 27. Another Example SELECT product, sum(price * quantity) AS SumSales max(quantity) AS MaxQuantity FROM Purchase GROUP BY product What does it mean ?
  • 28. HAVING Clause SELECT product, Sum(price * quantity) FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product HAVING Sum(quantity) > 30 Same query, except that we consider only products that had at least 100 buyers. HAVING clause contains conditions on aggregates.
  • 29. General form of Grouping and Aggregation SELECT S FROM R1,…,Rn WHERE C1 GROUP BY a1,…,ak HAVING C2 S = may contain attributes a1,…,ak and/or any aggregates but NO OTHER ATTRIBUTES C1 = is any condition on the attributes in R1,…,Rn C2 = is any condition on aggregate expressions Why ?
  • 30. General form of Grouping and Aggregation Evaluation steps: 1. Evaluate FROM-WHERE, apply condition C1 2. Group by the attributes a1,…,ak 3. Apply condition C2 to each group (may have aggregates) 4. Compute aggregates in S and return the result SELECT S FROM R1,…,Rn WHERE C1 GROUP BY a1,…,ak HAVING C2
  • 31. Advanced SQLizing 1. Getting around INTERSECT and EXCEPT 2. Quantifiers 3. Aggregation v.s. subqueries
  • 32. 1. INTERSECT and EXCEPT: (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) SELECT R.A, R.B FROM R WHERE EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) (SELECT R.A, R.B FROM R) EXCEPT (SELECT S.A, S.B FROM S) SELECT R.A, R.B FROM R WHERE NOT EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) If R, S have no duplicates, then can write without subqueries (HOW ?) INTERSECT and EXCEPT: not in SQL Server
  • 33. 2. Quantifiers Product ( pname, price, company) Company( cname, city) Find all companies that make some products with price < 100 SELECT DISTINCT Company.cname FROM Company, Product WHERE Company.cname = Product.company and Product.price < 100 Existential: easy ! 
  • 34. 2. Quantifiers Product ( pname, price, company) Company( cname, city) Find all companies s.t. all of their products have price < 100 Universal: hard !  Find all companies that make only products with price < 100 same as:
  • 35. 2. Quantifiers 2. Find all companies s.t. all their products have price < 100 1. Find the other companies: i.e. s.t. some product  100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname IN (SELECT Product.company FROM Product WHERE Produc.price >= 100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname NOT IN (SELECT Product.company FROM Product WHERE Produc.price >= 100
  • 36. 3. Group-by v.s. Nested Query  Find authors who wrote  10 documents:  Attempt 1: with nested queries SELECT DISTINCT Author.name FROM Author WHERE count(SELECT Wrote.url FROM Wrote WHERE Author.login=Wrote.login) > 10 This is SQL by a novice Author(login,name) Wrote(login,url)
  • 37. 3. Group-by v.s. Nested Query  Find all authors who wrote at least 10 documents:  Attempt 2: SQL style (with GROUP BY) SELECT Author.name FROM Author, Wrote WHERE Author.login=Wrote.login GROUP BY Author.name HAVING count(wrote.url) > 10 This is SQL by an expert No need for DISTINCT: automatically from GROUP BY
  • 38. 3. Group-by v.s. Nested Query Find authors with vocabulary  10000 words: SELECT Author.name FROM Author, Wrote, Mentions WHERE Author.login=Wrote.login AND Wrote.url=Mentions.url GROUP BY Author.name HAVING count(distinct Mentions.word) > 10000 Author(login,name) Wrote(login,url) Mentions(url,word)
  • 39. Two Examples Store(sid, sname) Product(pid, pname, price, sid) Find all stores that sell only products with price > 100 same as: Find all stores s.t. all their products have price > 100)
  • 40. SELECT Store.name FROM Store, Product WHERE Store.sid = Product.sid GROUP BY Store.sid, Store.name HAVING 100 < min(Product.price) SELECT Store.name FROM Store WHERE Store.sid NOT IN (SELECT Product.sid FROM Product WHERE Product.price <= 100) SELECT Store.name FROM Store WHERE 100 < ALL (SELECT Product.price FROM product WHERE Store.sid = Product.sid) Almost equivalent… Why both ?
  • 41. Two Examples Store(sid, sname) Product(pid, pname, price, sid) For each store, find its most expensive product
  • 42. Two Examples SELECT Store.sname, max(Product.price) FROM Store, Product WHERE Store.sid = Product.sid GROUP BY Store.sid, Store.sname SELECT Store.sname, x.pname FROM Store, Product x WHERE Store.sid = x.sid and x.price >= ALL (SELECT y.price FROM Product y WHERE Store.sid = y.sid) This is easy but doesn’t do what we want: Better: But may return multiple product names per store
  • 43. Two Examples SELECT Store.sname, max(x.pname) FROM Store, Product x WHERE Store.sid = x.sid and x.price >= ALL (SELECT y.price FROM Product y WHERE Store.sid = y.sid) GROUP BY Store.sname Finally, choose some pid arbitrarily, if there are many with highest price:
  • 44. NULLS in SQL  Whenever we don’t have a value, we can put a NULL  Can mean many things:  Value does not exists  Value exists but is unknown  Value not applicable  Etc.  The schema specifies for each attribute if can be null (nullable attribute) or not  How does SQL cope with tables that have NULLs ?
  • 45. Null Values  If x= NULL then 4*(3-x)/7 is still NULL  If x= NULL then x=“Joe” is UNKNOWN  In SQL there are three boolean values: FALSE = 0 UNKNOWN = 0.5 TRUE = 1
  • 46. Null Values  C1 AND C2 = min(C1, C2)  C1 OR C2 = max(C1, C2)  NOT C1 = 1 – C1 Rule in SQL: include only tuples that yield TRUE SELECT * FROM Person WHERE (age < 25) AND (height > 6 OR weight > 190) E.g. age=20 heigth=NULL weight=200
  • 47. Null Values Unexpected behavior: Some Persons are not included ! SELECT * FROM Person WHERE age < 25 OR age >= 25
  • 48. Null Values Can test for NULL explicitly:  x IS NULL  x IS NOT NULL Now it includes all Persons SELECT * FROM Person WHERE age < 25 OR age >= 25 OR age IS NULL
  • 49. Outerjoins Explicit joins in SQL = “inner joins”: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product, Purchase WHERE Product.name = Purchase.prodName Same as: But Products that never sold will be lost !
  • 50. Outerjoins Left outer joins in SQL: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName
  • 51. Name Category Gizmo gadget Camera Photo OneClick Photo ProdName Store Gizmo Wiz Camera Ritz Camera Wiz Name Store Gizmo Wiz Camera Ritz Camera Wiz OneClick NULL Product Purchase
  • 52. Application Compute, for each product, the total number of sales in ‘September’ Product(name, category) Purchase(prodName, month, store) SELECT Product.name, count(*) FROM Product, Purchase WHERE Product.name = Purchase.prodName and Purchase.month = ‘September’ GROUP BY Product.name What’s wrong ?
  • 53. Application Compute, for each product, the total number of sales in ‘September’ Product(name, category) Purchase(prodName, month, store) SELECT Product.name, count(*) FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName and Purchase.month = ‘September’ GROUP BY Product.name Now we also get the products who sold in 0 quantity
  • 54. Outer Joins  Left outer join:  Include the left tuple even if there’s no match  Right outer join:  Include the right tuple even if there’s no match  Full outer join:  Include the both left and right tuples even if there’s no match
  • 55. Modifying the Database Three kinds of modifications  Insertions  Deletions  Updates Sometimes they are all called “updates”
  • 56. Insertions General form: Missing attribute  NULL. May drop attribute names if give them in order. INSERT INTO R(A1,…., An) VALUES (v1,…., vn) INSERT INTO Purchase(buyer, seller, product, store) VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’, ‘The Sharper Image’) Example: Insert a new purchase to the database:
  • 57. Insertions INSERT INTO PRODUCT(name) SELECT DISTINCT Purchase.product FROM Purchase WHERE Purchase.date > “10/26/01” The query replaces the VALUES keyword. Here we insert many tuples into PRODUCT
  • 58. Insertion: an Example prodName is foreign key in Product.name Suppose database got corrupted and we need to fix it: name listPrice category gizmo 100 gadgets prodName buyerName price camera John 200 gizmo Smith 80 camera Smith 225 Task: insert in Product all prodNames from Purchase Product Product(name, listPrice, category) Purchase(prodName, buyerName, price) Purchase
  • 59. Insertion: an Example INSERT INTO Product(name) SELECT DISTINCT prodName FROM Purchase WHERE prodName NOT IN (SELECT name FROM Product) name listPrice category gizmo 100 Gadgets camera - -
  • 60. Insertion: an Example INSERT INTO Product(name, listPrice) SELECT DISTINCT prodName, price FROM Purchase WHERE prodName NOT IN (SELECT name FROM Product) name listPrice category gizmo 100 Gadgets camera 200 - camera ?? 225 ?? - Depends on the implementation
  • 61. Deletions DELETE FROM PURCHASE WHERE seller = ‘Joe’ AND product = ‘Brooklyn Bridge’ Factoid about SQL: there is no way to delete only a single occurrence of a tuple that appears twice in a relation. Example:
  • 62. Updates UPDATE PRODUCT SET price = price/2 WHERE Product.name IN (SELECT product FROM Purchase WHERE Date =‘Oct, 25, 1999’); Example:
  • 63. Database Management System Advanced Training Practices in 2017 Prepared by: Moutasm Tamimi Using SQL language Microsoft SQL Server Management Studio Versions (2008-2010-2012-2014)