Copyright © 2010 M. E. Kabay. All rights reserved. Analyzing and Building Simple Queries in SQL IS240 – Database Management Supplement to Chapter 4 M.

Slides:



Advertisements
Similar presentations
Database Relationships in Access As you recall, the data in a database is stored in tables. In a relational database like Access, you can have multiple.
Advertisements

The Relational Model and Normalization (3) IS 240 – Database Management Lecture # Prof. M. E. Kabay, PhD, CISSP Norwich University
The Relational Model and Normalization (1)
Foundations of Relational Implementation (2) IS 240 – Database Management Lecture #14 – Prof. M. E. Kabay, PhD, CISSP Norwich University
Database Design (1) IS 240 – Database Management Lecture #10 – Prof. M. E. Kabay, PhD, CISSP Norwich University
Foundations of Relational Implementation (1) IS 240 – Database Management Lecture #13 – Prof. M. E. Kabay, PhD, CISSP Norwich University
Managing Multi-User Databases (2) IS 240 – Database Management Lecture #19 – Prof. M. E. Kabay, PhD, CISSP Norwich University
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
1 Copyright © 2010 Jerry Post with additions & narration by M. E. Kabay. All rights reserved. Advanced Queries IS240 – DBMS Lecture # 8 – M.
PowerPoint Presentation by Charlie Cook Copyright © 2004 South-Western. All rights reserved. Chapter 3 Database Management Systems Database Management.
MatLab – Palm Chapter 4, Part 3 For and While Loops
Lab 7: Attribute SQL- Query the database
Attribute databases. GIS Definition Diagram Output Query Results.
Pet Fish and High Cholesterol in the WHI OS: An Analysis Example Joe Larson 5 / 6 / 09.
Microsoft Access 2007 Microsoft Access 2007 Introduction to Database Programs.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1 Created By Martin Schedlbauer
Chapter 4 Querying Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba Asper School of Business 3500 DBMS Bob Travica.
Chapter 6: Foundations of Business Intelligence - Databases and Information Management Dr. Andrew P. Ciganek, Ph.D.
Chapter 11 Describing Process Specifications and Structured Decisions Systems Analysis and Design Kendall and Kendall Fifth Edition.
Working with MS-ACCESS IS 240 – Database Management Lecture #4 – Assoc. Prof. M. E. Kabay, PhD, CISSP-ISSMP Norwich University
Chapter 5 Advanced Querying
Inner Join vs. Outer Join
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
1 Agenda – 03/25/2014 Login to SQL Server 2012 Management Studio. Answer questions about HW#7 – display answers. Exam is 4/1/2014. It will be in the lab.
Agency (BU) Query Manager Training State of Indiana Instructor: Lori Shapiro, ENTAP.
Database Systems Microsoft Access Practical #3 Queries Nos 215.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
1 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Queries: Part 2 of 2 IS240 – DBMS Lecture # 7 – M. E. Kabay, PhD, CISSP-ISSMP.
Advanced Accounting Information Systems Day 10 answers Organizing and Manipulating Data September 16, 2009.
Databases.  A database is simply a collection of information stored in an orderly manner.  A database can be as simple as a birthday book, address book.
Intro to Access and Data Management. Announcements Chapter 5 – Thursday Entropy Registration Quiz Due Date Extended to Weds. Dreamspark Registration Questions?
Databases Letts Chapter 11. A database program can be used to:  sort a file into a different order;  search through the records for a matching string.
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.
Relational Database in Access Student System As always please use speaker notes!
Contact Information Leena Razzaq WVH 310B Office Hours: Monday 12:00 – 2:00 pm And by appointment.
1 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Queries: Part 1 of 2 IS240 – DBMS Lecture # 6 – M. E. Kabay, PhD, CISSP-ISSMP.
U:/msu/course/cse/103 Day 12, Slide 1 CSE 103 Students: Read Day 18 and learn about subqueries Others: Please save your.
E-R Model (2) IS 240 – Database Management Lecture #6 – Prof. M. E. Kabay, PhD, CISSP Norwich University
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
1 CPE 332 Introduction DBMS: Relational Database Managment Systems Instructor:Suthep Madarasmi, Ph.D. ดร. สุเทพ มาดารัศมี
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Advanced Query Analysis
Queries: Part 1 of 2 IS240 – DBMS Lecture # 6 –
GO! with Microsoft Office 2016
3.5 Databases Relationships.
IBC233 Week 6.
M. E. Kabay, PhD, CISSP-ISSMP V:
Database Normalization
Introduction: Course, SQ3R, Basic Concepts
Logging In ASP.Net Core 1.1.
GO! with Microsoft Access 2016
Chapter 7: Financial Statements
CIS 336 str Competitive Success/snaptutorial.com
CIS 336 str Education for Service- -snaptutorial.com.
CIS 336 str Teaching Effectively-- snaptutorial.com.
CIS 336 PAPERS Education for Service-- cis336papers.com.
My web site..
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Logging In ASP.Net Core 2.0.
Supplement: Using the DBDesign System
Analyzing and Building Simple Queries in SQL
2-1-1 Automated Verifications
Lesson 12.
Computer Science Projects Database Theory / Prototypes
Building Queries using the Principle of Simplest Query (POSQ)
Joins and other advanced Queries
IBC233 Week 5.
Presentation transcript:

Copyright © 2010 M. E. Kabay. All rights reserved. Analyzing and Building Simple Queries in SQL IS240 – Database Management Supplement to Chapter 4 M. E. Kabay, PhD, CISSP-ISSMP Assoc. Prof. Information Assurance School of Business & Management, Norwich University V:

Copyright © 2010 M. E. Kabay. All rights reserved. Sally’s Pet Store  Chapter 4  Exercises 1-25 deal with the Pet Store  Pp (in 4 th edition PDF file)  We will examine a way to analyze and solve query problems using SQL on the Pet Store database available from the Jerry Post Web site Someone From Ireland Will Grow Horseradish and Onions SELECT FROM INNER JOIN WHERE GROUP BY HAVING ORDER BY Mnemonic

Copyright © 2010 M. E. Kabay. All rights reserved. Sally’s Pet Store on the Web

Copyright © 2010 M. E. Kabay. All rights reserved. Downloading Other Databases

Copyright © 2010 M. E. Kabay. All rights reserved. Rolling Thunder Bicycles

Copyright © 2010 M. E. Kabay. All rights reserved. How to LEARN about SQL  In the following slides, we will dissect SQL queries into very small slices  This approach is useful when you are learning SQL  But it is very tedious  It is not the normal approach to creating queries  As you become familiar with SQL, you will be able to skip intermediate steps  It is nonetheless useful to break up complex queries into subqueries  Can test each step to be sure you are getting the records you expect  Avoids the confusing debugging problem caused when a complex query is created all at once and gives the wrong answer

Copyright © 2010 M. E. Kabay. All rights reserved. Sample Problem 1: Pet Store Animals born in May

Copyright © 2010 M. E. Kabay. All rights reserved. Sample Problem 1 (cont’d) Animals born in May  Which fields do we need?  AnimalID  DateBorn  Where are they?  Animal dataset  Do we need any other datasets to answer this question?  No  Which records do we need?  The ones where the month part of the date is equal to the code for May or where the date is between the start and end of the May. Someone From Ireland Will Grow Horseradish and Onions SELECT FROM INNER JOIN WHERE GROUP BY HAVING ORDER BY

Copyright © 2010 M. E. Kabay. All rights reserved. Sample Problem 1 (cont’d) Animals born in May  Start with the simplest query (no condition): SELECT Animal.AnimalID, Animal.DateBorn FROM Animal; !

Copyright © 2010 M. E. Kabay. All rights reserved. Sample Problem 1 (cont’d) Animals born in May  How do we determine the month in the date field?  We can use the Month() function  Functions can save enormous trouble  Avoid having to define complex conditions  E.g., setting ranges such as “date between the first and last day of a particular month” which requires hand-coding P. 196

Copyright © 2010 M. E. Kabay. All rights reserved. Sample Problem 1 (cont’d) Animals born in May  Now select the particular month:

Copyright © 2010 M. E. Kabay. All rights reserved. Sample Problem 2  During the 3 rd quarter, which item was sold in the largest quantity greater than 5 units?  Which fields are involved?  ItemID  Description  Quantity  OrderDate  PONumber  What datasets are involved?  OrderItem  Order  Merchandise Someone From Ireland Will Grow Horseradish and Onions SELECT FROM INNER JOIN WHERE GROUP BY HAVING ORDER BY

Copyright © 2010 M. E. Kabay. All rights reserved. Sample Problem 2 (cont’d)  During the 3 rd quarter, which item was sold in the largest quantity greater than 5 units in a single order?  How shall we break down the problem?  First figure out how to see all the items sold  Next determine how to find only the items sold more than 5 units at a time (makes code general)  Next determine how to locate the qualifying item sold in the largest quantity in a single order  Then figure out how to see all the items sold in the 3 rd quarter (don’t use range of days – use months)  Then combine the conditions to locate the record that fits the problem requirements  Finally link the chosen record to the Merchandise Description field to get the name of the item OrderItem MerchandiseOrder Merchandise

Copyright © 2010 M. E. Kabay. All rights reserved. First figure out how to see all the items sold ! Complete problem: During the 3rd quarter, which item was sold in the largest quantity greater than 5 units in a single order?

Copyright © 2010 M. E. Kabay. All rights reserved. Next determine how to find only the items sold more than 5 units at a time

Copyright © 2010 M. E. Kabay. All rights reserved. Next determine the qualifying item sold in the largest quantity in a single order Someone From Ireland Will Grow Horseradish and Onions SELECT FROM INNER JOIN WHERE GROUP BY HAVING ORDER BY Complete problem: During the 3rd quarter, which item was sold in the largest quantity greater than 5 units in a single order?

Copyright © 2010 M. E. Kabay. All rights reserved. Then figure out how to see all the items sold in the 3 rd quarter First we get all the data we need, then we reduce them to the 3rd quarter (MONTH 7, 8, or 9)

Copyright © 2010 M. E. Kabay. All rights reserved. Then combine the conditions to locate the record that fits the problem requirements Since we don’t actually need all this info about the item, we use this simpler query for ItemID:

Copyright © 2010 M. E. Kabay. All rights reserved. Finally link the chosen record to the Merchandise Description field to see the name of the item

Copyright © 2010 M. E. Kabay. All rights reserved. END