2. Table of Contents
Introduction 3
What is Set Focus? 4
JungleBooks 5
Database Diagram 6
Single Table Query 7
Library 8
Query with Concatenation 9
Advanced Query with Joins 10
Query with Outer Join 11
Join/Union Query 12
Alternate to Previous Query 14
Query Using Temporary Table 16
Query Using Aggregate Function SUM 17
Query with Calculated Column 18
Create View 19
Stored Procedure 20
2
3. ToC (cont’d)
Piggy Bank 22
Database Diagram 23
Stored Proc Example (Make a deposit) 24
DDL Example 25
About Me 21
3
4. Introduction
•This portfolio contains examples of my development skills in MS SQL
Server. It is a work in progress and reflects my current work thus far in the
SetFocus Master’s Program.
•SetFocus utilizes Microsoft Official Curriculum in conjunction with its own
materials to produce some of the following coursework:
• RDBMS and XML
•Querying using Transact SQL
•Implementing and maintaining a MS SQL Server 2005 Database
•Designing an MS SQL Server 2005 Infrastructure
•Designing security for MS SQL Server 2005
•Designing High Availability Database Solutions using MS SQL Server 2005
•Troubleshooting and Optimizing Database Servers using MS SQL Server 2005
•SQL Server Integration Services
•SQL Server Reporting Services
•In addition to the coursework and programming labs, the program includes
challenging real-world projects where I apply the skills learned in class.
Return to TOC 4
5. What is SetFocus?
• The SetFocus SQL Master’s Program is an intensive,
hands-on, project-oriented program allowing
knowledge and valuable experience putting the SQL
skill set to use in a simulated work environment.
• I am currently enrolled and will receive over 300 hours
of in-depth, hands-on experience focused on SQL.
• SetFocus projects are real world projects that are
distributed just as I would receive in a position. I
receive project specifications and am expected to
identify best courses of action with deadlines set for
completion.
Return to TOC 5
6. Junglebooks Scenario
• Junglebooks is a book company which has a database consisting of books,
authors, orders and customers.
• For this project I created a database and submitted a database diagram.
• In this project scenario, I am working with a fictitious application
programmer who is building a client application using .NET.
• To meet the needs of the application programmer, I created queries for
different forms of the application.
Return to TOC 6
8. Single Table Query
The Cheap Books form displays available books below a
certain price. The user enters 15 in the txtUnitPrice form
field. Return ISBN, title and publisher in order by title.
USE JungleBooks;
DECLARE @txtUnitPrice as int = 15;
SELECT ISBN, Title, Publisher
FROM JungleBooks.dbo.Books as Books
WHERE Books.UnitPrice <= @txtUnitPrice
ORDER BY Books.Title;
Return to TOC 8
9. Library Database Scenario
The Library Database is created to support the principal functions of a lending
library’s day-to-day operations.
For this database project, I created queries against the Library database that
returned a number of results using:
• string concatenations
• different types of joins
• UNION statements
• CASE statements
• date manipulation
• and aggregate functions.
Return to TOC 9
10. Query with String Concatenation
Write and execute a query on the Member and Adult tables in the Library database that
returns the firstname, middleinitial, lastname, street, city, state and zip. Concatenate the
firstname, middleinitial and lastname columns into one string and alias as Name. Make sure
that the spacing is appropriate when there is no middle initial due to a NULL or empty string
value. Display records in order by lastname and firstname.
USE library;
SELECT M.firstname + ' ' +
CASE
WHEN M.middleinitial IS NULL THEN ' '
ELSE M.middleinitial + ' '
END
+ M.lastname As Name
, A.street as [Street]
, A.city as [City]
, A.state as [State]
, A.zip as [Zip]
FROM member as M
JOIN adult as A
ON M.member_no = A.member_no
ORDER BY M.lastname, M.firstname;
Return to TOC 10
11. Advanced Query Using Joins
Write and execute a query on the Title, Item and Copy tables that returns the ISBN, copy_no,
on_loan, title, translation, cover for rows in the copy table with an ISBN of 500 or 1000.
Only available books should be displayed and hardback copies should be listed first.
USE library;
SELECT I.isbn
, C.copy_no
, C.on_loan
, T.title
, I.translation
, I.cover
FROM item as I
JOIN copy as C
ON I.isbn = C.isbn
JOIN title as T
ON C.title_no = T.title_no
WHERE (C.on_loan = 'N' AND C.isbn = 500)
or (C.on_loan = 'N' AND C.isbn =
1000)
ORDER BY I.cover;
Return to TOC 11
12. Query with Outer Join
Write and execute a query to retrieve the member’s full name and member_no from the Member table
and the ISBN and log_date values from the Reservation table for member numbers 250, 341, and 1675.
Order the results by member_no and log_date. You should show information for these members, even
if they have no books on reserve.
USE library;
SELECT M.member_no
, M.firstname + ' ' +
CASE
WHEN M.middleinitial IS NULL THEN
' '
ELSE M.middleinitial + ' '
END
+ M.lastname as Name
, R.isbn
, CAST(R.log_date as DATE) as log_date
FROM member as M
LEFT OUTER JOIN reservation as R
ON M.member_no = R.member_no
WHERE m.member_no IN (250, 341, 1675)
ORDER BY m.member_no, r.log_date;
Return to TOC 12
13. USE library;
SELECT I.isbn
Join/Union Query ,T.title
,A.member_no
,M.lastname + ', ' +
M.firstname + ' ' +
Write and execute a query to CASE
WHEN M.middleinitial IS NULL THEN ' '
retrieve the member’s full ELSE M.middleinitial + ' '
END as FullName,
name and member_no from 'Adult' as [Status]
the Member table and the FROM adult as A
JOIN member as M
ISBN and log_date values ON A.member_no = M.member_no
from the Reservation table JOIN reservation as R
ON R.member_no = A.member_no
for member numbers 250, JOIN item as I
ON I.isbn = R.isbn
341, and 1675. Order the JOIN title as T
results by member_no and ON T.title_no = I.title_no
WHER E I.isbn = '288'
log_date. You should show UNION ALL
information for these
SELECT I.isbn
members, even if they have ,T.title
,J.member_no
no books on reserve. ,M.lastname + ', ' +
M.firstname + ' ' +
CASE
WHEN M.middleinitial IS NULL THEN ' '
ELSE M.middleinitial + ' '
END as FullName,
'Juvenile' as [Status]
FROM Juvenile as J
JOIN member as M
ON J.member_no = M.member_no
JOIN reservation as R
ON R.member_no = J.member_no
JOIN item as I
ON I.isbn = R.isbn
JOIN title as T
ON T.title_no = I.title_no
Return to TOC 13
WHERE I.isbn = '288'
15. Alternate to Previous Query
USE library;
SELECT DISTINCT I.isbn
,T.title
,M.member_no
Write the above statement ,M.lastname + ', ' +
M.firstname + ' ' +
again using a CASE statement. CASE
You cannot JOIN to the Adult WHEN M.middleinitial IS NULL THEN ' '
ELSE M.middleinitial + ' '
or Juvenile tables. Compare END as FullName,
the two versions and [Status] =
CASE
determine which one is more WHEN EXISTS (SELECT * FROM dbo.adult
WHERE dbo.adult.member_no=m.member_no) THEN 'Adult'
efficient. Cut and paste the ELSE 'Juvenile'
proof of your research. (No END
FROM
need to copy the output of member as M
records) JOIN reservation as R
ON R.member_no = M.member_no
JOIN item as I
ON I.isbn = R.isbn
JOIN title as T
ON T.title_no = I.title_no
WHERE R.isbn = '288'
ORDER BY FullName
Return to TOC 15
17. Query Using Temporary Table to Store Records
Write and execute a query that returns the member_no, full name, out_date, due_date, and
title columns from the Loan, Member, and Title tables. Convert the datetime values from
the out_date and due_date columns to char(12), format 101. Restrict the results to books
which were due prior to the current date. Load the records into a temporary table called
#overdue. Leave the query window open after you create the temporary table.
USE library;
IF OBJECT_ID('tempdb.dbo.#overdue') IS NOT NULL
DROP TABLE dbo.#overdue;
GO
SELECT M.member_no
, M.firstname + ' ' +
CASE
WHEN M.middleinitial IS NULL THEN ' '
ELSE M.middleinitial + ' '
END + ' ' +
M.lastname as FullName
, CONVERT(char(12), L.out_date, 101) as [Out Date]
, CONVERT(char(12), L.due_date, 101) as [Due Date]
, T.title
INTO dbo.#overdue
FROM loan as L
JOIN member as M
ON L.member_no = M.member_no
JOIN title as T
ON T.title_no = L.title_no
WHERE due_date < CURRENT_TIMESTAMP
Return to TOC ORDER BY FullName; 17
18. Query Using Aggregate Function SUM
Write and execute a query that returns member_no, firstname, lastname and sum of
fine_paid for members who have paid the highest fines to date. Members should only
appear once in the list. Display the highest fine first. If more than one member has paid the
same amount display the records in order by member_no.
USE library;
SELECT M.member_no
, M.firstname
, M.Lastname
, CAST(SUM(LH.fine_paid) as
Numeric(5,2)) as FinePaid
FROM member as M
JOIN loanhist as LH
ON M.member_no = LH.member_no
GROUP BY M.member_no, M.firstname,
M.lastname
HAVING SUM(LH.fine_paid) IS NOT NULL
ORDER BY
FinePaid DESC,
M.member_no;
Return to TOC 18
19. Query with Calculated Column
Write and execute a query on the Reservation table that returns the ISBN, title and Total.
The Total column is a calculated column which represents the number of members wishing
to reserve a particular book.
USE library;
SELECT R.isbn
,(SELECT T.title
FROM title as T
WHERE I.title_no =
T.title_no) as Title
, COUNT(R.member_no) as
Total
FROM reservation as R
JOIN item as I
ON I.isbn = R.isbn
GROUP BY R.isbn, I.title_no
ORDER BY Title
Return to TOC 19
20. Create View
Create a view in the TSQLFundamentals2008 database that returns the orderid,
day of the week (Spelled Out), the name of the month (spelled Out),
the day of the month, and the year based on the order date in the sales.orders table.
USE TSQLFundamentals2008;
IF OBJECT_ID('Sales.View1') IS NOT NULL
DROP VIEW Sales.View1;
GO
Create View Sales.View1
As
SELECT orderid
, DATENAME(WEEKDAY, orderdate) as "DAY"
, DATENAME(MONTH, orderdate) as "MONTH"
, DATENAME(DAY, orderdate) as "Date"
, DATENAME(YEAR, orderdate) as "Year"
FROM Sales.Orders
Return to TOC 20
21. Stored Procedure
Create a stored procedure in the TSQLFundamentals2008 database that returns the order ID,
the order date, the ship country. The employee full name, and the company name.
The ship country should be a parameter and the result set should be sorted by order date
from most recent to oldest.
USE TSQLFundamentals2008;
if OBJECT_ID('StoredProc1', 'P') IS NOT NULL
DROP PROC StoredProc1;
Go
CREATE PROC StoredProc1
@ShipCntry as varchar(20) = 'USA'
AS
SELECT S.orderid, S.orderdate, S.shipcountry
FROM Sales.Orders AS S
JOIN HR.Employees as E
on S.empid = E.empid
JOIN Sales.Shippers as Ship
ON Ship.shipperid = S.shipperid
WHERE S.shipcountry = @ShipCntry
Order BY S.orderdate
Return to TOC 21
22. Piggy Bank Project
• The PiggyBank Database simulates bank operations such as Overdraft
Accounts, Customer and Accounts relationships, and Transactions.
• This database has been used for a couple of projects:
– Create an Entity Relationship Diagram given some specifications such
as Overdraft Fees, Error Information when a transaction fails, Login
Failures, and Customer/Account relationships.
– Design back-end stored procedures, DDL/DML triggers, parameterized
stored procedures that select from views. Some of the actions created
are Create/Update Customer, Create Checking/Savings Accounts,
Deposit/Withdrawal Procedures, Simulate ATM Balances, Customer
Account History (bank statements) and Use of Overdraft Accounts.
Return to TOC 22
26. Mini AdventureWorks Project
• The company Mini-AD (short for Mini-AdventureWorks) is interested in taking
historical spreadsheet (CSV) data for their list of products, vendors, and
purchase order history, and loading the data into a SQL Server database.
• Mini-AD wants the load process to work on a go-forward basis, so that
new/modified products/vendors/orders can be loaded in SQL Server as well
• Mini-AD’s load process for orders should validate that any incoming orders with
product numbers or vendor numbers that do not match an existing
product/vendor number should NOT be written to the SQL Server database.
Instead, this data should be written to an exception file and emailed.
• Mini-AD also wishes to build two reports : one for top vendor and product
sales, and the second for sales by vendor and ship method across years.
SSIS / SSRS Report Project 26
37. BlockFlix Project
• As part of a team I helped develop a database
complete with SSIS and SSRS services for a
fictional movie rental company called “BlockFlix”
• The general requirements included the creation
of a central, online database that handled movie
rentals through online streaming
video, kiosks, and brick-and-mortar stores.
• My primary responsibility involved using SSIS to
import movie information into the DB from an
XML file.
Return to TOC 37
39. Add Movies from XML
Phase One: Data Flow task
Also puts info into staging table
Inserts Movie and Cast info directly
where Unique Keys weed out
into their respective tables
duplications
Phase Two: Execute SQL Task
Once the XML data has been refined, the SQL task inserts the info into the
MovieCast table where Movies are associated with their respective cast
members
Phase Three
Run clean up to clear staging tables and save space.
Return to TOC 39
40. Inside the Dataflow Task Get
XML
file
Convert XML to
correct data types
Send directly
to DB table
Capture
errors
Return to TOC 40