Final Review. put the following items in their correct order where order by select having group by...
-
Upload
philip-andrews -
Category
Documents
-
view
216 -
download
0
Transcript of Final Review. put the following items in their correct order where order by select having group by...
Final Review
put the following items in their correct order
whereorder byselecthavinggroup byfrom
selectfromwheregroup byhavingorder by
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
What does DDL stand for?
• Stands for Data Definition Language– create statements– drop statements– alter statements
List reasons for using views
Tell me about indexes
Using SQL, how do you add records to a table
Using SQL, how do you remove data from a table?
Using SQL, how do you change data in a table?
Using SQL, how do you change your default database?
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
Field Type Null Key Default ExtraORDER_NUM char(5) NO PRI PART_NUM char(4) NO PRI NUM_ORDERED decimal(3,0) YES QUOTED_PRICE decimal(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:
Field Type Null Key Default ExtraBOOK_CODE char(4) NO PRI BRANCH_NUM decimal(2,0) NO PRI 0 ON_HAND decimal(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:
What is a database?
structure containing categories of information and relationships between these categories
What is a Database Management System (DBMS)?
software that lets you create a database and maintain the structures and data within
What is open source software?
software whose source code is freely and publicly available
Describe the relationship
17
18
A sales rep has one or many customers
19
A sales rep has one or many customers
A customer has one and only one sales rep
What does SQL stand for?
Structured Query Language
• What is another name for a table?– Entity
• What is a relationship– An association between entities
What are some reasons for database normalization?
• minimize duplication of data• help safeguard against data anomalies• improve performance for certain activities
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.”
Tell me about null values
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15
shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null
Shops
Workers
Revenues
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
What will the following query return?
select*
fromShops
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
Results
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
Write a query that will return all records that have the letter “L” in their shop name
shop_id shop_name2 College Coffee Shop3 Northland Coffee Shop
Results
select*
from Shopswhere shop_name like '%L%'
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
Write a query that will return a count of all records in the shops table
Count(*)3
Results
select count(*)from Shops
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15
What will the following query return?
select shop_namefrom Shops sjoin workers w on w.shop_id = s.shop_id and w.name = 'Kris'
shop_nameWisconsin Coffee Shop
Results
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15
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 sjoin workers w on w.shop_id = s.shop_idgroup by s.shop_name
shop_name Avg Pay RtCollege Coffee Shop 8.75
Northland Coffee Shop 9Wisconsin Coffee Shop 8.5
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15
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 sjoin workers w on w.shop_id = s.shop_idgroup by s.shop_namehaving avg(w.pay_rt) > 8.5
shop_name Avg Pay RtCollege Coffee Shop 8.75
Northland Coffee Shop 9
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15
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 sjoin workers w on w.shop_id = s.shop_id and w.pay_rt >= 8.5group by s.shop_namehaving avg(w.pay_rt) > 8.5
shop_name Avg Pay RtCollege Coffee Shop 8.75
Northland Coffee Shop 9Wisconsin Coffee Shop 8.75
worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15
What will the following query return?
Results
select distinct namefrom workers
nameKrisJill
ErikaKimBill
worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15
What will the following query return?
Results
select distinct namefrom workerssort by
1
ERROR
Sort by isn’t a valid clause
worker_id shop_id name pay_rt weekly_hrs1 1 Kris 8 252 2 Jill 8.5 403 2 Erika 9 404 1 Kim 8.5 305 1 Bill 9 256 3 Bill 9 15
What will the following query return?
Results
select distinct namefrom workersorder by
1
nameBill
ErikaJill
KimKris
put the following items in their correct order
whereorder byselecthavinggroup byfrom
selectfromwheregroup byhavingorder by
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null
What will the following query return?
select s.shop_name, sum(r.revenues)from shops sleft outer join revenues r on r.shop_id = s.shop_id
Results
ERROR
Query is missing a group by
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null
What will the following query return?
select s.shop_name, sum(r.revenues)from shops sleft outer join revenues r on r.shop_id = s.shop_idgroup by s.shop_name
Results
shop_name sum(r.revenues)College Coffee Shop 12100
Northland Coffee Shop Wisconsin Coffee Shop 23800
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.
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.
All operator
condition is true only if it satisfies all values
Any operator
condition is true only if it satisfies any value
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null
Write a query that will list shops that do not have any recorded revenues (use IN in your query)
select shop_namefrom shopswhere shop_id not in ( select shop_id from revenues )
Results
shop_nameNorthland Coffee Shop
shop_id shop_name1 Wisconsin Coffee Shop2 College Coffee Shop3 Northland Coffee Shop
shop_id year quarter revenues1 2008 1 100001 2008 2 75001 2008 3 63002 2008 1 48002 2008 2 73002 2008 3 null
Write a query that will list shops that do not have any recorded revenues (use EXISTS in your query)
select shop_namefrom shops swhere not exists ( select shop_id from revenues r
wherer.shop_id = s.shop_id
)Results
shop_nameNorthland Coffee Shop
What is a correlated subquery?
A subquery that involves a table listed in outer query
46
Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk
Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2
select name, carfrom 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?
name carKris Windorski Cavalier
Al Pacino FerrariAl Pacino Lexus
Paris Hilton nullJessica Simpson null
A.J. Hawk null
Results
47
Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk
Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2
select name, carfrom 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?
name carKris Windorski Cavalier
Al Pacino FerrariAl Pacino Lexus
Results
Why do these queries produce different results?
48
select name, carfrom car_dealer c
left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000
select name, carfrom 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
Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk
Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2
select name, carfrom 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
Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk
Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2
select name, carfrom car_dealer c
left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000
name carKris Windorski Cavalier
Al Pacino FerrariAl Pacino Lexus
Paris Hilton nullJessica Simpson null
A.J. Hawk null
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
Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk
Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2
select name, carfrom car_dealer c
left outer join sales s on s.car_dealer_id = c.car_dealer_id and s.cost > 50000
name carKris Windorski Cavalier
Al Pacino FerrariAl Pacino Lexus
Paris Hilton nullJessica Simpson null
A.J. Hawk null
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
Car_Dealer_ID Name1 Kris Windorski2 Al Pacino3 Paris Hilton4 Jessica Simpson5 A.J. Hawk
Sale_ID Car Cost Car_Dealer_ID1 Porsche 40,000 12 Cavalier 60,000 13 Mustang 30,000 34 Vibe 19,000 45 BMW 42,000 46 Ferrari 65,000 27 Lexus 61,000 2
select name, carfrom 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
Union vs. Union all
Instructor_ID Name
1 Doug Waterman
2 Terri Keane
3 Glen Orsburn
4 Kris Windorski
Class_NBR Instructor_ID Name
1111 4 SQL
4444 1 VB.net
2222 3 SQL
5555 2 C++
99999 4 Accounting
7777 4 SQL
What will the following query return?
54
select i.name as 'Instructor Name', c.name as 'Class Name'from instructor i, class c
Instructor Name Class NameDoug Waterman SQL
Terri Keane SQLGlen Orsburn SQLKris Windorski SQL
Doug Waterman VB.netTerri Keane VB.net
Glen Orsburn VB.netKris Windorski VB.net
Doug Waterman SQLTerri Keane SQL
Glen Orsburn SQLKris Windorski SQL
Doug Waterman C++Terri Keane C++
Glen Orsburn C++Kris Windorski C++
Doug Waterman AccountingTerri Keane Accounting
Glen Orsburn AccountingKris Windorski Accounting
Doug Waterman SQLTerri Keane SQL
Glen Orsburn SQLKris Windorski SQL
Results
A join is not specified for the tables
SQL returns every possible combination
Referred to as a Cartesian Product
put the following items in their correct order
whereorder byselecthavinggroup byfrom
selectfromwheregroup byhavingorder by