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.

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online
Multiple Table Queries Part 2
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Chapter 11 Group Functions
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
Structured Query Language Part I Chapter Three CIS 218.
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Concepts of Database Management, Fifth Edition
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Chapter 3 Single-Table Queries
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Basic SQL. Implementation Schemes: Once we have a set of relation schemes, we can translate them into implementation schemes. We use to express implementation.
Chapter 9 Joining Data from Multiple Tables
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making Chapter.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 16 Using Relational Databases.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Database Basics BCIS 3680 Enterprise Programming.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
1 2 Concepts of Database Management, 4 th Edition, Pratt & Adamski Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Connecting (relating) Data Tables to get Custom Records (Queries) Database Basics.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Query Processing – Implementing Set Operations and Joins Chap. 19.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Thinking in Sets and SQL Query Logical Processing.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL Query Getting to the data ……..
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Lecture 2 The Relational Model
Database Management  .
David M. Kroenke and David J
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Lecture 3 Finishing SQL
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Presentation transcript:

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

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

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:

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:

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_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill Erika940 41Kim Bill925 63Bill915 shop_idyearquarterrevenues null Shops Workers Revenues

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

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%'

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

shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill Erika940 41Kim Bill925 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

shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill Erika940 41Kim Bill925 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

shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill Erika940 41Kim Bill925 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

shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill Erika940 41Kim Bill925 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

worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill Erika940 41Kim Bill925 63Bill915 What will the following query return? Results select distinct name from workers name Kris Jill Erika Kim Bill

worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill Erika940 41Kim Bill925 63Bill915 What will the following query return? Results select distinct name from workers sort by 1 ERROR Sort by isn’t a valid clause

worker_idshop_idnamepay_rtweekly_hrs 11Kris825 22Jill Erika940 41Kim Bill925 63Bill915 What will the following query return? Results select distinct name from workers order by 1 name Bill Erika Jill Kim Kris

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

shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop shop_idyearquarterrevenues null 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

shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop shop_idyearquarterrevenues null 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

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_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop shop_idyearquarterrevenues null 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

shop_idshop_name 1Wisconsin Coffee Shop 2College Coffee Shop 3Northland Coffee Shop shop_idyearquarterrevenues null 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

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

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 > What will the following query return? namecar Kris WindorskiCavalier Al PacinoFerrari Al PacinoLexus Paris Hiltonnull Jessica Simpsonnull A.J. Hawknull Results

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 > What will the following query return? namecar Kris WindorskiCavalier Al PacinoFerrari Al PacinoLexus Results

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 > select name, car from car_dealer c left outer join sales s on s.car_dealer_id = c.car_dealer_id where s.cost > 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_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 > 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_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 > 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 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 > 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 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 > 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_IDName 1Doug Waterman 2Terri Keane 3Glen Orsburn 4Kris Windorski Class_NBRInstructor_IDName 11114SQL 44441VB.net 22223SQL 55552C Accounting 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

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