1 All Powder Board and Ski SQL Server Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003.

Slides:



Advertisements
Similar presentations
All Powder Board and Ski
Advertisements

1 All Powder Board and Ski Microsoft Access Workbook Chapter 9: Data Warehouses and Data Mining Jerry Post Copyright © 2007.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2007.
All Powder Board and Ski Microsoft Access Workbook Chapter 3: Database Tables Jerry Post Copyright © 2007.
1 All Powder Board and Ski SQL Server Workbook Chapter 4: Queries Jerry Post Copyright © 2003.
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
Introduction to Structured Query Language (SQL)
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
MySql In Action Step by step method to create your own database.
Working with Queries in Microsoft Access The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition.
SQL in Action Amit Bhawnani & Nimesh Shah. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
1 All Powder Board and Ski Microsoft Access Workbook Chapter 4: Queries Jerry Post Copyright © 2007.
Chapter 5 Advanced Querying
All Powder Board and Ski SQL Server 2000 Workbook Chapter 1 Jerry Post Copyright © 2003.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2003.
1 All Powder Board and Ski SQL Server Workbook Chapter 2: Database Design Jerry Post Copyright © 2004.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2003.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 9: Database Administration Jerry Post Copyright © 2003.
Using Special Operators (LIKE and IN)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
All Powder Board and Ski Oracle 9i Workbook Chapter 1 Jerry Post Copyright © 2003.
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
SQL for Data Retrieval. Review Questions of Previous Class Q1. Show the sum of hours worked for project with ID 1200 (use ASSIGNMENT table) – Use “SUM”
Getting to Know SQL. © Jim Hope 2002 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement TRANSFORM.
Recap of SQL Lab no 8 Advance Database Management System.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 4: Queries Jerry Post Copyright © 2003.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 10: Distributed Databases Jerry Post Copyright © 2003.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
All Powder Board and Ski Microsoft Access Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 8: Data Warehouses and Data Mining Jerry Post Copyright © 2003.
All Powder Board and Ski Microsoft Access Workbook Chapter 2: Database Design Jerry Post Copyright © 2003.
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.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 8: Data Warehouses and Data Mining Jerry Post Copyright © 2003.
All Powder Board and Ski Oracle 9i Workbook Chapter 3: Database Tables Jerry Post Copyright © 2003.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 4: Queries Jerry Post Copyright © 2003.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
McGraw-Hill/Irwin Copyright © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 9: Database Administration All Powder Board and Ski.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Chapter 6: Forms, Reports and Applications All Powder Board and Ski
Relational Database Design
M. E. Kabay, PhD, CISSP-ISSMP V:
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
© 2016, Mike Murach & Associates, Inc.
Chapter 2: Database Design All Powder Board and Ski
All Powder Board and Ski
All Powder Board and Ski
Module 5: Implementing Data Integrity by Using Constraints
DB Implementation: MS Access Queries
Introduction To Structured Query Language (SQL)
Introduction To Structured Query Language (SQL)
Chapter 8 Advanced SQL.
Presentation transcript:

1 All Powder Board and Ski SQL Server Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003

2 Primary Tables

3 Find Best Customers: 1 Total sales by customer.

4 Action Create a new query Tables: Customer, Sale, SaleItem Columns: CustomerID, LastName, FirstName, Sum(QuantitySold*SalePrice) AS SalesValue Group By the other columns Run the query Save as a view CustomerSales Create new query Table: CustomerSales query SELECT Avg(SalesValue) … Run the query

5 Average Customer Sales

6 Action Create a new query Table: CustomerSales query Columns: LastName, FirstName, SalesValue Criteria for SalesValue >(SELECT Avg(SalesValue) FROM CustomerSales)

7 Best Customers Subquery

8 INNER JOIN: Sales and Rentals

9 Action Create a new query Tables: Rental and Sale Columns: RentDate, SaleDate, and CustomerID from both tables. Join the tables on CustomerID Run the query Add a join between the tables on Rental.CustomerID=Sale.CustomerID Run the query

10 Left Join: Sales + Rental Left Join Sale+Customer to Rental Sales without rentals

11 Action Create a new query FROM (Customer INNER JOIN Sale ON Customer.CustomerID=Sale.CustomerID) LEFT JOIN Rental ON Sale.CustomerID = Rental.CustomerID Columns: LastName, FirstName, and CustomerID from Sale and Rental Run the query

12 Inner Join: Same Customer and Day

13 Action Create a new query Tables: Customer and Sale Columns: LastName, FirstName, and CustomerID WHERE CustomerID Not In (SELECT CustomerID FROM Rental) Run the query

14 NOT IN Not In subquery to list those who did NOT rent

15 Action Create a new query Table: Inventory Columns: ModelID and Sum(QuantityOnHand) Sort by the Sum descending Run the query Save it as ModelsOnHand Create a new table: SalesCategory Columns: CategoryID, CategoryName, LowLimit, HighLimit Enter data from Figure 5.10

16 Model Quantity On Hand

17 Categories

18 Action Create a new query Columns: ModelID, SumOfQuantityOnHand, CategoryID, and CategoryName Tables: ModelsOnHand and SalesCategory Add the inequality join Run the query

19 Inequality Join Inequality (theta) joins assign the proper category name

20 Sales Categories Find the number of models in each sales category

21 Action Create a new query Columns: CustomerID, LastName, FirstName, and SaleDate Tables: Customer and Sale Set January sale date in WHERE Copy the entire statement Add the word Union Paste the SELECT statement and change the date condition and name to March Run the query

22 UNION Query List customers who bought items in January or in March. Note: it could be done with simple conditions, but it is good practice for UNION.

23 Action Create a new query Enter the CREATE TABLE command Run the query

24 CREATE TABLE Query CREATE TABLE Contacts ( ContactIDint, ManufacturerIDint, LastNamenvarchar(25) null, FirstNamenvarchar(25) null, Phonenvarchar (15) null, nvarchar (120) null, CONSTRAINT pk_Contacts PRIMARY KEY (ContactID), CONSTRAINT fk_ContactsManufacturer FOREIGN KEY (ManufacturerID) REFERENCES Manufacturer(ManufacturerID) ON DELETE CASCADE ) ;

25 Create a Temporary Table CREATE TABLE MyTemp ( IDint, LNamenvarchar(25), FNamenvarchar(25), CONSTRAINT pk_MyTemp PRIMARY KEY (ID) );

26 Action Create a new query Type the INSERT command: INSERT INTO Customer (CustomerID, LastName, FirstName, City, Gender) VALUES (4000, 'Jones', 'Jack', 'Nowhere', 'Male'); Run the query

27 INSERT INTO (One Row) INSERT INTO Customer (CustomerID, LastName, FirstName, City, Gender) VALUES (4000, 'Jones', 'Jack', 'Nowhere', 'Male'); Lab 7 shows how to create sequences so CustomerID is generated automatically.

28 INSERT INTO (Copy Rows) INSERT INTO MyTemp (ID, LName, FName) SELECT CustomerID, LastName, FirstName FROM Customer WHERE City='Sacramento' ;

29 Action Create a new query Columns: Category, ModelYear, and Round(Cost*1.04,2) Table: ItemModel Criteria: Category=’Board’ And ModelYear=2004 Run the query Change the first two lines to be: UPDATE ItemModel SET Cost = Round(Cost*1.04,2) Run the query

30 UPDATE Board Cost Test query to see new values. Delete it before running second query Query to change data in the Cost column

31 Action Create a new SELECT query Columns: ID, LName, FName Table: MyTemp Criteria: ID>100 Test the query Change the SELECT row to DELETE Run the query Run a commit; command

32 DELETE Rows First write a SELECT query to see the rows Second change the SELECT row to DELETE

33 DROP TABLE DROP TABLE MyTemp;

34 Action Create a new query Columns: Category, Sum(RentFee) Tables: Rental, RentItem, Inventory, and ItemModel GROUP BY Category Test the query

35 Query Parameters SELECT Category, Sum(RentFee) AS SumOfRentFee FROM Rental INNER JOIN RentItem ON Rental.RentID=RentItem.RentID INNER JOIN Inventory ON RentItem.SKU=Inventory.SKU INNER JOIN ItemModel ON Inventory.ModelID=ItemModel.ModelID WHERE RentDate Between '01-Jan-2004' And '31-Mar-2004' GROUP BY Category; Need to replace fixed dates with parameters that can be entered by a manager

36 Action Create a new query Copy or paste the code to create the package and procedure Run the package creation code Enter the five commands to execute the parameter query Run the query Change the dates to Oct-Dec Run the query

37 Query Parameters: Procedure CREATE PROCEDURE datetime AS SELECT Category, Sum(RentFee) AS SumOfRentFee FROM Rental INNER JOIN RentItem ON Rental.RentID=RentItem.RentID INNER JOIN Inventory ON RentItem.SKU=Inventory.SKU INNER JOIN ItemModel ON Inventory.ModelID=ItemModel.ModelID WHERE RentDate GROUP BY Category GO

38 Query Parameters Parameters: specify in order Or specify by name