Docsity

SQL Querying and Data Retrieval: Concatenating Columns, Grouping, Subqueries, and Joins, Study notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
CSIS 350 โ€“ Lecture 5
Read: Chapter 3, pages 69 โ€“ 81.
Turn in homework
More SQL
Sometimes you'd like the output in textual format instead of columns. You can
concatenate columns โ€“ unfortunately, the syntax is not consistent between DBMSs:
mySQL โ€“ use CONCAT function
SQLServer โ€“ use +
Example:
Select concat( buyer, ' is in the ', department, ' department' ) AS 'Buyer In'
from SKU_Data;
Select buyer + ' is in the ', department, ' department' AS 'Buyer In' from SKU_Data;
GROUP BY with WHERE,
HAVING clause
where clause โ€“ filters raw data (before grouping)
having clause โ€“ filters group data (after grouping)
select orderNumber, sum( extendedPrice )
from order_item
group by orderNumber;
select orderNumber, sum( extendedPrice )
from order_item
where orderNumber != 1000
group by orderNumber;
select orderNumber, sum( extendedPrice )
from order_item
group by orderNumber
having sum( extendedPrice ) > 500;
Note: To be safe, put WHERE before GROUP BY. Not all DBMSs require this, but some
do.
select orderNumber, sum( extendedPrice )
from order_item
pf3
pf4
pf5

Partial preview of the text

Download SQL Querying and Data Retrieval: Concatenating Columns, Grouping, Subqueries, and Joins and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

CSIS 350 โ€“ Lecture 5

Read: Chapter 3, pages 69 โ€“ 81. Turn in homework More SQL Sometimes you'd like the output in textual format instead of columns. You can concatenate columns โ€“ unfortunately, the syntax is not consistent between DBMSs: mySQL โ€“ use CONCAT function SQLServer โ€“ use + Example: Select concat( buyer, ' is in the ', department, ' department' ) AS 'Buyer In' from SKU_Data; Select buyer + ' is in the ', department, ' department' AS 'Buyer In' from SKU_Data; GROUP BY with WHERE, HAVING clause where clause โ€“ filters raw data (before grouping) having clause โ€“ filters group data (after grouping) select orderNumber, sum( extendedPrice ) from order_item group by orderNumber; select orderNumber, sum( extendedPrice ) from order_item where orderNumber != 1000 group by orderNumber; select orderNumber, sum( extendedPrice ) from order_item group by orderNumber having sum( extendedPrice ) > 500; Note: To be safe, put WHERE before GROUP BY. Not all DBMSs require this, but some do. select orderNumber, sum( extendedPrice ) from order_item

where extendedPrice > 50 group by orderNumber having sum( extendedPrice ) > 500 SQL applies the WHERE criteria before the group criteria โ€“ this is to avoid ambiguity in the statements. Subqueries Up to this point, all of our queries have operated on a single table. We've been able to derive a lot of useful information this way, but by using multiple tables, we can derive a lot more. Suppose we want to look at the order details for the order that contains SKU 200100? Well, first we could use a query to look up the order that contains this SKU: select orderNumber from order_item where SKU = 200100; And then we could use that number in another query: select * from retail_order where orderNumber = 1000; But, this isn't very nice. We'd like to be able to do this in one step. We can! Use a subquery: select * from retail_order where orderNumber = ( select orderNumber from order_item where SKU = 200100 ); A subquery is a Select statement that is nested inside of another select statement. What if we are interested in the order containing SKU 302000? [Repeat above, using this number. What happens? How can we get around this problem?]. Use IN. select * from retail_order where orderNumber IN ( select orderNumber from order_item where SKU = 302000 );

from <table1, table2> where table1.column = table2.column; select from JOIN ON table1.column = table2.column; Note that the operations does not have to be '='. Let's look at some examples:

  1. Show the product names of products that have been ordered select sku_description, orderNumber from sku_data, order_item where sku_data.sku = order_item.sku; select sku_description, orderNumber from sku_data join order_item on sku_data.sku = order_item.sku;
  2. Show the OrderNumber, StoreID, SKUs and ExtendedPrice for all items that have been ordered: select orderNumber, storeNumber, sku, extendedPrice from retail_order JOIN order_item on retail_order.orderNumber = order_item.orderNumber Note : The above query fails, because orderNumber is ambiguous โ€“ need to specify which orderNumber. Discuss use of aliases: select r.orderNumber, storeNumber, sku, extendedPrice from retail_order r JOIN order_item oi on r.orderNumber = oi.orderNumber; Don't have to type out table name every time. Makes queries shorter. Aliases get used in other places also. Discuss Cross product style vs inner join style. Advantages of Join syntax:
  3. It clarifies which expressions are for joining tables and which are for restricting the rows retrieved in the Where clause. select * from SKU_Data s, order_item oi

where s.sku = oi.sku and ( buyer in ('Mr. Spock', 'Deanna Troi' ) OR department = 'Lawn Sports'); select * from SKU_Data s JOIN order_item oi ON s.sku = oi.sku WHERE ( buyer in ('Mr. Spock', 'Deanna Troi' ) OR department = 'Lawn Sports'); Note that in above queries, SKU appears twice in the result table.

  1. It supports outer joins. Sometimes, we want to include rows that would be excluded because there is no corresponding row in one of the tables. For example, maybe we want to look at all the products, including the ones that have not been ordered. select sku_description, orderNumber from sku_data LEFT join order_item on sku_data.sku = order_item.sku; Joining 3 tables: Let's say that we want to determine the names of the products that have been sold in every store. Need to have storeID, the orderNumber, and the SKU_description. What tables are these from? Join form: select storeNumber, ro.orderNumber, sku_Description from retail_order ro join order_item oi on ro.orderNumber = oi.orderNumber join sku_data s on oi.sku = s.sku order by storeNumber; Cross-product form: select storeNumber, ro.orderNumber, sku_Description -> from retail_order ro, sku_data s, order_item oi where ro.orderNumber = oi.orderNumber AND oi.sku = s.sku order by storeNumber; Note use of aliases here. In-Class assignment : Handout (based on Starships DB) Homework: On course web site, due Thusday.