Solutions to the Problems in SQL Practice Problems by Sylvia Moestl Vasilik. John Weatherwax

Similar documents
ISM 4212/4480 Milestone V Submission Sample

Northwind Database. Sample Output from TechWriter 2007 for Databases

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Module 11: Implementing Triggers

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

How to use SQL to work with a MySQL database

Assignment Grading Rubric

Introduction to relational databases and MySQL

Submit the MS Access Database file that contains the forms created in this lab.

Views in SQL Server 2000

QUETZALANDIA.COM. 5. Data Manipulation Language

Answer: The tables being joined each have two columns with the same name and compatible data types, and you want to join on both of the columns.

Project 7: Northwind Traders Order Entry

Structured Query Language (SQL)

9 No limit. Number of UNIONs 9 No limit. 30 No limit. 1 No limit

SQL Server 2012 Development Course

Assignment 6: SQL III Solution

Assignment 6: SQL III

Grouping and Sorting

MySQL. Prof.Sushila Aghav

Microsoft_ Querying Microsoft SQL Server 2012

COOKBOOK Creating an Order Form

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types

Skills Exam Objective Objective Number. Creating crosstab queries Create a crosstab query

Jarek Szlichta

FIGURE 2.57 Denver Rooms 3 Guests Query. '3 LocationlD C", Address. Orders. Service. ServlceNlIme PerPersonCharge

Appendix A. Using DML to Modify Data. Contents: Lesson 1: Adding Data to Tables A-3. Lesson 2: Modifying and Removing Data A-8

Consuming and Manipulating Data O BJECTIVES

MIS2502: Review for Exam 2. JaeHwuen Jung

Temporal Data Warehouses: Logical Models and Querying

-- Jerad Godsave -- November 9, CIS Assignment A8.sql

Chris Singleton 03/12/2017 PROG 140 Transactions & Performance. Module 8 Assignment

DATABASE MANAGEMENT SYSTEMS

MIS2502: Review for Exam 2. Jing Gong

Database Logical Design

Database Logical Design

Exam code: Exam name: Database Fundamentals. Version 16.0

Chapter 3 Introduction to relational databases and MySQL

Stored Procedures and Functions. Rose-Hulman Institute of Technology Curt Clifton

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://

More on MS Access queries

Advisor Answers. Match multiple items in a query. December, 2005 VFP 9/8/7

How to use SQL to create a database

Database Wizard Guide. i-net Designer

CONCAT SUBSTR LOWER (*) All three will be evaluated simultaneously. Correct

CHAPTER: 4 ADVANCE SQL: SQL PERFORMANCE TUNING (12 Marks)

Load generation tools. By Drew Hamre

GETTING STARTED WITH CODE ON TIME Got data? Generate modern web apps in minutes. Learn to create sophisticated web apps with Code On Time application

Automatic Conflict Resolution to Integrate Relational Schema. A dissertation submitted in partial satisfaction of the requirements for the degree of

Access Objects. Tables Queries Forms Reports Relationships

Study Guide for: Oracle Database SQL Certified Expert Exam Guide (Exam 1Z0-047)

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

SQL is a standard language for accessing and manipulating databases.

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

COPYRIGHTED MATERIAL. Chapter. Database Logical Modeling MICROSOFT EXAM OBJECTIVES COVERED IN THIS CHAPTER:

KillTest. 半年免费更新服务

Working with Data in ASP.NET 2.0 :: Using TemplateFields in the DetailsView Control Introduction

Test: Mid Term Exam Semester 2 Part 1 Review your answers, feedback, and question scores below. An asterisk (*) indica tes a correct answer.

Microsoft Exam Querying Microsoft SQL Server 2012 Version: 13.0 [ Total Questions: 153 ]

Smart Database [Stored Procedures - Functions Trigger]

COOKBOOK Row-Level Security

Reporting Functions & Operators

CS363: Structured Query Language for Data Management 1. CS363: Structured Query Language (SQL) for Data Management. Team Echo Group Project - Final

L e a r n S q l select where

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Coveo Platform 7.0. Database Connector Guide

1z Exam Code: 1z Exam Name: Oracle Database SQL Expert

Data Modelling and Databases Exercise dates: March 22/March 23, 2018 Ce Zhang, Gustavo Alonso Last update: March 26, 2018.

MICROSOFT EXAM QUESTIONS & ANSWERS

Extend your queries with APPLY

IMPORTING DATA IN PYTHON I. Introduction to relational databases

8. Orders shipping to France or Belgium

PassReview. PassReview - IT Certification Exams Pass Review

Project Spring Item. Field Data Type Description Example Constraints Required. The ID for this table; autoincremented.

SELF TEST. List the Capabilities of SQL SELECT Statements

RESTRICTING AND SORTING DATA

Natural-Join Operation

Oracle 1Z Oracle Database 12c SQL. Download Full Version :

--NOTE: We are now using a different database. USE AdventureWorks2012;

Importing into Neo4j - No witty subtitle - Dave July 27

Intermediate SQL: Aggregated Data, Joins and Set Operators

Working with Actuate Query

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

Database Tables Lookup Wizard Relationships Forms Subforms Queries Reports

Preventing the Alteration of Dependent Objects:The SCHEMABINDING Option

Exam Actual. Higher Quality. Better Service! QUESTION & ANSWER

Generating crosstabs in VFP

CIS 363 MySQL. Chapter 12 Joins Chapter 13 Subqueries

Instructor: Craig Duckett. Lecture 14: Tuesday, May 15 th, 2018 Stored Procedures (SQL Server) and MySQL

Microsoft Exam Transition Your MCTS on SQL Server 2008 to MCSA: SQL Server 2012, Part 1 Version: 7.8 [ Total Questions: 183 ]

Kaotii.

ADMISTRATOR S GUIDE INTERACTIVE REPORTING VERSION 3.3. Volume. Sales Analysis and Reporting Products

Oracle Academy Amazing Books Part 1: Building Tables and Adding Constraints

Oracle EXAM - 1Z Oracle Database SQL Expert. Buy Full Product.

Exam #1 Review. Zuyin (Alvin) Zheng

SECTION 1 DBMS LAB 1.0 INTRODUCTION 1.1 OBJECTIVES 1.2 INTRODUCTION TO MS-ACCESS. Structure Page No.

Database Programming with SQL

Oracle 1Z Oracle Database SQL Expert. Download Full Version :

exam.87q.

Transcription:

Solutions to the Problems in SQL Practice Problems by Sylvia Moestl Vasilik John Weatherwax 1

Text copyright c 2018 John L. Weatherwax All Rights Reserved Please Do Not Redistribute Without Permission from the Author 2

To my family. 3

Introduction This is my solution manual to some of the problems in the excellent book: SQL Practice Problems by Sylvia Moestl Vasilik While the original book has solution in SQL Server I found it easiest to work the problems in SQLite and thus these solutions are in that dialect of SQL. I hope you find them useful. /* P 1: */ * Shippers; /* P 2 (EPage 20): */ CategoryName, Description Categories; /* P 3: */ FirstName, LastName, HireDate Employees Title = Sales Representative ; /* P 4 (EPage 25): */ FirstName, LastName, HireDate Employees Title = Sales Representative AND = USA ; /* P 5 (EPage 28): */ OrderID, OrderDate EmployeeID = 5; 4

/* P 6 (EPage 32): */ SupplierID, ContactName, ContactTitle Suppliers ContactTitle <> Manager ; /* P 7 (EPage 35): */ ProductID, ProductName Products ProductName LIKE %queso% ; /* P 8 (EPage 38): */ OrderID, CustomerID, Ship Ship IN ( France, Belgium ); /* P 9 (EPage 41): */ OrderID, CustomerID, Ship Ship IN ( Brazil, Mexico, Argentina, Venezuela ); /* P 10 (EPage 44): */ FirstName, LastName, Title, BirthDate Employees BirthDate ASC; 5

/* P 11 (EPage 44): */ FirstName, LastName, Title, strftime( %Y-%m-%d, BirthDate) AS DateOnlyBirthDate Employees BirthDate ASC; /* P 12 (EPage 50): */ FirstName, LastName, FirstName LastName AS FullName Employees; /* P 13 (EPage 53): */ OrderID, ProductID, UnitPrice, Quantity, UnitPrice * Quantity AS TotalPrice [Order Details] OrderID, ProductID; /* P 14 (EPage 56): */ COUNT(*) AS Total ; /* P 15 (EPage 59): */ MIN(OrderDate) AS FirstOrder ; /* P 16 (EPage 62): */ 6

; /* P 17 (EPage 65): */ ContactTitle, COUNT(*) AS TotalContractTitle ContactTitle TotalContractTitle DESC; /* P 18 (EPage): */ p.productid, p.productname, s.companyname Products p LEFT JOIN Suppliers s ON p.supplierid = s.supplierid; /* P 19 (EPage 71): */ OrderID, date(orderdate) AS OrderDate, CompanyName LEFT JOIN Shippers ON.ShipVia = Shippers.ShipperID OrderID < 10300 OrderID; /* P 20 (EPage 76) */ CategoryName, COUNT(*) AS Count Categories JOIN Products ON Categories.CategoryID = Products.CategoryID CategoryName 7

Count DESC; /* P 21 (EPage 79) */, City, COUNT(*) AS Total, City Total DESC; /* P 22 (EPage 82) */ ProductID, ProductName, UnitsInStock, ReorderLevel Products UnitsInStock <= ReorderLevel ProductID; /* P 23 (EPage 85) */ ProductID, ProductName, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued Products (UnitsInStock + UnitsOnOrder) <= ReorderLevel AND Discontinued= 0 ProductID; /* P 24 (EPage 88) */ 8

CustomerID, CompanyName, Region (CASE WHEN Region IS NULL THEN 1 ELSE 0 END), Region, CustomerID; /* P 25 (EPage 92) */ Ship, AVG(Freight) AS AverageFreight Ship AverageFreight DESC LIMIT 3; /* P 26 (EPage 97) */ Ship, AVG(Freight) AS AverageFreight strftime( %Y, OrderDate) = 1997 Ship AverageFreight DESC LIMIT 3; /* P 28 (EPage 105) */ Ship, AVG(Freight) AS AverageFreight OrderDate >= ( datetime(julianday(max(orderdate)) - 365) ) Ship 9

AverageFreight DESC LIMIT 3; /* P 29 (EPage 110) */.EmployeeID, Employees.LastName,.OrderID, Products.ProductName, [Order Details].Quantity LEFT JOIN Employees ON.EmployeeID = Employees.EmployeeID LEFT JOIN [Order Details] ON.OrderID = [Order Details].OrderID LEFT JOIN Products ON [Order Details].ProductID = Products.ProductID.OrderID, [Order Details].ProductID LIMIT 20; /* P 30 (EPage 113) */.CustomerID AS _CustomerID,.CustomerID AS _CustomerID LEFT JOIN ON.CustomerID =.CustomerID.CustomerID IS NULL.CustomerID; /* P 31 (EPage 117) */.CustomerID AS _CustomerID,.CustomerID AS _CustomerID LEFT JOIN ON.CustomerID =.CustomerID AND.EmployeeID = 4.CustomerID IS NULL.CustomerID; 10

/* P 32 (EPage 121) */.CustomerID,.CompanyName,.OrderID, SUM([Order Details].UnitPrice * [Order Details].Quantity) AS TotalOrderAmount JOIN ON.CustomerID =.CustomerID JOIN [Order Details] ON.OrderID = [Order Details].OrderID strftime( %Y,.OrderDate) = 1998.CustomerID,.OrderID HAVING TotalOrderAmount > 10000 TotalOrderAmount DESC; /* P 33 (EPage 126) */.CustomerID,.CompanyName, SUM([Order Details].UnitPrice * [Order Details].Quantity) AS TotalOrderAmount JOIN ON.CustomerID =.CustomerID JOIN [Order Details] ON.OrderID = [Order Details].OrderID strftime( %Y,.OrderDate) = 1998.CustomerID HAVING TotalOrderAmount > 15000 TotalOrderAmount DESC; /* P 34 (EPage 129) */.CustomerID,.CompanyName, SUM([Order Details].UnitPrice * [Order Details].Quantity) AS TotalWithoutDiscount, SUM([Order Details].UnitPrice * [Order Details].Quantity * (1-[Order Details].Discount)) AS TotalWithDiscount JOIN ON.CustomerID =.CustomerID 11

JOIN [Order Details] ON.OrderID = [Order Details].OrderID strftime( %Y,.OrderDate) = 1998.CustomerID HAVING TotalWithDiscount > 15000 TotalWithDiscount DESC; /* P 35 (EPage 133) */.EmployeeID,.OrderID, date(.orderdate) date(.orderdate, start of month, +1 month, -1 day ) = date(.orderdate).employeeid,.orderid; /* P 36 (EPage 136) */.OrderID, COUNT(.OrderID) AS TotalOrderDetails JOIN [Order Details] ON.OrderID = [Order Details].OrderID.OrderID TotalOrderDetails LIMIT 10; /* P 37 (EPage 139) */ /* To select a fixed number of random rows we can use */ OrderID RANDOM() LIMIT 10; 12

/* To select a fixed percentage of random rows we can use */ OrderID RANDOM() LIMIT CAST(0.02 * ( COUNT(*) ) AS INTEGER); /* P 38 (EPage 142) */ OrderID, Quantity, COUNT(*) AS Number [Order Details] Quantity >= 60 OrderID, Quantity HAVING Number > 1; /* P 39 (EPage 146) */ /* Using a CTE (common table expression) */ WITH PossibleOrderIDs AS ( OrderID [Order Details] Quantity >= 60 OrderID, Quantity HAVING COUNT(*) > 1 ) OrderID, ProductID, UnitPrice, Quantity, Discount [Order Details] 13

OrderID IN PossibleOrderIDs OrderID, Quantity; /* P 40 (EPage 149) */ For this problem we add the keyword DISTINCT in the statement in the subquery in the provided SQL. /* P 41 (EPage 152) */ OrderID, OrderDate, RequiredDate, ShippedDate ShippedDate >= RequiredDate OrderDate ASC; /* P 42 (EPage 156) */.EmployeeID, Employees.LastName, COUNT(*) As TotalLate JOIN Employees ON.EmployeeID = Employees.EmployeeID ShippedDate >= RequiredDate.EmployeeID, Employees.LastName TotalLate DESC; /* P 43-47 (EPage 159) */ /* Using two CTE (common table expressions) */ WITH -- Total orders TotalNumberOf AS ( EmployeeID, COUNT(*) AS Total 14

EmployeeID ), -- Late orders TotalLate AS ( EmployeeID, COUNT(*) AS TotalLate ShippedDate >= RequiredDate EmployeeID ) DISTINCT.EmployeeID, Employees.LastName, Total, TotalLate, ROUND(CAST(TotalLate AS FLOAT)/Total, 2) AS PercentLate JOIN Employees ON.EmployeeID = Employees.EmployeeID JOIN TotalNumberOf ON.EmployeeID = TotalNumberOf.EmployeeID LEFT JOIN TotalLate ON.EmployeeID = TotalLate.EmployeeID PercentLate DESC; /* P 48-49 (EPage 177) */ WITH CustomerOrderSizes AS (.CustomerID,.CompanyName, SUM([Order Details].UnitPrice * [Order Details].Quantity) AS TotalOrderAmount JOIN ON.CustomerID =.CustomerID JOIN [Order Details] ON.OrderID = [Order Details].OrderID strftime( %Y,.OrderDate) = 1998.CustomerID.CustomerID ) 15

CustomerID, TotalOrderAmount, CASE WHEN ((TotalOrderAmount > 0) AND (TotalOrderAmount <= 1000)) THEN low WHEN ((TotalOrderAmount > 1000) AND (TotalOrderAmount <= 5000)) THEN medium WHEN ((TotalOrderAmount > 5000) AND (TotalOrderAmount < 10000)) THEN high ELSE very high END AS CustomerGroup CustomerOrderSizes CustomerID; /* P 50 (EPage 185) */ WITH CustomerOrderSizes AS (.CustomerID,.CompanyName, SUM([Order Details].UnitPrice * [Order Details].Quantity) AS TotalOrderAmount JOIN ON.CustomerID =.CustomerID JOIN [Order Details] ON.OrderID = [Order Details].OrderID strftime( %Y,.OrderDate) = 1998.CustomerID.CustomerID ), CustomerGroups AS ( TotalOrderAmount, CASE WHEN ((TotalOrderAmount > 0) AND (TotalOrderAmount <= 1000)) THEN low WHEN ((TotalOrderAmount > 1000) AND (TotalOrderAmount <= 5000)) THEN medium WHEN ((TotalOrderAmount > 5000) AND (TotalOrderAmount < 10000)) THEN high ELSE very high END AS CustomerGroup CustomerOrderSizes ) CustomerGroup, COUNT(*) AS TotalInGroup, ROUND(CAST(COUNT(*) AS DOUBLE)/( COUNT(*) CustomerGroups), 2) 16

AS PercentageInGroup CustomerGroups CustomerGroup PercentageInGroup DESC; /* P 51 (EPage 189) */ WITH CustomerOrderSizes AS (.CustomerID,.CompanyName, SUM([Order Details].UnitPrice * [Order Details].Quantity) AS TotalOrderAmount JOIN ON.CustomerID =.CustomerID JOIN [Order Details] ON.OrderID = [Order Details].OrderID strftime( %Y,.OrderDate) = 1998.CustomerID.CustomerID ), CustomerGroups AS ( CustomerGroupName AS CustomerGroup, TotalOrderAmount CustomerOrderSizes JOIN CustomerGroupThresholds ON (RangeBottom < TotalOrderAmount) AND (TotalOrderAmount <= RangeTop) ) CustomerGroup, COUNT(*) AS TotalInGroup, ROUND(CAST(COUNT(*) AS DOUBLE)/( COUNT(*) CustomerGroups), 2) AS PercentageInGroup CustomerGroups CustomerGroup PercentageInGroup DESC; /* P 52 (EPage 193) */ 17

Suppliers UNION ; /* P 53 (EPage 196) */ WITH SupplierCountries AS ( DISTINCT Suppliers ), CustomerCountries AS ( DISTINCT ) -- Using ideas from: http://www.sqlitetutorial.net/sqlite-full-outer-join/ SupplierCountries. AS Supplier, CustomerCountries. AS Customer SupplierCountries LEFT JOIN CustomerCountries USING() UNION SupplierCountries. AS Supplier, CustomerCountries. AS Customer CustomerCountries LEFT JOIN SupplierCountries USING() NOT ((Supplier IS NULL) AND (Customer IS NULL)); /* P 54 (EPage 200) */ WITH -- get suppliers countries (and count) SupplierCountries AS ( 18

, COUNT(*) AS TotalSuppliers Suppliers IS NOT NULL ), -- get customer countries (and count) CustomerCountries AS (, COUNT(*) AS Total IS NOT NULL ), -- get a union of all countries AllCountries AS ( Suppliers IS NOT NULL UNION IS NOT NULL ) ac., IFNULL(sc.TotalSuppliers, 0) AS TotalSuppliers, IFNULL(cc.Total, 0) AS Total AllCountries ac LEFT JOIN SupplierCountries sc ON ac. = sc. LEFT JOIN CustomerCountries cc ON ac. = cc. ac.; /* P 55 (EPage 204) */ 19

-- -- Using ideas from: -- https://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ -- WITH CountriesFirstOrder AS ( -- get the first order date in each country Ship, MIN(OrderDate) AS FirstOrderDate Ship ) -- select the other information for this first order cfo.ship, CustomerID, OrderID, date(orderdate) AS OrderDate CountriesFirstOrder cfo LEFT JOIN o ON (cfo.firstorderdate = o.orderdate) AND (cfo.ship = o.ship) cfo.ship; /* P 56 (EPage 209) */ io.customerid, io.orderid AS InitialOrderID, date(io.orderdate) AS InitialOrderDate, so.orderid AS NextOrderID, date(so.orderdate) AS NextOrderDate, CAST(julianday(so.OrderDate) - julianday(io.orderdate) AS INTEGER) AS DaysBetween -- io = initial order io -- so = second order JOIN so ON (io.customerid = so.customerid) AND (io.orderid < so.orderid) DaysBetween <= 5; /* P 57 (EPage 217) SQLite does not currently support windowing functions */ 20