Professional Documents
Culture Documents
Bcis5420 - Lecture Note - ch3 - Multi Table Queries
Bcis5420 - Lecture Note - ch3 - Multi Table Queries
Bcis5420 - Lecture Note - ch3 - Multi Table Queries
Chapter 3
Section. 1
Subquery
Ch.3. Multi Table Queries
Subquery
• Query in query
• One query sentence belongs to another sentence
• Each query sentence has its SELECT and FROM operators
• Connecting multiple queries using parentheses
Subquery with IN
• Retrieve data from one table based on data from another table(s)
• Use IN to find values of rows with matching ID’s or defined values
• Query within query, working as below;
customer_id name
SELECT customer_id, name CID1 A
FROM Customers CID2 B
WHERE customer_id CID3 C
IN (SELECT customer_id FROM Orders) CID4 D
Ch.3. Multi Table Queries
• In the SQL sever, identifying customers who have placed at least one
order (similar to the example on the previous slide);
SELECT *
FROM AdventureWorks.Person.Person
WHERE BusinessEntityID
IN (SELECT BusinessEntityID
FROM AdventureWorks.Person.PersonPhone
WHERE PhoneNumberTypeID <3)
Ch.3. Multi Table Queries
customer_id name
SELECT customer_id, name CID1 A
FROM Customers CID2 B
WHERE EXISTS (SELECT customer_id FROM Orders CID3 C
WHERE Orders.customer_id = Customers.customer_id)
CID4 D
Ch.3. Multi Table Queries
• In the SQL server, identifying customer information for those with orders
• Use NOT EXIST to rows without matching ID’s, regardless if the rows
have values
• In the SQL server, identifying products with list price greater than
or equal to average list price of any product subcategory
• In the SQL server, identifying currency rate ID and their from and
to currency code, where currency rate ID is larger than 10
Section. 2
JOIN
Ch.3. Multi Table Queries
JOIN
• A relational operation that causes two or more tables with a common domain to
be combined into a single table
• Specifying the column from each table to be used for the join, using a foreign
key from one table and its associated key in the other table (i.e., matching keys)
• According to approaches, categorized into;
▪ INNER JOIN
▪ OUTER JOIN
Ch.3. Multi Table Queries
INNER JOIN
• Show individual’s first and last name along with address ID and address ID type
SELECT
Person.Person.BusinessEntityID, Person.Person.FirstName, Person.Person.LastName,
Person.BusinessEntityAddress.AddressID, Person.BusinessEntityAddress.AddressTypeID
FROM Person.Person INNER JOIN Person.BusinessEntityAddress
ON (Person.Person.BusinessEntityID = Person.BusinessEntityAddress.BusinessEntityID)
Ch.3. Multi Table Queries
• Find product and supplier information for any combination of parts supplied by a
company where price of product is more than $10 (DB: AdventureWorks)
SELECT
Purchasing.ProductVendor.ProductID, Purchasing.Vendor.BusinessEntityID,
Purchasing.Vendor.Name
FROM Purchasing.ProductVendor INNER JOIN Purchasing.Vendor
ON (Purchasing.ProductVendor.BusinessEntityID = Purchasing.Vendor.BusinessEntityID)
WHERE Purchasing.ProductVendor.StandardPrice > $10
Ch.3. Multi Table Queries
Assuming ProductID, Name, and StandardPrice exist only one of the two tables,
will the two queries below produce the same result?
ON (Purchasing.ProductVendor.BusinessEntityID = Purchasing.Vendor.BusinessEntityID)
2022-10-11 A 20 2022-10-11 A 25
2022-10-11 B 50 2022-10-11 B 40
2022-10-11 C 40 2022-10-11 C 55
2022-10-12 A 25 2022-10-11 D 60
2022-10-12 B 45 2022-10-11 E 75
Ch.3. Multi Table Queries
• Show order id, order detail id, product id, special offer id, and modified date
SELECT
sod.SalesOrderID, sod.SalesOrderDetailID, so.ProductID, so.SpecialOfferID, so.ModifiedDate
FROM Sales.SalesOrderDetail AS sod INNER JOIN Sales.SpecialOfferProduct AS so
ON so.ProductID = sod.ProductID AND so.SpecialOfferID = sod.SpecialOfferID
Ch.3. Multi Table Queries
OUTER JOIN
Sales.Customer Sales.SalesOrderHeader
Ch.3. Multi Table Queries
Sales.Customer Sales.SalesOrderHeader
Ch.3. Multi Table Queries
References
• The major contents of this note are reproduced from the textbook of BCIS 5420;
Topi et al. Modern Database Management. 13 th edition. Pearson's, 2019
• Unless having a specific reference source, the photos and icons used in this
material are from the following sources providing copyright free images:
imagesource.com, iconfinder.com, and pexels.com