diff --git a/.vscode/settings.json b/.vscode/settings.json index 66874f2..3e74749 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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, @@ -36,5 +35,8 @@ "password": "mariadb" } ], - "sqltools.useNodeRuntime": true + "sqltools.useNodeRuntime": true, + "githubPullRequests.ignoredPullRequestBranches": [ + "main" + ] } \ No newline at end of file diff --git a/Chapter 2/Finding Duplicate Rows/Duplicate1.csv b/Chapter 2/Finding Duplicate Rows/Duplicate1.csv new file mode 100644 index 0000000..703432c --- /dev/null +++ b/Chapter 2/Finding Duplicate Rows/Duplicate1.csv @@ -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" diff --git a/Chapter 2/Finding Duplicate Rows/Duplicate1.sql b/Chapter 2/Finding Duplicate Rows/Duplicate1.sql index 6d4dfd5..c1970df 100644 --- a/Chapter 2/Finding Duplicate Rows/Duplicate1.sql +++ b/Chapter 2/Finding Duplicate Rows/Duplicate1.sql @@ -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 \ No newline at end of file diff --git a/Chapter 2/Finding Duplicate Rows/Duplicate2.csv b/Chapter 2/Finding Duplicate Rows/Duplicate2.csv new file mode 100644 index 0000000..321be49 --- /dev/null +++ b/Chapter 2/Finding Duplicate Rows/Duplicate2.csv @@ -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" diff --git a/Chapter 2/Finding Duplicate Rows/Duplicate2.sql b/Chapter 2/Finding Duplicate Rows/Duplicate2.sql index f70013a..5389e8e 100644 --- a/Chapter 2/Finding Duplicate Rows/Duplicate2.sql +++ b/Chapter 2/Finding Duplicate Rows/Duplicate2.sql @@ -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 \ No newline at end of file diff --git a/Chapter 2/Finding Duplicate Rows/Duplicate3.sql b/Chapter 2/Finding Duplicate Rows/Duplicate3.sql index b1484da..2e029d0 100644 --- a/Chapter 2/Finding Duplicate Rows/Duplicate3.sql +++ b/Chapter 2/Finding Duplicate Rows/Duplicate3.sql @@ -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 \ No newline at end of file +-- 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; \ No newline at end of file diff --git a/Chapter 2/Finding Duplicate Rows/Duplicate4.csv b/Chapter 2/Finding Duplicate Rows/Duplicate4.csv new file mode 100644 index 0000000..ce0cd6e --- /dev/null +++ b/Chapter 2/Finding Duplicate Rows/Duplicate4.csv @@ -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" diff --git a/Chapter 2/Finding Duplicate Rows/Duplicate4.sql b/Chapter 2/Finding Duplicate Rows/Duplicate4.sql new file mode 100644 index 0000000..e98c75a --- /dev/null +++ b/Chapter 2/Finding Duplicate Rows/Duplicate4.sql @@ -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; diff --git a/Chapter 2/Finding Inaccurate Data Values/CheckOrderTotal.sql b/Chapter 2/Finding Inaccurate Data Values/CheckOrderTotal.sql index 64089b3..06e6e27 100644 --- a/Chapter 2/Finding Inaccurate Data Values/CheckOrderTotal.sql +++ b/Chapter 2/Finding Inaccurate Data Values/CheckOrderTotal.sql @@ -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 \ No newline at end of file +-- 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; \ No newline at end of file diff --git a/SOURCE b/SOURCE new file mode 100644 index 0000000..e69de29 diff --git a/Selfstudy/DataBaseGeneralView.sql b/Selfstudy/DataBaseGeneralView.sql new file mode 100644 index 0000000..e69de29 diff --git a/Selfstudy/SelectLastNameAndOrderID.sql b/Selfstudy/SelectLastNameAndOrderID.sql new file mode 100644 index 0000000..7187296 --- /dev/null +++ b/Selfstudy/SelectLastNameAndOrderID.sql @@ -0,0 +1,6 @@ +SELECT LastName, + OrderId +FROM Customer +-- Join the Orders table to the Customer table + JOIN Orders + on Customer.CustomerID = Orders.CustomerId; \ No newline at end of file diff --git a/Selfstudy/SelectLastNameAndOrderID.txt b/Selfstudy/SelectLastNameAndOrderID.txt new file mode 100644 index 0000000..9a7387f --- /dev/null +++ b/Selfstudy/SelectLastNameAndOrderID.txt @@ -0,0 +1,201 @@ +LastName OrderId +Mcdonald 1195 +Clark 1189 +Ramirez 1021 +Meyer 1088 +Palmer 1073 +Alexander 1142 +Howard 1027 +Howard 1171 +Gibson 1001 +Carter 1060 +Carter 1163 +Watkins 1056 +Graham 1036 +Mccoy 1092 +Jordan 1084 +Hicks 1155 +Jacobs 1101 +Jacobs 1116 +Taylor 1141 +Cooper 1065 +Jordan 1121 +Henderson 1119 +Cook 1014 +Scott 1113 +Freeman 1123 +Bell 1149 +Perez 1029 +Gilbert 1052 +Ramos 1153 +Garcia 1110 +Gonzales 1138 +Anderson 1070 +Rivera 1040 +Wallace 1114 +Fisher 1089 +Morrison 1007 +Campbell 1129 +Johnston 1178 +Dean 1177 +Welch 1018 +Welch 1185 +Kennedy 1068 +Foster 1165 +Gutierrez 1179 +Campbell 1188 +Warren 1037 +Robertson 1025 +Johnston 1161 +Brooks 1191 +Elliott 1059 +Marshall 1057 +Garcia 1164 +Price 1160 +Franklin 1173 +Wheeler 1028 +Powell 1148 +Hernandez 1111 +Crawford 1000 +Morrison 1082 +Morrison 1115 +Hanson 1186 +Holmes 1168 +Ross 1151 +Lawrence 1104 +Lopez 1100 +Porter 1085 +Cunningham 1124 +Turner 1095 +Roberts 1086 +Mitchell 1032 +Mitchell 1080 +Ramos 1176 +Henry 1181 +Martinez 1019 +Robertson 1063 +Crawford 1098 +Grant 1079 +Matthews 1136 +Johnston 1158 +Wood 1193 +Shaw 1074 +Shaw 1159 +Gordon 1091 +Hansen 1132 +Snyder 1135 +Carpenter 1017 +Willis 1122 +Willis 1184 +Campbell 1023 +Campbell 1030 +Weaver 1075 +Lopez 1076 +Hamilton 1034 +Medina 1169 +Thomas 1097 +Gardner 1006 +Reid 1128 +Lee 1127 +Scott 1152 +Barnes 1044 +Hansen 1010 +Hansen 1172 +Johnson 1125 +Porter 1061 +Walker 1015 +Stephens 1054 +Stephens 1105 +Arnold 1167 +Jones 1145 +Thompson 1199 +Lawson 1041 +Jordan 1069 +Schmidt 1126 +Davis 1066 +Riley 1038 +Brooks 1147 +Collins 1197 +Moreno 1077 +Pierce 1035 +Pierce 1062 +Anderson 1182 +Wood 1154 +Howard 1049 +Howard 1144 +Martinez 1106 +Reed 1146 +Gibson 1012 +Myers 1140 +Martin 1108 +Rice 1174 +Peterson 1143 +Watkins 1170 +Adams 1043 +Grant 1009 +Grant 1064 +Graham 1002 +Graham 1130 +Dunn 1026 +Alexander 1005 +Young 1083 +Nguyen 1078 +Young 1087 +Howard 1109 +Lopez 1011 +Hughes 1156 +Dunn 1180 +Foster 1112 +Foster 1139 +Ferguson 1102 +Cunningham 1008 +Ross 1042 +Mills 1137 +Mills 1194 +Howell 1175 +Dunn 1067 +Castillo 1071 +Castillo 1031 +Evans 1196 +Montgomery 1004 +Montgomery 1090 +Scott 1072 +Hernandez 1051 +Clark 1103 +Chavez 1157 +Castillo 1133 +Knight 1096 +Daniels 1093 +Nelson 1190 +Spencer 1033 +Jones 1055 +Rose 1134 +Rose 1150 +Reyes 1162 +Ferguson 1020 +Ferguson 1099 +Adams 1048 +Hart 1117 +Ramos 1192 +Andrews 1107 +Johnston 1131 +Taylor 1198 +Carpenter 1022 +Carpenter 1050 +Cooper 1003 +Brooks 1047 +Frazier 1024 +Martin 1046 +Cooper 1183 +Bennett 1081 +Robinson 1187 +Harper 1045 +Henry 1166 +Franklin 1094 +Arnold 1039 +Snyder 1053 +Nelson 1120 +Matthews 1058 +Alvarez 1016 +Carter 1013 +Gonzalez 1118 diff --git a/mydb.db b/mydb.db new file mode 100644 index 0000000..e69de29