Presentation is loading. Please wait.

Presentation is loading. Please wait.

Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by.

Similar presentations


Presentation on theme: "Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by."— Presentation transcript:

1 Final Review

2 put the following items in their correct order where order by select having group by from select from where group by having order by

3 Table_A has 3 records and Table_B has 4 records How many records will the following query return? select * from Table_A, Table_B

4 What does DDL stand for? Stands for Data Definition Language – create statements – drop statements – alter statements

5 List reasons for using views

6 Tell me about indexes

7 Using SQL, how do you add records to a table

8 Using SQL, how do you remove data from a table?

9 Using SQL, how do you change data in a table?

10 Using SQL, how do you change your default database?

11 CREATE TABLE a_marina ( MARINA_NUM char(4) NOT NULL, NAME char(20), ADDRESS char(15), CITY char(15), STATE char(2), ZIP ) What will this SQL statement do? ERROR We did not specify a datatype for the column ZIP

12 FieldTypeNullKeyDefaultExtra ORDER_NUMchar(5)NOPRI PART_NUMchar(4)NOPRI NUM_ORDEREDdecimal(3,0)YES QUOTED_PRICEdecimal(6,2)YES CREATE TABLE p_order_line ( ORDER_NUM char(5) NOT NULL, PART_NUM char(4) NOT NULL, NUM_ORDERED decimal(3,0) NULL, QUOTED_PRICE decimal(6,2) NULL, PRIMARY KEY (ORDER_NUM,PART_NUM) ) Write the SQL that will create an P_ORDER_LINE table with the following structure:

13 FieldTypeNullKeyDefaultExtra BOOK_CODEchar(4)NOPRI BRANCH_NUMdecimal(2,0)NOPRI0 ON_HANDdecimal(2,0)YES CREATE TABLE h_inventory ( BOOK_CODE char(4) NOT NULL, BRANCH_NUM decimal(2,0) NOT NULL default 0, ON_HAND decimal(2,0) NULL, PRIMARY KEY (BOOK_CODE,BRANCH_NUM) ) Write the SQL that will create an H_INVENTORY table with the following structure:

14 What is a database? structure containing categories of information and relationships between these categories

15 What is a Database Management System (DBMS)? software that lets you create a database and maintain the structures and data within

16 What is open source software? software whose source code is freely and publicly available

17 Describe the relationship 17

18 18 A sales rep has one or many customers

19 19 A sales rep has one or many customers A customer has one and only one sales rep

20 What does SQL stand for? Structured Query Language

21 What is another name for a table? – Entity What is a relationship – An association between entities

22 What are some reasons for database normalization? minimize duplication of data help safeguard against data anomalies improve performance for certain activities

23 What is a primary key? A primary key uniquely identifies a row in a table. A primary key can be a single column, or a group of columns. When a primary key is made up of multiple columns it may also be referred to as a composite key or a concatenated key, both of which mean “consisting of more than one column.”

24 Tell me about null values

25 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill8.540 32Erika940 41Kim8.530 51Bill925 63Bill915 shop_idyearquarterrevenues 12008110000 1200827500 1200836300 2200814800 2200827300 220083null Shops Workers Revenues

26 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop What will the following query return? select * from Shops shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop Results

27 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop Write a query that will return all records that have the letter “L” in their shop name shop_idshop_name 2College Coffee Shop 3Northland Coffee Shop Results select * from Shops where shop_name like '%L%'

28 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop Write a query that will return a count of all records in the shops table Count(*) 3 Results select count(*) from Shops

29 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill8.540 32Erika940 41Kim8.530 51Bill925 63Bill915 What will the following query return? select shop_name from Shops s join workers w on w.shop_id = s.shop_id and w.name = 'Kris' shop_name Wisconsin Coffee Shop Results

30 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill8.540 32Erika940 41Kim8.530 51Bill925 63Bill915 Write a query that will return average pay rate by shop name Results select s.shop_name, avg(w.pay_rt) as 'Avg Pay Rt' from Shops s join workers w on w.shop_id = s.shop_id group by s.shop_name shop_nameAvg Pay Rt College Coffee Shop8.75 Northland Coffee Shop9 Wisconsin Coffee Shop8.5

31 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill8.540 32Erika940 41Kim8.530 51Bill925 63Bill915 Write a query that will return average pay rate by shop name. Only display shops with an average pay rate over 8.50 Results select s.shop_name, avg(w.pay_rt) as 'Avg Pay Rt' from Shops s join workers w on w.shop_id = s.shop_id group by s.shop_name having avg(w.pay_rt) > 8.5 shop_nameAvg Pay Rt College Coffee Shop8.75 Northland Coffee Shop9

32 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill8.540 32Erika940 41Kim8.530 51Bill925 63Bill915 Write a query that will return average pay rate by shop name. Don’t include workers making less then 8.50 and only display shops with an average pay rate over 8.50 Results select s.shop_name, avg(w.pay_rt) as 'Avg Pay Rt' from Shops s join workers w on w.shop_id = s.shop_id and w.pay_rt >= 8.5 group by s.shop_name having avg(w.pay_rt) > 8.5 shop_nameAvg Pay Rt College Coffee Shop8.75 Northland Coffee Shop9 Wisconsin Coffee Shop8.75

33 worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill8.540 32Erika940 41Kim8.530 51Bill925 63Bill915 What will the following query return? Results select distinct name from workers name Kris Jill Erika Kim Bill

34 worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill8.540 32Erika940 41Kim8.530 51Bill925 63Bill915 What will the following query return? Results select distinct name from workers sort by 1 ERROR Sort by isn’t a valid clause

35 worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill8.540 32Erika940 41Kim8.530 51Bill925 63Bill915 What will the following query return? Results select distinct name from workers order by 1 name Bill Erika Jill Kim Kris

36 put the following items in their correct order where order by select having group by from select from where group by having order by

37 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop shop_idyearquarterrevenues 12008110000 1200827500 1200836300 2200814800 2200827300 220083null What will the following query return? select s.shop_name, sum(r.revenues) from shops s left outer join revenues r on r.shop_id = s.shop_id Results ERROR Query is missing a group by

38 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop shop_idyearquarterrevenues 12008110000 1200827500 1200836300 2200814800 2200827300 220083null What will the following query return? select s.shop_name, sum(r.revenues) from shops s left outer join revenues r on r.shop_id = s.shop_id group by s.shop_name Results shop_namesum(r.revenues) College Coffee Shop12100 Northland Coffee Shop Wisconsin Coffee Shop23800

39 Explain left outer joins Left outer join: Returns data from the table on the left regardless if it has a match in the table on the right. If there is not a match on the right, null values are returned.

40 Explain right outer joins right outer join: Returns data from the table on the right regardless if it has a match in the table on the left. If there is not a match on the left, null values are returned.

41 All operator condition is true only if it satisfies all values

42 Any operator condition is true only if it satisfies any value

43 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop shop_idyearquarterrevenues 12008110000 1200827500 1200836300 2200814800 2200827300 220083null Write a query that will list shops that do not have any recorded revenues (use IN in your query) select shop_name from shops where shop_id not in ( select shop_id from revenues ) Results shop_name Northland Coffee Shop

44 shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop shop_idyearquarterrevenues 12008110000 1200827500 1200836300 2200814800 2200827300 220083null Write a query that will list shops that do not have any recorded revenues (use EXISTS in your query) select shop_name from shops s where not exists ( select shop_id from revenues r where r.shop_id = s.shop_id ) Results shop_name Northland Coffee Shop

45 What is a correlated subquery? A subquery that involves a table listed in outer query

46 46 Car_Dealer_IDName 1Kris Windorski 2Al Pacino 3Paris Hilton 4Jessica Simpson 5A.J. Hawk Sale_IDCarCostCar_Dealer_ID 1Porsche40,0001 2Cavalier60,0001 3Mustang30,0003 4Vibe19,0004 5BMW42,0004 6Ferrari65,0002 7Lexus61,0002 select name, car from car_dealer c left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000 What will the following query return? namecar Kris WindorskiCavalier Al PacinoFerrari Al PacinoLexus Paris Hiltonnull Jessica Simpsonnull A.J. Hawknull Results

47 47 Car_Dealer_IDName 1Kris Windorski 2Al Pacino 3Paris Hilton 4Jessica Simpson 5A.J. Hawk Sale_IDCarCostCar_Dealer_ID 1Porsche40,0001 2Cavalier60,0001 3Mustang30,0003 4Vibe19,0004 5BMW42,0004 6Ferrari65,0002 7Lexus61,0002 select name, car from car_dealer c left outer join sales s on s.car_dealer_id = c.car_dealer_id where s.cost > 50000 What will the following query return? namecar Kris WindorskiCavalier Al PacinoFerrari Al PacinoLexus Results

48 Why do these queries produce different results? 48 select name, car from car_dealer c left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000 select name, car from car_dealer c left outer join sales s on s.car_dealer_id = c.car_dealer_id where s.cost > 50000 When a query only has inner joins a filter placed in the “where” clause will yield the same result as placing the filter in the “from” clause. However, the same cannot be said when dealing with outer joins Let’s take a closer look

49 49 Car_Dealer_IDName 1Kris Windorski 2Al Pacino 3Paris Hilton 4Jessica Simpson 5A.J. Hawk Sale_IDCarCostCar_Dealer_ID 1Porsche40,0001 2Cavalier60,0001 3Mustang30,0003 4Vibe19,0004 5BMW42,0004 6Ferrari65,0002 7Lexus61,0002 select name, car from car_dealer c left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000 A left outer join returns data from the table on the left regardless if it has a match in the table on the right. In this query in order for there to be a match between the two tables the join criteria must be met, which includes the following : - Matching car dealer ids - A cost greater then 50,000

50 50 Car_Dealer_IDName 1Kris Windorski 2Al Pacino 3Paris Hilton 4Jessica Simpson 5A.J. Hawk Sale_IDCarCostCar_Dealer_ID 1Porsche40,0001 2Cavalier60,0001 3Mustang30,0003 4Vibe19,0004 5BMW42,0004 6Ferrari65,0002 7Lexus61,0002 select name, car from car_dealer c left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000 namecar Kris WindorskiCavalier Al PacinoFerrari Al PacinoLexus Paris Hiltonnull Jessica Simpsonnull A.J. Hawknull Results The car_dealer table is the table on the left, so we know every car dealer’s name will be returned regadless if it satisfies the join criteria

51 51 Car_Dealer_IDName 1Kris Windorski 2Al Pacino 3Paris Hilton 4Jessica Simpson 5A.J. Hawk Sale_IDCarCostCar_Dealer_ID 1Porsche40,0001 2Cavalier60,0001 3Mustang30,0003 4Vibe19,0004 5BMW42,0004 6Ferrari65,0002 7Lexus61,0002 select name, car from car_dealer c left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000 namecar Kris WindorskiCavalier Al PacinoFerrari Al PacinoLexus Paris Hiltonnull Jessica Simpsonnull A.J. Hawknull Results If the join condition is satisfied the information from the right (in this case the sales table) will also be returned. If the condition is not met null values will be returned from the right.

52 52 Car_Dealer_IDName 1Kris Windorski 2Al Pacino 3Paris Hilton 4Jessica Simpson 5A.J. Hawk Sale_IDCarCostCar_Dealer_ID 1Porsche40,0001 2Cavalier60,0001 3Mustang30,0003 4Vibe19,0004 5BMW42,0004 6Ferrari65,0002 7Lexus61,0002 select name, car from car_dealer c left outer join sales s on s.car_dealer_id = c.car_dealer_id where s.cost > 50000 A left outer join returns data from the table on the left regardless if it has a match in the table on the right. In this query in order for there to be a match between the two tables the join criteria must be met, which includes the following : - Matching car dealer ids Note: the “where” clause also contains join/filter criteria, but logically it is applied after the join/filter criteria in the “from” clause have been applied

53 Union vs. Union all

54 Instructor_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C++ 999994Accounting 77774SQL What will the following query return? 54 select i.name as 'Instructor Name', c.name as 'Class Name' from instructor i, class c Instructor NameClass Name Doug WatermanSQL Terri KeaneSQL Glen OrsburnSQL Kris WindorskiSQL Doug WatermanVB.net Terri KeaneVB.net Glen OrsburnVB.net Kris WindorskiVB.net Doug WatermanSQL Terri KeaneSQL Glen OrsburnSQL Kris WindorskiSQL Doug WatermanC++ Terri KeaneC++ Glen OrsburnC++ Kris WindorskiC++ Doug WatermanAccounting Terri KeaneAccounting Glen OrsburnAccounting Kris WindorskiAccounting Doug WatermanSQL Terri KeaneSQL Glen OrsburnSQL Kris WindorskiSQL Results A join is not specified for the tables SQL returns every possible combination Referred to as a Cartesian Product

55 put the following items in their correct order where order by select having group by from select from where group by having order by


Download ppt "Final Review. put the following items in their correct order where order by select having group by from select from where group by having order by."

Similar presentations


Ads by Google