Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1.

Similar presentations


Presentation on theme: "SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1."— Presentation transcript:

1 SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 Examples: Table CSupplier (1) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD13.00433P15.0020140217 1X15.00VKVolinV14.00443Stoka 16.0020140217 1X15.00JGJohnJ11.00444Good 14.0020140217 2X26.00DGCDimitarD11.00433P22.0020140218 2X26.00VKVolinV11.00443Stoka 23.0020140219 3X37.00DGCDimitarD10.30433P38.0020140220 3X37.00JGJohnJ11.50444Good 36.0020140220 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.

10 Examples: Table CSupplier (2) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD13.00433P15.0020140217 1X15.00VKVolinV14.00443Stoka 16.0020140217 1X15.00JGJohnJ11.00444Good 14.0020140217 2X26.00DGCDimitarD11.00433P22.0020140218 2X26.00VKVolinV11.00443Stoka 23.0020140219 3X37.00DGCDimitarD10.30433P38.0020140220 3X37.00JGJohnJ11.50444Good 36.0020140220 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.

11 Examples: Table CSupplier (3) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD13.00433P15.0020140217 1X15.00VKVolinV14.00443Stoka 16.0020140217 1X15.00JGJohnJ11.00444Good 14.0020140217 2X26.00DGCDimitarD11.00433P22.0020140218 2X26.00VKVolinV11.00443Stoka 23.0020140219 3X37.00DGCDimitarD10.30433P38.0020140220 3X37.00JGJohnJ11.50444Good 36.0020140220 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.

12 Examples: Table CSupplier (4) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD13.00433P15.0020140217 1X15.00VKVolinV14.00443Stoka 16.0020140217 1X15.00JGJohnJ11.00444Good 14.0020140217 2X26.00DGCDimitarD11.00433P22.0020140218 2X26.00VKVolinV11.00443Stoka 23.0020140219 3X37.00DGCDimitarD10.30433P38.0020140220 3X37.00JGJohnJ11.50444Good 36.0020140220 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;

13 Examples: Table CSupplier (5) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD13.00433P15.0020140217 1X15.00VKVolinV14.00443Stoka 16.0020140217 1X15.00JGJohnJ11.00444Good 14.0020140217 2X26.00DGCDimitarD21.00433P22.0020140218 2X26.00VKVolinV21.00443Stoka 23.0020140219 3X37.00DGCDimitarD30.30433P38.0020140220 3X37.00JGJohnJ31.50444Good 36.0020140220 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;

14 Examples: Table CSupplier (6) CodeNamePriceDealerDnameDcodeDpriceDphoneDPNameDQntyDDate 1X15.00DGCDimitarD13.00433P15.0020140217 1X15.00VKVolinV14.00443Stoka 16.0020140217 1X15.00JGJohnJ11.00444Good 14.0020140217 2X26.00DGCDimitarD21.00433P22.0020140218 2X26.00VKVolinV21.00443Stoka 23.0020140219 3X37.00DGCDimitarD30.30433P38.0020140220 3X37.00JGJohnJ31.50444Good 36.0020140220 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;

15 Examples: Table HomeDeliveryPizzas (1) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice 1345523Tomato sauceM3.002.00DGCSofia5.00 1124312CheeseSm4.001.00DGCSofia6.00 1233117DressingL1.0012.00DGCSofia4.00 2345223Tomato sauceM1.002.00VKAUBG2.00 2124112CheeseSm1.00 VKAUBG3.00 3345127HamKg0.3012.00JGScapto8.00 323328SausageM1.5011.00JGScapto6.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.

16 Examples: Table HomeDeliveryPizzas (2) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice 1345523Tomato sauceM3.002.00DGCSofia5.00 1124312CheeseSm4.001.00DGCSofia6.00 1233117DressingL1.0012.00DGCSofia4.00 2345223Tomato sauceM1.002.00VKAUBG2.00 2124112CheeseSm1.00 VKAUBG3.00 3345127HamKg0.3012.00JGScapto8.00 323328SausageM1.5011.00JGScapto6.00 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 16 Write SQL SELECT statement to evaluate the profitability by address.

17 Examples: Table HomeDeliveryPizzas (3) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice 1345523Tomato sauceM3.002.00DGCSofia5.00 1124312CheeseSm4.001.00DGCSofia6.00 1233117DressingL1.0012.00DGCSofia4.00 2345223Tomato sauceM1.002.00VKAUBG2.00 2124112CheeseSm1.00 VKAUBG3.00 3345127HamKg0.3012.00JGScapto8.00 323328SausageM1.5011.00JGScapto6.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) ).

18 Examples: Table HomeDeliveryPizzas (4) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice 1345523Tomato sauceM3.002.00DGCSofia5.00 1124312CheeseSm4.001.00DGCSofia6.00 1233117DressingL1.0012.00DGCSofia4.00 2345223Tomato sauceM1.002.00VKAUBG2.00 2124112cheeseSm1.00 VKAUBG3.00 3345127HamKg0.3012.00JGScapto8.00 323328SausageM1.5011.00JGScapto6.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;

19 Examples: Table HomeDeliveryPizzas (5) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice 1345523Tomato sauceM3.002.00DGCSofia5.00 1124312CheeseSm4.001.00DGCSofia6.00 1233117DressingL1.0012.00DGCSofia4.00 2345223Tomato sauceM1.002.00VKAUBG2.00 2124112cheeseSm1.00 VKAUBG3.00 3345127HamKg0.3012.00JGScapto8.00 323328SausageM1.5011.00JGScapto6.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;

20 Examples: Table HomeDeliveryPizzas (6) Order_idPizza_idQuantityIngr_idDescrUnitAmountI_priceNameAddressPrice 1345523Tomato sauceM3.002.00DGCSofia5.00 1124312CheeseSm4.001.00DGCSofia6.00 1233117DressingL1.0012.00DGCSofia4.00 2345223Tomato sauceM1.002.00VKAUBG2.00 2124112CheeseSm1.00 VKAUBG3.00 3345127HamKg0.3012.00JGScapto8.00 323328SausageM1.5011.00JGScapto6.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;

21 Examples: Table Booking (1) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double2014-03-052014-03-10250.00 AAA101Double2014-03-052014-03-0880.00 AAA102Double2014-03-112014-04-251500.00 BBB200SingleNull 100.00 AAA205Apartment2014-06-012014-08-3010000.00 AAA210SingleNull 120.00 BBB550Double2014-04-102014-04-30500.00 BBB312Single2014-04-122014-05-08343.00 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.

22 Examples: Table Booking (2) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double2014-03-052014-03-10250.00 AAA101Double2014-03-052014-03-0880.00 AAA102Double2014-03-112014-04-251500.00 BBB200SingleNull 100.00 AAA205Apartment2014-06-012014-08-3010000.00 AAA210SingleNull 120.00 BBB550Double2014-04-102014-04-30500.00 BBB312Single2014-04-122014-05-08343.00 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.

23 Examples: Table Booking (3) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double2014-03-052014-03-10250.00 AAA101Double2014-03-052014-03-0880.00 AAA102Double2014-03-112014-04-251500.00 BBB200SingleNull 100.00 AAA205Apartment2014-06-012014-08-3010000.00 AAA210SingleNull 120.00 BBB550Double2014-04-102014-04-30500.00 BBB312Single2014-04-122014-05-08343.00 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.

24 Examples: Table Booking (4) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double2014-03-052014-03-10250.00 AAA101Double2014-03-052014-03-0880.00 AAA102Double2014-03-112014-04-251500.00 BBB200SingleNull 100.00 AAA205Apartment2014-06-012014-08-3010000.00 AAA210SingleNull 120.00 BBB550Double2014-04-102014-04-30500.00 BBB312Single2014-04-122014-05-08343.00 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;

25 Examples: Table Booking (5) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double2014-03-052014-03-10250.00 AAA101Double2014-03-052014-03-0880.00 AAA102Double2014-03-112014-04-251500.00 BBB200SingleNull 100.00 AAA205Apartment2014-06-012014-08-3010000.00 AAA210SingleNull 120.00 BBB550Double2014-04-102014-04-30500.00 BBB312Single2014-04-122014-05-08343.00 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;

26 Examples: Table Booking (6) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double2014-03-052014-03-10250.00 AAA101Double2014-03-052014-03-0880.00 AAA102Double2014-03-112014-04-251500.00 BBB200SingleNull 100.00 AAA205Apartment2014-06-012014-08-3010000.00 AAA210SingleNull 120.00 BBB550Double2014-04-102014-04-30500.00 BBB312Single2014-04-122014-05-08343.00 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;

27 Examples: Table Booking (7) Hotel_idRoom_NumberRoom_typeDate_fromDate_toPrice AAA100Double2014-03-052014-03-10250.00 AAA101Double2014-03-052014-03-0880.00 AAA102Double2014-03-112014-04-251500.00 BBB200SingleNull 100.00 AAA205Apartment2014-06-012014-08-3010000.00 AAA210SingleNull 120.00 BBB550Double2014-04-102014-04-30500.00 BBB312Single2014-04-122014-05-08343.00 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;

28 Examples: table Vehicles – claims (1) V_idDate_SoldC_idCdateCostMileagePart 0012013-12-04C0012014-03-12100.00500P001 0022014-01-02C0022014-03-12200.00300P001 0022014-01-02C0032014-04-15120.001200P004 0032014-01-23C0042014-05-1734.00800P325 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 28 Write a query to list vehicles with a single claim only.

29 Examples: table Vehicles – claims (2) V_idDate_SoldC_idCdateCostMileagePart 0012013-12-04C0012014-03-12100.00500P001 0022014-01-02C0022014-03-12200.00300P001 0022014-01-02C0032014-04-15120.001200P004 0032014-01-23C0042014-05-1734.00800P325 D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 29 Write a query to list vehicles with multiple claims only.

30 Examples: table Vehicles – claims (4) V_idDate_SoldC_idCdateCostMileagePart 0012013-12-04C0012014-03-12100.00500P001 0022014-01-02C0022014-03-12200.00300P001 0022014-01-02C0032014-04-15120.001200P004 0032014-01-23C0042014-05-1734.00800P325 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.


Download ppt "SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1."

Similar presentations


Ads by Google