|
343 | 343 | },
|
344 | 344 | {
|
345 | 345 | "cell_type": "markdown",
|
346 |
| - "id": "0999b96c-72b1-4f3e-bf8d-73adf98d95c8", |
| 346 | + "id": "4bea2a7d-5823-4ff6-bc05-4b0c87b8e9e0", |
347 | 347 | "metadata": {},
|
348 | 348 | "source": [
|
349 | 349 | "<a id='ex4'></a>\n",
|
350 | 350 | "### Exercise 4: Joining Tables\n",
|
351 | 351 | "\n",
|
352 | 352 | "**Objective:** Combine data from multiple tables using joins.\n",
|
353 | 353 | "\n",
|
354 |
| - "- **Tasks:**\n", |
355 |
| - " - List all orders along with the `CompanyName` of the customer who placed each order.\n", |
356 |
| - " ```sql\n", |
357 |
| - " SELECT Orders.OrderID, Customers.CompanyName\n", |
358 |
| - " FROM Orders\n", |
359 |
| - " INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;\n", |
360 |
| - " ```\n", |
361 |
| - " - Retrieve a list of products along with their supplier's `CompanyName`.\n", |
362 |
| - " - Find all orders processed by the employee with `LastName` 'Fuller'." |
| 354 | + "Review these resources:\n", |
| 355 | + " - <a href=\"https://medium.com/swlh/the-best-visual-to-explain-sql-joins-612b95c81555\">The Best Visual To Explain SQL JOINs | by Junji Zhi | The Startup | Medium</a>\n", |
| 356 | + " - <a href=\"https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/\">A Visual Explanation of SQL Joins</a>\n", |
| 357 | + " - <a href=\"https://www.atlassian.com/data/sql/sql-join-types-explained-visually\">Visualizing SQL Joins | Atlassian</a>" |
| 358 | + ] |
| 359 | + }, |
| 360 | + { |
| 361 | + "cell_type": "code", |
| 362 | + "execution_count": null, |
| 363 | + "id": "6d32b473-5cfd-4927-bd03-670901c7e1bb", |
| 364 | + "metadata": {}, |
| 365 | + "outputs": [], |
| 366 | + "source": [ |
| 367 | + "%%sql -- List all orders along with the `CompanyName` of the customer who placed each order.\n", |
| 368 | + "SELECT Orders.OrderID, Customers.CompanyName\n", |
| 369 | + "FROM Orders\n", |
| 370 | + "INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;" |
363 | 371 | ]
|
364 | 372 | },
|
365 | 373 | {
|
|
404 | 412 | },
|
405 | 413 | {
|
406 | 414 | "cell_type": "markdown",
|
407 |
| - "id": "f5309bc4-629b-4b8a-a9b6-7a0bc44e3e6d", |
| 415 | + "id": "c6af1111-6e08-4625-8f51-fdcbbee2f81d", |
408 | 416 | "metadata": {},
|
409 | 417 | "source": [
|
410 | 418 | "<a id='ex5'></a>\n",
|
411 | 419 | "### Exercise 5: Aggregate Functions and GROUP BY\n",
|
412 | 420 | "\n",
|
413 |
| - "**Objective:** Use aggregate functions to summarize data.\n", |
414 |
| - "\n", |
415 |
| - "- **Tasks:**\n", |
416 |
| - " - Calculate the total number of orders.\n", |
417 |
| - " ```sql\n", |
418 |
| - " SELECT COUNT(*) AS TotalOrders FROM Orders;\n", |
419 |
| - " ```\n", |
420 |
| - " - Find the average `UnitPrice` of all products.\n", |
421 |
| - " - Determine the number of customers in each country.\n", |
422 |
| - " ```sql\n", |
423 |
| - " SELECT Country, COUNT(*) AS NumberOfCustomers\n", |
424 |
| - " FROM Customers\n", |
425 |
| - " GROUP BY Country;\n", |
426 |
| - " ```" |
| 421 | + "**Objective:** Use aggregate functions to summarize data." |
| 422 | + ] |
| 423 | + }, |
| 424 | + { |
| 425 | + "cell_type": "code", |
| 426 | + "execution_count": null, |
| 427 | + "id": "e49cac8d-cdf3-4e2d-baa8-6686f525e825", |
| 428 | + "metadata": {}, |
| 429 | + "outputs": [], |
| 430 | + "source": [ |
| 431 | + "%%sql -- Calculate the total number of orders.\n", |
| 432 | + " SELECT COUNT(*) AS TotalOrders FROM Orders;" |
| 433 | + ] |
| 434 | + }, |
| 435 | + { |
| 436 | + "cell_type": "code", |
| 437 | + "execution_count": null, |
| 438 | + "id": "fad21a05-07bb-4fa6-b3f9-cee5a0f188f5", |
| 439 | + "metadata": {}, |
| 440 | + "outputs": [], |
| 441 | + "source": [ |
| 442 | + "%%sql -- Determine the number of customers in each country.\n", |
| 443 | + "SELECT Country, COUNT(*) AS NumberOfCustomers\n", |
| 444 | + "FROM Customers\n", |
| 445 | + "GROUP BY Country;" |
427 | 446 | ]
|
428 | 447 | },
|
429 | 448 | {
|
|
472 | 491 | },
|
473 | 492 | {
|
474 | 493 | "cell_type": "markdown",
|
475 |
| - "id": "aab1594f-5e1e-482b-89c7-1824505b0bb0", |
| 494 | + "id": "92f18575-6b60-4e35-af83-819429cc872f", |
476 | 495 | "metadata": {},
|
477 | 496 | "source": [
|
478 | 497 | "<a id='ex6'></a>\n",
|
479 | 498 | "### Exercise 6: Advanced Joins and Subqueries\n",
|
480 | 499 | "\n",
|
481 |
| - "**Objective:** Solve complex queries using advanced SQL techniques.\n", |
482 |
| - "\n", |
483 |
| - "**Tasks:**\n", |
484 |
| - " - List employees and the total number of orders they have processed.\n", |
485 |
| - " ```sql\n", |
486 |
| - " SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders\n", |
487 |
| - " FROM Employees\n", |
488 |
| - " LEFT JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID\n", |
489 |
| - " GROUP BY Employees.LastName;\n", |
490 |
| - " ```\n", |
491 |
| - " - Find products that have never been ordered.\n", |
492 |
| - " - Retrieve customers who have not placed any orders in 2019." |
| 500 | + "**Objective:** Solve complex queries using advanced SQL techniques." |
| 501 | + ] |
| 502 | + }, |
| 503 | + { |
| 504 | + "cell_type": "code", |
| 505 | + "execution_count": null, |
| 506 | + "id": "d0ee7627-5c93-45bb-a41d-8b956f71264c", |
| 507 | + "metadata": {}, |
| 508 | + "outputs": [], |
| 509 | + "source": [ |
| 510 | + "%%sql -- List employees and the total number of orders they have processed.\n", |
| 511 | + "SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders\n", |
| 512 | + "FROM Employees\n", |
| 513 | + "LEFT JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID\n", |
| 514 | + "GROUP BY Employees.LastName;" |
493 | 515 | ]
|
494 | 516 | },
|
495 | 517 | {
|
|
636 | 658 | "## Additional Resources\n",
|
637 | 659 | "\n",
|
638 | 660 | "- **SQLite Documentation:** [https://www.sqlite.org/docs.html](https://www.sqlite.org/docs.html)\n",
|
639 |
| - "- **SQL Tutorial:** [W3Schools SQL Tutorial](https://www.w3schools.com/sql/)\n", |
| 661 | + "- **SQLite Command Line Reference:** [https://www.sqlite.org/cli.html](https://www.sqlite.org/cli.html)\n", |
| 662 | + "- **SQL Tutorial Cheatsheet:** [https://www.sqltutorial.org/sql-cheat-sheet/](https://www.sqltutorial.org/sql-cheat-sheet/)\n", |
640 | 663 | "- **SQLite Browser Wiki:** [SQLite Browser GitHub Wiki](https://github.com/sqlitebrowser/sqlitebrowser/wiki)\n",
|
641 |
| - "- **Northwind Database Schema:** [Schema Diagram](https://github.com/jpwhite3/northwind-SQLite3/blob/master/Northwind_ERD.png)" |
| 664 | + "- **Northwind SQLite Repo:** [Github Repo](https://github.com/jpwhite3/northwind-SQLite3/)\n", |
| 665 | + "- **Northwind Database Schema:** [Schema Diagram](https://github.com/jpwhite3/northwind-SQLite3/blob/main/docs/Northwind_ERD.png)" |
642 | 666 | ]
|
643 | 667 | },
|
644 | 668 | {
|
|
0 commit comments