Skip to content
Open

Save #21

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"files.autoSave": "afterDelay",
"screencastMode.onlyKeyboardShortcuts": true,
"terminal.integrated.fontSize": 18,
"workbench.activityBar.visible": true,
"workbench.colorTheme": "Visual Studio Dark",
"workbench.fontAliasing": "antialiased",
"workbench.statusBar.visible": true,
Expand All @@ -36,5 +35,8 @@
"password": "mariadb"
}
],
"sqltools.useNodeRuntime": true
"sqltools.useNodeRuntime": true,
"githubPullRequests.ignoredPullRequestBranches": [
"main"
]
}
180 changes: 180 additions & 0 deletions Chapter 2/Finding Duplicate Rows/Duplicate1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
"CustomerID","FirstName","LastName","NUM_ORDERS"
133,"Earl","Carter","2"
292,"Roger","Welch","2"
469,"Emily","Mitchell","2"
561,"Kelly","Campbell","2"
789,"Ronald","Grant","2"
916,"Melissa","Rose","2"
791,"Nancy","Graham","2"
866,"Frank","Montgomery","2"
971,"Pamela","Carpenter","2"
126,"James","Howard","2"
153,"Matthew","Jacobs","2"
415,"Bruce","Morrison","2"
529,"Charles","Shaw","2"
734,"Cynthia","Howard","2"
925,"Gary","Ferguson","2"
555,"Sandra","Willis","2"
653,"Frank","Hansen","2"
666,"Alan","Stephens","2"
717,"Catherine","Pierce","2"
822,"Mary","Foster","2"
846,"Julie","Mills","2"
107,"Brian","Mcdonald","1"
122,"Jean","Palmer","1"
141,"Elizabeth","Jordan","1"
157,"Sarah","Cooper","1"
189,"Ryan","Scott","1"
204,"Donald","Gilbert","1"
227,"Michael","Anderson","1"
265,"Julia","Morrison","1"
330,"Paula","Campbell","1"
343,"Raymond","Brooks","1"
367,"Jonathan","Price","1"
400,"Joseph","Hernandez","1"
444,"Ruth","Holmes","1"
454,"Anna","Porter","1"
487,"Jeffrey","Robertson","1"
513,"Debra","Johnston","1"
538,"Terry","Hansen","1"
592,"Tammy","Medina","1"
624,"Johnny","Lee","1"
658,"Betty","Johnson","1"
669,"Carl","Arnold","1"
678,"Samuel","Jordan","1"
704,"Christopher","Brooks","1"
718,"Doris","Anderson","1"
746,"Carol","Reed","1"
768,"Ruby","Rice","1"
803,"Gregory","Young","1"
811,"Paula","Lopez","1"
830,"Paul","Ferguson","1"
847,"Denise","Howell","1"
862,"Antonio","Evans","1"
895,"Carolyn","Clark","1"
905,"Brenda","Daniels","1"
935,"Catherine","Hart","1"
968,"Steven","Taylor","1"
978,"Karen","Frazier","1"
1009,"Willie","Robinson","1"
1065,"Teresa","Arnold","1"
1080,"Pamela","Alvarez","1"
108,"Nicholas","Clark","1"
125,"Kathy","Alexander","1"
135,"Doris","Watkins","1"
144,"Anne","Hicks","1"
164,"Joshua","Jordan","1"
191,"Antonio","Freeman","1"
210,"Norma","Ramos","1"
228,"Jason","Rivera","1"
268,"Anne","Campbell","1"
302,"Deborah","Kennedy","1"
334,"Lois","Warren","1"
347,"Brandon","Elliott","1"
369,"Daniel","Franklin","1"
413,"Angela","Crawford","1"
447,"Theresa","Ross","1"
461,"Albert","Cunningham","1"
482,"Andrew","Ramos","1"
507,"Janet","Crawford","1"
528,"Adam","Wood","1"
544,"Patrick","Snyder","1"
570,"Patrick","Weaver","1"
600,"Phyllis","Thomas","1"
633,"Michelle","Scott","1"
660,"Virginia","Porter","1"
670,"Sandra","Jones","1"
693,"Steven","Schmidt","1"
712,"Anthony","Collins","1"
724,"Russell","Wood","1"
751,"Paula","Gibson","1"
778,"Earl","Peterson","1"
804,"Deborah","Nguyen","1"
813,"Kevin","Hughes","1"
831,"Brian","Cunningham","1"
849,"Randy","Dunn","1"
898,"Henry","Chavez","1"
911,"Bobby","Nelson","1"
917,"Emily","Reyes","1"
945,"Gloria","Ramos","1"
982,"Karen","Martin","1"
1039,"William","Harper","1"
1066,"Matthew","Snyder","1"
1093,"Helen","Carter","1"
110,"Doris","Ramirez","1"
136,"Rebecca","Graham","1"
166,"Thomas","Henderson","1"
193,"Barbara","Bell","1"
211,"Benjamin","Garcia","1"
248,"Anna","Wallace","1"
280,"Katherine","Johnston","1"
307,"Jack","Foster","1"
336,"Aaron","Robertson","1"
348,"Gloria","Marshall","1"
371,"Melissa","Wheeler","1"
448,"James","Lawrence","1"
466,"Matthew","Turner","1"
483,"Sara","Henry","1"
509,"Bonnie","Grant","1"
552,"Paul","Carpenter","1"
571,"Patricia","Lopez","1"
606,"Heather","Gardner","1"
652,"John","Barnes","1"
661,"Nancy","Walker","1"
672,"Sharon","Thompson","1"
695,"Stephen","Davis","1"
714,"Kimberly","Moreno","1"
752,"Arthur","Myers","1"
782,"Barbara","Watkins","1"
794,"George","Dunn","1"
805,"Andrea","Young","1"
820,"Eric","Dunn","1"
833,"Mary","Ross","1"
853,"Richard","Castillo","1"
868,"John","Scott","1"
899,"Keith","Castillo","1"
914,"Deborah","Spencer","1"
949,"Lois","Andrews","1"
974,"Rose","Cooper","1"
985,"Steven","Cooper","1"
1043,"Walter","Henry","1"
1067,"Jose","Nelson","1"
1098,"Michael","Gonzalez","1"
118,"Beverly","Meyer","1"
128,"Randy","Gibson","1"
139,"Brian","Mccoy","1"
155,"Sarah","Taylor","1"
181,"Louise","Cook","1"
199,"Janet","Perez","1"
225,"Jonathan","Gonzales","1"
250,"Alice","Fisher","1"
281,"Douglas","Dean","1"
308,"Alice","Gutierrez","1"
338,"Michael","Johnston","1"
359,"Ruth","Garcia","1"
387,"Timothy","Powell","1"
422,"Ernest","Hanson","1"
450,"Janice","Lopez","1"
468,"Billy","Roberts","1"
486,"Billy","Martinez","1"
511,"Kenneth","Matthews","1"
533,"Ann","Gordon","1"
586,"Harold","Hamilton","1"
610,"Martin","Reid","1"
675,"Amy","Lawson","1"
699,"Doris","Riley","1"
743,"Kelly","Martinez","1"
758,"Louis","Martin","1"
783,"Brandon","Adams","1"
798,"Lillian","Alexander","1"
810,"Diane","Howard","1"
859,"Mildred","Castillo","1"
883,"Melissa","Hernandez","1"
903,"Christina","Knight","1"
915,"Louise","Jones","1"
929,"Shawn","Adams","1"
955,"Lawrence","Johnston","1"
977,"Christopher","Brooks","1"
1002,"Shawn","Bennett","1"
1055,"Jesse","Franklin","1"
1071,"Cynthia","Matthews","1"
10 changes: 8 additions & 2 deletions Chapter 2/Finding Duplicate Rows/Duplicate1.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
-- Select the customer information and how many orders they have
SELECT Customer.CustomerID,
Customer.FirstName,
Customer.FirstName,
Customer.LastName,
-- Total number of orders for each customer
COUNT(1) as NUM_ORDERS
FROM Customer
-- Start with the Customer table, Join with the Orders table, matching each customer to their orders
JOIN Orders ON Customer.CustomerID = Orders.CustomerID
GROUP BY Customer.CustomerID,
-- Group rows so that each customer is one line in the result
-- Required because we've using count()
GROUP BY Customer.CustomerID,
Customer.FirstName,
Customer.LastName
-- Sort the result by number of orders (Highest first)
ORDER BY NUM_ORDERS DESC
10 changes: 10 additions & 0 deletions Chapter 2/Finding Duplicate Rows/Duplicate2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"FirstName","LastName","-- Number of rows for each (FirstName, LastName) Combination
COUNT(1)"
"Brandon","Murray","2"
"Carolyn","Welch","2"
"Christopher","Brooks","2"
"Cynthia","Greene","2"
"Frances","Jones","2"
"Lori","Harris","2"
"Randy","Gonzales","2"
"Randy","Parker","2"
9 changes: 7 additions & 2 deletions Chapter 2/Finding Duplicate Rows/Duplicate2.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
SELECT FirstName, LastName, COUNT(1)
SELECT FirstName,
LastName,
-- Number of rows for each (FirstName, LastName) Combination
COUNT(1)
FROM Customer
GROUP BY FirstName, LastName
GROUP BY FirstName,
LastName
-- Only keep groups where the count is greater than 1
HAVING COUNT(1) > 1
15 changes: 11 additions & 4 deletions Chapter 2/Finding Duplicate Rows/Duplicate3.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
SELECT FirstName,
-- Select customer identity details and count how many times
-- each unique combination of these fields appears
SELECT
FirstName,
LastName,
Address,
City,
State,
Zipcode,
COUNT(1)
COUNT(1) -- number of rows that share the exact same values
FROM Customer
GROUP BY FirstName,
GROUP BY
FirstName,
LastName,
Address,
City,
State,
Zipcode
HAVING COUNT(1) > 1
-- Keep only the groups that appear more than once.
-- In other words, only return customers where *all six fields*
-- are duplicated across multiple rows.
HAVING COUNT(1) > 1;
17 changes: 17 additions & 0 deletions Chapter 2/Finding Duplicate Rows/Duplicate4.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"CustomerID","FirstName","LastName"
704,"Christopher","Brooks"
977,"Christopher","Brooks"
508,"Randy","Gonzales"
651,"Randy","Gonzales"
557,"Cynthia","Greene"
650,"Cynthia","Greene"
601,"Lori","Harris"
888,"Lori","Harris"
394,"Frances","Jones"
501,"Frances","Jones"
152,"Brandon","Murray"
559,"Brandon","Murray"
234,"Randy","Parker"
894,"Randy","Parker"
403,"Carolyn","Welch"
1070,"Carolyn","Welch"
9 changes: 9 additions & 0 deletions Chapter 2/Finding Duplicate Rows/Duplicate4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT c.CustomerID, c.FirstName, c.LastName
FROM Customer c
JOIN (
SELECT FirstName, LastName
FROM Customer
GROUP BY FirstName, LastName
HAVING COUNT(*) > 1
) d ON d.FirstName = c.FirstName AND d.LastName = c.LastName
ORDER BY c.LastName, c.FirstName, c.CustomerID;
25 changes: 18 additions & 7 deletions Chapter 2/Finding Inaccurate Data Values/CheckOrderTotal.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
SELECT OrderItem.OrderId,
Orders.TotalDue,
SUM(Price * Quantity) as NewTotalDue
-- Select order-level information and recalculate totals from items
SELECT
OrderItem.OrderId, -- the ID of the order
Orders.TotalDue, -- the total amount stored in the Orders
SUM(Price * Quantity) as NewTotalDue -- new total, calculated by summing each
-- product's price X quantity
-- Start from the OrderItem table (contains line items of each order)
FROM OrderItem
JOIN Product ON OrderItem.ProductID = Product.ProductID
JOIN Orders ON OrderItem.OrderID = Orders.OrderID
GROUP BY OrderItem.OrderId,
Orders.TotalDue
-- Bring in product information, to get the Price for each item
JOIN Product
ON OrderItem.ProductID = Product.ProductID
-- Bring in order-level information (e.g. stored TotalDue)
JOIN Orders
ON OrderItem.OrderID = Orders.OrderID
-- Group results so that each order is represented once
-- Required because we're using SUM() across multiple order items
GROUP BY
OrderItem.OrderId,
Orders.TotalDue;
Empty file added SOURCE
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions Selfstudy/SelectLastNameAndOrderID.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT LastName,
OrderId
FROM Customer
-- Join the Orders table to the Customer table
JOIN Orders
on Customer.CustomerID = Orders.CustomerId;
Loading