├── Advanced_SQL.sql ├── Beginner_SQL.sql ├── Intermediate_SQL.sql ├── Northwind2016.bak ├── README.md └── kupdf.net_sq-l-practice-problems.pdf /Advanced_SQL.sql: -------------------------------------------------------------------------------- 1 | Select 2 | Customers.CustomerID 3 | ,Customers.CompanyName 4 | ,Orders.OrderID 5 | ,TotalOrderAmount = SUM(Quantity * UnitPrice) 6 | From Customers 7 | Join Orders 8 | on Orders.CustomerID = Customers.CustomerID 9 | Join OrderDetails 10 | on Orders.OrderID = OrderDetails.OrderID 11 | Where 12 | OrderDate >= '20160101' 13 | and OrderDate < '20170101' 14 | Group by 15 | Customers.CustomerID 16 | ,Customers.CompanyName 17 | ,Orders.Orderid 18 | Having Sum(Quantity * UnitPrice) > 10000 19 | Order by TotalOrderAmount DESC 20 | 21 | 22 | Select 23 | Customers.CustomerID 24 | ,Customers.CompanyName 25 | --,Orders.OrderID 26 | ,TotalOrderAmount = SUM(Quantity * UnitPrice) 27 | From Customers 28 | Join Orders 29 | on Orders.CustomerID = Customers.CustomerID 30 | Join OrderDetails 31 | on Orders.OrderID = OrderDetails.OrderID 32 | Where 33 | OrderDate >= '20160101' 34 | and OrderDate < '20170101' 35 | Group by 36 | Customers.CustomerID 37 | ,Customers.CompanyName 38 | --,Orders.Orderid 39 | Having sum(Quantity * UnitPrice) > 15000 40 | Order by TotalOrderAmount desc; 41 | 42 | 43 | Select 44 | Customers.CustomerID 45 | ,Customers.CompanyName 46 | ,TotalsWithoutDiscount = SUM(Quantity * UnitPrice) 47 | ,TotalsWithDiscount = SUM(Quantity * UnitPrice * (1- Discount)) 48 | From Customers 49 | Join Orders 50 | on Orders.CustomerID = Customers.CustomerID 51 | Join OrderDetails 52 | on Orders.OrderID = OrderDetails.OrderID 53 | Where 54 | OrderDate >= '20160101' 55 | and OrderDate < '20170101' 56 | Group by 57 | Customers.CustomerID 58 | ,Customers.CompanyName 59 | Having sum(Quantity * UnitPrice * (1- Discount)) > 10000 60 | Order by TotalsWithDiscount DESC; Select 61 | EmployeeID 62 | ,OrderID 63 | ,OrderDate 64 | From Orders 65 | Where OrderDate = EOMONTH(OrderDate ) 66 | Order by 67 | EmployeeID 68 | ,OrderID Select top 10 69 | Orders.OrderID 70 | ,TotalOrderDetails = count(*) 71 | From Orders 72 | Join OrderDetails 73 | on Orders.OrderID = OrderDetails.OrderID 74 | Group By Orders.OrderID 75 | Order By count(*) desc Select top 2 percent 76 | OrderID 77 | From Orders 78 | Order By NewID() Select 79 | OrderID 80 | From OrderDetails 81 | Where Quantity >= 60 82 | Group By 83 | OrderID 84 | ,Quantity 85 | Having Count(*) > 1 ;with PotentialDuplicates as ( 86 | Select 87 | OrderID 88 | From OrderDetails 89 | Where Quantity >= 60 90 | Group By OrderID, Quantity 91 | Having Count(*) > 1 92 | ) 93 | Select 94 | OrderID 95 | ,ProductID 96 | ,UnitPrice 97 | ,Quantity 98 | ,Discount 99 | From OrderDetails 100 | Where 101 | OrderID in (Select OrderID from PotentialDuplicates) 102 | Order by 103 | OrderID 104 | ,Quantity Select 105 | OrderDetails.OrderID 106 | ,ProductID 107 | ,UnitPrice 108 | ,Quantity 109 | ,Discount 110 | From OrderDetails 111 | Join ( 112 | Select distinct 113 | OrderID 114 | From OrderDetails 115 | Where Quantity >= 60 116 | Group By OrderID, Quantity 117 | Having Count(*) > 1 118 | )PotentialProblemOrders 119 | on PotentialProblemOrders.OrderID = OrderDetails.OrderID 120 | Order by OrderID, ProductID Select 121 | OrderID 122 | ,OrderDate = convert(date, OrderDate) 123 | ,RequiredDate = convert(date, RequiredDate) 124 | ,ShippedDate = convert(date, ShippedDate) 125 | From Orders 126 | Where 127 | RequiredDate <= ShippedDate Select 128 | Employees.EmployeeID 129 | ,LastName 130 | ,TotalLateOrders = Count(*) 131 | From Orders 132 | Join Employees 133 | on Employees.EmployeeID = Orders.EmployeeID 134 | Where 135 | RequiredDate <= ShippedDate 136 | Group By 137 | Employees.EmployeeID 138 | ,Employees.LastName 139 | Order by TotalLateOrders desc ;With LateOrders as ( 140 | Select 141 | EmployeeID 142 | ,TotalOrders = Count(*) 143 | From Orders 144 | Where 145 | RequiredDate <= ShippedDate 146 | Group By 147 | EmployeeID 148 | ) 149 | ,AllOrders as ( 150 | Select 151 | EmployeeID 152 | ,TotalOrders = Count(*) 153 | From Orders 154 | Group By 155 | EmployeeID 156 | ) 157 | Select 158 | Employees.EmployeeID 159 | ,LastName 160 | ,AllOrders = AllOrders.TotalOrders 161 | ,LateOrders = LateOrders.TotalOrders 162 | From Employees 163 | Join AllOrders 164 | on AllOrders.EmployeeID = Employees.EmployeeID 165 | Join LateOrders 166 | on LateOrders.EmployeeID = Employees.EmployeeID ;With LateOrders as ( 167 | Select 168 | EmployeeID 169 | ,TotalOrders = Count(*) 170 | From Orders 171 | Where 172 | RequiredDate <= ShippedDate 173 | Group By 174 | EmployeeID 175 | ) 176 | , AllOrders as ( 177 | Select 178 | EmployeeID 179 | ,TotalOrders = Count(*) 180 | From Orders 181 | Group By 182 | EmployeeID 183 | ) 184 | Select 185 | Employees.EmployeeID 186 | ,LastName 187 | ,AllOrders = AllOrders.TotalOrders 188 | ,LateOrders = LateOrders.TotalOrders 189 | From Employees 190 | Join AllOrders 191 | on AllOrders.EmployeeID = Employees.EmployeeID 192 | Left Join LateOrders 193 | on LateOrders.EmployeeID = Employees.EmployeeID 194 | 195 | 196 | ;With LateOrders as ( 197 | Select 198 | EmployeeID 199 | ,TotalOrders = Count(*) 200 | From Orders 201 | Where 202 | RequiredDate <= ShippedDate 203 | Group By 204 | EmployeeID 205 | ) 206 | ,AllOrders as ( 207 | Select 208 | EmployeeID 209 | ,TotalOrders = Count(*) 210 | From Orders 211 | Group By 212 | EmployeeID 213 | ) 214 | Select 215 | Employees.EmployeeID 216 | ,LastName 217 | ,AllOrders = AllOrders.TotalOrders 218 | ,LateOrders = IsNull(LateOrders.TotalOrders, 0) 219 | From Employees 220 | Join AllOrders 221 | on AllOrders.EmployeeID = Employees.EmployeeID 222 | Left Join LateOrders 223 | on LateOrders.EmployeeID = Employees.EmployeeID ;With LateOrders as ( 224 | Select 225 | EmployeeID 226 | ,TotalOrders = Count(*) 227 | From Orders 228 | Where 229 | RequiredDate <= ShippedDate 230 | Group By 231 | EmployeeID 232 | ) 233 | ,AllOrders as ( 234 | Select 235 | EmployeeID 236 | ,TotalOrders = Count(*) 237 | From Orders 238 | Group By 239 | EmployeeID 240 | ) 241 | Select 242 | Employees.EmployeeID 243 | ,LastName 244 | ,AllOrders = AllOrders.TotalOrders 245 | ,LateOrders = IsNull(LateOrders.TotalOrders, 0) 246 | ,PercentLateOrders = 247 | (IsNull(LateOrders.TotalOrders, 0) * 1.00) / AllOrders.TotalOrders 248 | From Employees 249 | Join AllOrders 250 | on AllOrders.EmployeeID = Employees.EmployeeID 251 | Left Join LateOrders 252 | on LateOrders.EmployeeID = Employees.EmployeeID ;With LateOrders as ( 253 | Select 254 | EmployeeID 255 | ,TotalOrders = Count(*) 256 | From Orders 257 | Where 258 | RequiredDate <= ShippedDate 259 | Group By 260 | EmployeeID 261 | ) 262 | ,AllOrders as ( 263 | Select 264 | EmployeeID 265 | ,TotalOrders = Count(*) 266 | From Orders 267 | Group By 268 | EmployeeID 269 | ) 270 | Select 271 | Employees.EmployeeID 272 | ,LastName 273 | ,AllOrders = AllOrders.TotalOrders 274 | ,LateOrders = IsNull(LateOrders.TotalOrders, 0) 275 | ,PercentLateOrders = 276 | Convert( 277 | Decimal (10,2) 278 | ,(IsNull(LateOrders.TotalOrders, 0) * 1.00) / AllOrders.TotalOrders 279 | ) 280 | From Employees 281 | Join AllOrders 282 | on AllOrders.EmployeeID = Employees.EmployeeID 283 | Left Join LateOrders 284 | on LateOrders.EmployeeID = Employees.EmployeeID ;with Orders2016 as ( 285 | Select 286 | Customers.CustomerID 287 | ,Customers.CompanyName 288 | ,TotalOrderAmount = SUM(Quantity * UnitPrice) 289 | From Customers 290 | Join Orders 291 | on Orders.CustomerID = Customers.CustomerID 292 | Join OrderDetails 293 | on Orders.OrderID = OrderDetails.OrderID 294 | Where 295 | OrderDate >= '20160101' 296 | and OrderDate < '20170101' 297 | Group by 298 | Customers.CustomerID 299 | ,Customers.CompanyName 300 | ) 301 | Select 302 | CustomerID 303 | ,CompanyName 304 | ,TotalOrderAmount 305 | ,CustomerGroup = 306 | Case 307 | when TotalOrderAmount between 0 and 1000 then 'Low' 308 | when TotalOrderAmount between 1001 and 5000 then 'Medium' 309 | when TotalOrderAmount between 5001 and 10000 then 'High' 310 | when TotalOrderAmount > 10000 then 'Very High' 311 | End 312 | from Orders2016 313 | Order by CustomerID ;with Orders2016 as ( 314 | Select 315 | Customers.CustomerID 316 | ,Customers.CompanyName 317 | ,TotalOrderAmount = SUM(Quantity * UnitPrice) 318 | From Customers 319 | Join Orders 320 | on Orders.CustomerID = Customers.CustomerID 321 | Join OrderDetails 322 | on Orders.OrderID = OrderDetails.OrderID 323 | Where 324 | OrderDate >= '20160101' 325 | and OrderDate < '20170101' 326 | Group by 327 | Customers.CustomerID 328 | ,Customers.CompanyName 329 | ) 330 | Select 331 | CustomerID 332 | ,CompanyName 333 | ,TotalOrderAmount 334 | ,CustomerGroup = 335 | case 336 | when TotalOrderAmount >= 0 and TotalOrderAmount < 1000 then 'Low' 337 | when TotalOrderAmount >= 1000 and TotalOrderAmount < 5000 then 'Medium' 338 | when TotalOrderAmount >= 5000 and TotalOrderAmount <10000 then 'High' 339 | when TotalOrderAmount >= 10000 then 'Very High' 340 | end 341 | from Orders2016 342 | Order by CustomerID ;with Orders2016 as ( 343 | Select 344 | Customers.CustomerID 345 | ,Customers.CompanyName 346 | ,TotalOrderAmount = SUM(Quantity * UnitPrice) 347 | From Customers 348 | join Orders 349 | on Orders.CustomerID = Customers.CustomerID 350 | join OrderDetails 351 | on Orders.OrderID = OrderDetails.OrderID 352 | Where 353 | OrderDate >= '20160101' 354 | and OrderDate < '20170101' 355 | Group By 356 | Customers.CustomerID 357 | ,Customers.CompanyName 358 | ) 359 | ,CustomerGrouping as ( 360 | Select 361 | CustomerID 362 | ,CompanyName 363 | ,TotalOrderAmount 364 | ,CustomerGroup = 365 | case 366 | when TotalOrderAmount >= 0 and TotalOrderAmount < 1000 then 'Low' 367 | when TotalOrderAmount >= 1000 and TotalOrderAmount < 5000 then 'Medium' 368 | when TotalOrderAmount >= 5000 and TotalOrderAmount <10000 then 'High' 369 | when TotalOrderAmount >= 10000 then 'Very High' end from Orders2016 370 | -- Order by CustomerID 371 | ) 372 | Select 373 | CustomerGroup 374 | , TotalInGroup = Count(*) 375 | , PercentageInGroup = Count(*) * 1.0/ (select count(*) from CustomerGrouping) 376 | from CustomerGrouping 377 | group by CustomerGroup 378 | order by TotalInGroup desc ;with Orders2016 as ( 379 | Select 380 | Customers.CustomerID 381 | ,Customers.CompanyName 382 | ,TotalOrderAmount = SUM(Quantity * UnitPrice) 383 | From Customers 384 | Join Orders 385 | on Orders.CustomerID = Customers.CustomerID 386 | Join OrderDetails 387 | on Orders.OrderID = OrderDetails.OrderID 388 | Where 389 | OrderDate >= '20160101' 390 | and OrderDate < '20170101' 391 | Group by 392 | Customers.CustomerID 393 | ,Customers.CompanyName 394 | ) 395 | Select 396 | CustomerID ,CompanyName 397 | ,TotalOrderAmount 398 | ,CustomerGroupName 399 | from Orders2016 400 | Join CustomerGroupThresholds 401 | on Orders2016.TotalOrderAmount between 402 | CustomerGroupThresholds.RangeBottom and CustomerGroupThresholds.RangeTop 403 | Order by CustomerID Select Country From Customers 404 | Union 405 | Select Country From Suppliers 406 | Order by Country 407 | 408 | 409 | ;With SupplierCountries as 410 | (Select Distinct Country from Suppliers) 411 | ,CustomerCountries as 412 | (Select Distinct Country from Customers) 413 | Select 414 | SupplierCountry = SupplierCountries .Country 415 | ,CustomerCountry = CustomerCountries .Country 416 | From SupplierCountries 417 | Full Outer Join CustomerCountries 418 | on CustomerCountries.Country = SupplierCountries.Country 419 | 420 | 421 | ;With SupplierCountries as 422 | (Select Country , Total = Count(*) from Suppliers group by Country) 423 | ,CustomerCountries as 424 | (Select Country , Total = Count(*) from Customers group by Country) 425 | Select 426 | Country = isnull( SupplierCountries.Country, CustomerCountries.Country) 427 | ,TotalSuppliers= isnull(SupplierCountries.Total,0) 428 | ,TotalCustomers= isnull(CustomerCountries.Total,0) 429 | From SupplierCountries 430 | Full Outer Join CustomerCountries 431 | on CustomerCountries.Country = SupplierCountries.Country ;with OrdersByCountry as 432 | ( 433 | Select 434 | ShipCountry 435 | ,CustomerID 436 | ,OrderID 437 | ,OrderDate = convert(date, OrderDate) 438 | ,RowNumberPerCountry = 439 | Row_Number() 440 | over (Partition by ShipCountry Order by ShipCountry, OrderID) 441 | From Orders 442 | ) 443 | Select 444 | ShipCountry 445 | ,CustomerID 446 | ,OrderID 447 | ,OrderDate 448 | From OrdersByCountry 449 | Where 450 | RowNumberPerCountry = 1 451 | Order by 452 | ShipCountry Select 453 | InitialOrder.CustomerID 454 | ,InitialOrderID = InitialOrder.OrderID 455 | ,InitialOrderDate = convert(date, InitialOrder.OrderDate) 456 | ,NextOrderID = NextOrder.OrderID 457 | ,NextOrderDate = convert(date, NextOrder.OrderDate) 458 | ,DaysBetween = datediff(dd, InitialOrder.OrderDate, NextOrder.OrderDate) 459 | from Orders InitialOrder 460 | join Orders NextOrder 461 | on InitialOrder.CustomerID = NextOrder.CustomerID 462 | where 463 | InitialOrder.OrderID < NextOrder.OrderID 464 | and datediff(dd, InitialOrder.OrderDate, NextOrder.OrderDate) <= 5 465 | Order by 466 | InitialOrder.CustomerID 467 | ,InitialOrder.OrderID ;With NextOrderDate as ( 468 | Select 469 | CustomerID 470 | ,OrderDate = convert(date, OrderDate) 471 | ,NextOrderDate = 472 | convert( 473 | date 474 | ,Lead(OrderDate,1) 475 | OVER (Partition by CustomerID order by CustomerID, OrderDate) 476 | ) 477 | From Orders 478 | ) 479 | Select 480 | CustomerID 481 | ,OrderDate 482 | ,NextOrderDate 483 | ,DaysBetweenOrders = DateDiff (dd, OrderDate, NextOrderDate) 484 | From NextOrderDate 485 | Where 486 | DateDiff (dd, OrderDate, NextOrderDate) <= 5 487 | 488 | -------------------------------------------------------------------------------- /Beginner_SQL.sql: -------------------------------------------------------------------------------- 1 | Select * 2 | From Shippers 3 | 4 | 5 | Select CategoryName 6 | ,Description 7 | from Categories 8 | 9 | 10 | Select 11 | FirstName 12 | ,LastName 13 | ,HireDate 14 | From Employees 15 | Where Title = 'Sales Representative' 16 | 17 | 18 | Select 19 | FirstName 20 | ,LastName 21 | ,HireDate 22 | From Employees 23 | Where Title = 'Sales Representative'and Country = 'USA' 24 | 25 | 26 | Select 27 | OrderID 28 | ,OrderDate 29 | From Orders 30 | Where EmployeeID = 5 31 | 32 | 33 | Select 34 | SupplierID 35 | ,ContactName 36 | ,ContactTitle 37 | From Suppliers 38 | Where ContactTitle <> 'Marketing Manager' 39 | 40 | 41 | Select 42 | ProductID 43 | ,ProductName 44 | From Products 45 | Where ProductName like '%queso%' 46 | 47 | 48 | Select 49 | OrderID 50 | ,CustomerID 51 | ,ShipCountry 52 | From Orders 53 | where ShipCountry = 'France' or ShipCountry = 'Belgium' Select 54 | OrderID 55 | ,CustomerID 56 | ,ShipCountry 57 | From Orders 58 | where 59 | ShipCountry in 60 | ( 61 | 'Brazil' 62 | ,'Mexico' 63 | ,'Argentina' 64 | ,'Venezuela') 65 | 66 | 67 | Select 68 | FirstName 69 | ,LastName 70 | ,Title 71 | ,BirthDate 72 | From Employees 73 | Order By Birthdate 74 | 75 | 76 | Select 77 | FirstName 78 | ,LastName 79 | ,Title 80 | ,DateOnlyBirthDate = convert(date, BirthDate) 81 | From Employees 82 | Order By Birthdate 83 | 84 | 85 | Select 86 | FirstName 87 | ,LastName 88 | ,FullName = FirstName + ' ' + LastName 89 | From Employees Select 90 | OrderID 91 | ,ProductID 92 | ,UnitPrice 93 | ,Quantity 94 | ,TotalPrice = UnitPrice * Quantity 95 | From OrderDetails 96 | Order by 97 | OrderID 98 | ,ProductID 99 | 100 | Select 101 | TotalCustomers = count(*) 102 | from Customers Select 103 | FirstOrder = min(OrderDate) 104 | From Orders Select 105 | Country 106 | From Customers 107 | Group by Country 108 | 109 | Select 110 | ContactTitle 111 | ,TotalContactTitle = count(*) 112 | From Customers 113 | Group by 114 | ContactTitle 115 | Order by 116 | count(*) desc 117 | 118 | 119 | Select 120 | ProductID 121 | ,ProductName 122 | ,Supplier = CompanyName 123 | From Products 124 | Join Suppliers 125 | on Products.SupplierID = Suppliers.SupplierID 126 | 127 | Select 128 | OrderID 129 | ,OrderDate = convert(date, OrderDate) 130 | ,Shipper = CompanyName 131 | From Orders 132 | join Shippers 133 | on Shippers.ShipperID = Orders.ShipVia 134 | Where OrderID < 10300 135 | Order by OrderID 136 | -------------------------------------------------------------------------------- /Intermediate_SQL.sql: -------------------------------------------------------------------------------- 1 | Select 2 | CategoryName 3 | ,TotalProducts = count(*) 4 | From Products 5 | Join Categories 6 | on Products.CategoryID = Categories.CategoryID 7 | Group by 8 | CategoryName 9 | Order by 10 | count(*) desc 11 | 12 | 13 | Select 14 | Country 15 | ,City 16 | ,TotalCustomer = Count(*) 17 | From Customers 18 | Group by 19 | Country 20 | ,City 21 | Order by 22 | count(*) desc 23 | 24 | 25 | Select 26 | ProductID 27 | ,ProductName 28 | ,UnitsInStock 29 | ,ReorderLevel 30 | From Products 31 | Where 32 | UnitsInStock <= ReorderLevel 33 | Order by ProductID 34 | 35 | 36 | Select 37 | ProductID 38 | ,ProductName 39 | ,UnitsInStock 40 | ,UnitsOnOrder 41 | ,ReorderLevel 42 | ,Discontinued 43 | From Products 44 | Where 45 | UnitsInStock + UnitsOnOrder <= ReorderLevel 46 | and Discontinued = 0 47 | Order by ProductID 48 | 49 | 50 | Select 51 | CustomerID 52 | ,CompanyName 53 | ,Region 54 | From Customers 55 | Order By 56 | Case 57 | when Region is null then 1 58 | else 0 59 | End 60 | ,Region 61 | ,CustomerID 62 | 63 | 64 | Select Top 3 65 | ShipCountry 66 | ,AverageFreight = Avg(freight) 67 | From Orders 68 | Group By ShipCountry 69 | Order By AverageFreight desc; 70 | Select Top 3 71 | ShipCountry 72 | ,AverageFreight = avg(freight) 73 | From Orders 74 | Where 75 | OrderDate >= '20150101' 76 | and OrderDate < '20160101' 77 | Group By ShipCountry 78 | Order By AverageFreight desc; Select TOP (3) 79 | ShipCountry 80 | ,AverageFreight = Avg(freight) 81 | From Orders 82 | Where 83 | OrderDate >= Dateadd(yy, -1, (Select max(OrderDate) from Orders)) 84 | Group by ShipCountry 85 | Order by AverageFreight desc; 86 | 87 | 88 | Select 89 | Employees.EmployeeID 90 | ,Employees.LastName 91 | ,Orders.OrderID 92 | ,Products.ProductName 93 | ,OrderDetails.Quantity 94 | From Employees 95 | join Orders 96 | on Orders.EmployeeID = Employees.EmployeeID 97 | join OrderDetails 98 | on Orders.OrderID = OrderDetails.OrderID 99 | join Products 100 | on Products.ProductID = OrderDetails.ProductID 101 | Order by 102 | Orders.OrderID 103 | ,Products.ProductID Select 104 | Customers_CustomerID = Customers.CustomerID 105 | ,Orders_CustomerID = Orders.CustomerID 106 | From Customers 107 | left join Orders 108 | on Orders.CustomerID = Customers.CustomerID 109 | Where 110 | Orders.CustomerID is null 111 | 112 | 113 | Select 114 | Customers.CustomerID 115 | ,Orders.CustomerID 116 | From Customers 117 | left join Orders 118 | on Orders.CustomerID = Customers.CustomerID 119 | and Orders.EmployeeID = 4 120 | Where 121 | Orders.CustomerID is null 122 | 123 | 124 | -------------------------------------------------------------------------------- /Northwind2016.bak: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonmullins/SQL_Queries/616fdb675da69d02d82b948af5539fe9b7b5438a/Northwind2016.bak -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 57_SQL_Queries -------------------------------------------------------------------------------- /kupdf.net_sq-l-practice-problems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonmullins/SQL_Queries/616fdb675da69d02d82b948af5539fe9b7b5438a/kupdf.net_sq-l-practice-problems.pdf --------------------------------------------------------------------------------