SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1
Select: syntax SELECT [ DISTINCT | ALL ]{ * | column_list } FROM [owner.]table_name [alias] [,[owner.]table_name [alias]]... [WHERE condition] [GROUP BY column_list] [HAVING condition] [ORDER BY column_list]; Can accomplish the three relational operations: selection, projection, and join. The order of the clauses in the SELECT statement cannot be changed. The only required clauses are SELECT and FROM. D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 2
The GROUP BY clause Defines groups of output rows to which aggregate functions can be applied. There are restriction for the SELECT list, when GROUP BY is used: this list MUST include only fields that have a single value per group (only fields mentioned in GROUP BY clause or aggregate functions). In many dialects GROUP BY also order the result according to specified fields, applying the same rules as for ORDER BY. WHERE clause is applied first than groups are formed according to the remaining rows. D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 3
The GROUP BY clause (cont.) The 1989’ standard allows a single level grouping. The 1992’s introduces multi-level grouping (or more than one field can be used; the mechanism of major and minor keys is used). D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 4
The HAVING clause Just as the WHERE clause defines a filter, but on the aggregate group values. Only column names used in clause HAVING may appear in GROUP BY list OR aggregate functions. In general, the expressions must be single valued per group. D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 5
The HAVING clause (cont.) HAVING and WHERE can be used interchangeable if HAVING does not include aggregate functions. HAVING and WHERE cannot be used interchangeable, when: the filter-condition includes aggregate functions; the filter-condition deals with individual rows rather than groups of rows; combined WHERE and HAVING are required. D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 6
Group by: illustration cust_idquantity AAA10 BBB20 AAA30 CCC40 BBB50 AAA60 AAA70 EEE20 CCC40 BBB60 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 7 cust_idquantity AAA10 AAA30 AAA60 AAA70 BBB20 BBB50 BBB60 CCC40 CCC40 EEE20 SELECT cust_id, SUM(quantity) AS QNTY FROM orders GROUP BY cust_id cust_idQNTY AAA170 BBB130 CCC80 EEE20
Group by: illustration D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 8 cust_idQNTY AAA170 BBB130 CCC80 EEE20 SELECT Cust_id, SUM(quantity) AS QNTY FROM Orders GROUP BY Cust_id HAVING SUM(quantity) > 100 cust_idQNTY AAA170 BBB130
Examples: Table CSupplier (1) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD P X15.00VKVolinV Stoka X15.00JGJohnJ Good X26.00DGCDimitarD P X26.00VKVolinV Stoka X37.00DGCDimitarD P X37.00JGJohnJ Good D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 9 Write SQL SELECT statement to calculate the income (sum(Dprice*DQnty) ) received from every dealer.
Examples: Table CSupplier (2) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD P X15.00VKVolinV Stoka X15.00JGJohnJ Good X26.00DGCDimitarD P X26.00VKVolinV Stoka X37.00DGCDimitarD P X37.00JGJohnJ Good D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 10 Write SQL SELECT statement to calculate the income (sum(Dprice*DQnty) ) received every day.
Examples: Table CSupplier (3) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD P X15.00VKVolinV Stoka X15.00JGJohnJ Good X26.00DGCDimitarD P X26.00VKVolinV Stoka X37.00DGCDimitarD P X37.00JGJohnJ Good D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 11 Write SQL SELECT statement to calculate the income (sum(Dprice*DQnty) ) received for every product.
Examples: Table CSupplier (4) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD P X15.00VKVolinV Stoka X15.00JGJohnJ Good X26.00DGCDimitarD P X26.00VKVolinV Stoka X37.00DGCDimitarD P X37.00JGJohnJ Good D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 12 What the following query will print out: SELECT Code, DDate, SUM(DPrice*DQnty) AS Money FROM CSupplier WHERE Dealer <> ‘jg’ GROUP BY Code, DDate;
Examples: Table CSupplier (5) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD P X15.00VKVolinV Stoka X15.00JGJohnJ Good X26.00DGCDimitarD P X26.00VKVolinV Stoka X37.00DGCDimitarD P X37.00JGJohnJ Good D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 13 What the following query will print out: SELECT Dealer, Dcode, SUM(DPrice*DQnty) AS Money FROM CSupplier GROUP BY Dealer, Dcode HAVING Money < 5.00;
Examples: Table CSupplier (6) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD P X15.00VKVolinV Stoka X15.00JGJohnJ Good X26.00DGCDimitarD P X26.00VKVolinV Stoka X37.00DGCDimitarD P X37.00JGJohnJ Good D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 14 What the following query will print out: SELECT Code, Dcode, SUM(DPrice*DQnty) AS Money FROM CSupplier WHERE Dname <> ‘John’ GROUP BY Code, Dcode;
Examples: Table HomeDeliveryPizzas (1) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice Tomato sauceM DGCSofia CheeseSm DGCSofia DressingL DGCSofia Tomato sauceM VKAUBG CheeseSm1.00 VKAUBG HamKg JGScapto SausageM JGScapto6.00 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 15 Write SQL SELECT statement to evaluate the taste of consumers. Tip: “taste” is the combinations of pizza (Pizza_id) and ingredient (Ingr_id) and you have to count them.
Examples: Table HomeDeliveryPizzas (2) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice Tomato sauceM DGCSofia CheeseSm DGCSofia DressingL DGCSofia Tomato sauceM VKAUBG CheeseSm1.00 VKAUBG HamKg JGScapto SausageM JGScapto6.00 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 16 Write SQL SELECT statement to evaluate the profitability by address.
Examples: Table HomeDeliveryPizzas (3) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice Tomato sauceM DGCSofia CheeseSm DGCSofia DressingL DGCSofia Tomato sauceM VKAUBG CheeseSm1.00 VKAUBG HamKg JGScapto SausageM JGScapto6.00 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 17 Write SQL SELECT statement to calculate the income generated by every ingredient (Sum(Amount*I_price) ).
Examples: Table HomeDeliveryPizzas (4) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice Tomato sauceM DGCSofia CheeseSm DGCSofia DressingL DGCSofia Tomato sauceM VKAUBG cheeseSm1.00 VKAUBG HamKg JGScapto SausageM JGScapto6.00 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 18 What the following queries will print out: SELECT Pizza_id, Ingr_id, Descr, Sum(Amount*I_price) AS Money FROM HDPizzas WHERE Pizza_id <> 124 GROUP BY Pizza_id, Ingr_id, Descr;
Examples: Table HomeDeliveryPizzas (5) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice Tomato sauceM DGCSofia CheeseSm DGCSofia DressingL DGCSofia Tomato sauceM VKAUBG cheeseSm1.00 VKAUBG HamKg JGScapto SausageM JGScapto6.00 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 19 What the following queries will print out: SELECT Pizza_id, Ingr_id, Sum(Amount) AS Used FROM HDPizzas GROUP BY Pizza_id, Ingr_id ORDER BY 3;
Examples: Table HomeDeliveryPizzas (6) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice Tomato sauceM DGCSofia CheeseSm DGCSofia DressingL DGCSofia Tomato sauceM VKAUBG CheeseSm1.00 VKAUBG HamKg JGScapto SausageM JGScapto6.00 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 20 What the following queries will print out: SELECT Pizza_id, Address, Count(*) AS Used FROM HDPizzas GROUP BY Pizza_id, Address ORDER BY Address;
Examples: Table Booking (1) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double AAA101Double AAA102Double BBB200SingleNull AAA205Apartment AAA210SingleNull BBB550Double BBB312Single D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 21 Write the SQL query to list the number of rooms by type, booked for March or April, 2014.
Examples: Table Booking (2) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double AAA101Double AAA102Double BBB200SingleNull AAA205Apartment AAA210SingleNull BBB550Double BBB312Single D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 22 Write the SQL query to list the income expected by any of the hotels in May.
Examples: Table Booking (3) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double AAA101Double AAA102Double BBB200SingleNull AAA205Apartment AAA210SingleNull BBB550Double BBB312Single D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 23 Write the SQL query to list the daily average income, expected from every type of room booked for March or April, 2014.
Examples: Table Booking (4) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double AAA101Double AAA102Double BBB200SingleNull AAA205Apartment AAA210SingleNull BBB550Double BBB312Single D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 24 What the following queries will print out: SELECT Hotel_id, SUM(Price) FROM Booking WHERE Date_from IS NOT Null GROUP BY Hotel_id ORDER BY 2 DESC;
Examples: Table Booking (5) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double AAA101Double AAA102Double BBB200SingleNull AAA205Apartment AAA210SingleNull BBB550Double BBB312Single D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 25 What the following queries will print out: SELECT Room_type, SUM(Price) FROM Booking WHERE Date_from IS NOT Null GROUP BY Room_type HAVING Room_type IN ('Single', 'Double') ORDER BY 2 DESC;
Examples: Table Booking (6) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double AAA101Double AAA102Double BBB200SingleNull AAA205Apartment AAA210SingleNull BBB550Double BBB312Single D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 26 What the following queries will print out: SELECT Room_type, Count(*) FROM Booking WHERE Date_from IS NOT Null GROUP BY Room_type HAVING Room_type <> 'Single' ORDER BY 2 DESC;
Examples: Table Booking (7) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double AAA101Double AAA102Double BBB200SingleNull AAA205Apartment AAA210SingleNull BBB550Double BBB312Single D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 27 What the following queries will print out: SELECT Room_type, SUM(Price) FROM Booking WHERE Date_from IS NOT Null and Hotel_id = 'AAA' GROUP BY Room_type HAVING AVG(Price) > 100 ORDER BY 2 DESC;
Examples: table Vehicles – claims (1) V_idDate_SoldC_idCdateCostMileagePart C P C P C P C P325 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 28 Write a query to list vehicles with a single claim only.
Examples: table Vehicles – claims (2) V_idDate_SoldC_idCdateCostMileagePart C P C P C P C P325 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 29 Write a query to list vehicles with multiple claims only.
Examples: table Vehicles – claims (4) V_idDate_SoldC_idCdateCostMileagePart C P C P C P C P325 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 30 For vehicles with multiple claims, calculate the mileage accumulation before the first claim.