├── 1_learn_sql_codecademy ├── .ipynb_checkpoints │ └── codecademy_sql_course-checkpoint.ipynb └── codecademy_sql_course.ipynb ├── 2_sql_ex ├── .ipynb_checkpoints │ └── sql_ex-checkpoint.ipynb └── sql_ex.ipynb ├── 3_sql_course_stepik ├── .ipynb_checkpoints │ └── sql_course_stepik-checkpoint.ipynb └── sql_course_stepik.ipynb ├── 4_sql_data_analysis_course └── sql_tasks.ipynb ├── 5_yandex_praktikum └── sql_module.ipynb ├── 6_sql_bolt └── sql_tasks_and_solutions.ipynb ├── 7_oracle_db_foundations └── 210621_oracle_sql_foundations_certificate.pdf └── README.md /1_learn_sql_codecademy/codecademy_sql_course.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "SQL (Structured Query Language) is a programming language used to communicate with data stored in a relational database management system. SQL syntax is similar to the English language, which makes it relatively easy to write, read, and interpret. \n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "SQL COMMANDS Overview:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "# ALTER TABLE lets you add columns to a table in a database.\n", 24 | "ALTER TABLE \n", 25 | " table_name \n", 26 | "ADD \n", 27 | " column_name datatype;" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "# AND is an operator that combines two conditions. \n", 37 | "# Both conditions must be true for the row to be included in the result set.\n", 38 | "SELECT column_name(s)\n", 39 | "FROM table_name\n", 40 | "WHERE column_1 = value_1\n", 41 | " AND column_2 = value_2;" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "# AS is a keyword in SQL that allows you to rename a column or table using an alias.\n", 51 | "SELECT \n", 52 | " column_name(s) \n", 53 | "FROM \n", 54 | " table_name \n", 55 | "WHERE \n", 56 | " column_1 = value_1 \n", 57 | " AND column_2 = value_2;" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "# AVG() is an aggregate function that returns the average value for a numeric column.\n", 67 | "SELECT \n", 68 | " AVG(column_name) \n", 69 | "FROM \n", 70 | " table_name;" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "# The BETWEEN operator is used to filter the result set within a certain range. \n", 80 | "# The values can be numbers, text or dates.\n", 81 | "SELECT \n", 82 | " column_name(s) \n", 83 | "FROM \n", 84 | " table_name \n", 85 | "WHERE \n", 86 | " column_name BETWEEN value_1 \n", 87 | " AND value_2;" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "# CASE statements are used to create different outputs (usually in the SELECT statement). \n", 97 | "# It is SQL’s way of handling if-then logic.\n", 98 | "SELECT column_name,\n", 99 | " CASE\n", 100 | " WHEN condition THEN 'Result_1'\n", 101 | " WHEN condition THEN 'Result_2'\n", 102 | " ELSE 'Result_3'\n", 103 | " END\n", 104 | "FROM table_name;" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "# COUNT() is a function that takes the name of a column as an argument and \n", 114 | "# counts the number of rows where the column is not NULL.\n", 115 | "SELECT \n", 116 | " COUNT(column_name) \n", 117 | "FROM \n", 118 | " table_name;" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "# CREATE TABLE creates a new table in the database. \n", 128 | "# It allows you to specify the name of the table and the name of each column in the table.\n", 129 | "CREATE TABLE table_name (\n", 130 | " column_1 datatype, \n", 131 | " column_2 datatype, \n", 132 | " column_3 datatype\n", 133 | ");" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "# DELETE statements are used to remove rows from a table.\n", 143 | "DELETE FROM \n", 144 | " table_name \n", 145 | "WHERE \n", 146 | " some_column = some_value;" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "# GROUP BY is a clause in SQL that is only used with aggregate functions. \n", 156 | "# It is used in collaboration with the SELECT statement to arrange identical data into groups.\n", 157 | "SELECT \n", 158 | " column_name, \n", 159 | " COUNT(*) \n", 160 | "FROM \n", 161 | " table_name \n", 162 | "GROUP BY \n", 163 | " column_name;" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [ 172 | "# HAVING was added to SQL because the WHERE keyword \n", 173 | "# could not be used with aggregate functions.\n", 174 | "SELECT \n", 175 | " column_name, \n", 176 | " COUNT(*) \n", 177 | "FROM \n", 178 | " table_name \n", 179 | "GROUP BY \n", 180 | " column_name \n", 181 | "HAVING \n", 182 | " COUNT(*) > value;" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "# An inner JOIN will combine rows from different tables if the join condition is true.\n", 192 | "SELECT \n", 193 | " column_name(s) \n", 194 | "FROM \n", 195 | " table_1 \n", 196 | " JOIN table_2 ON table_1.column_name = table_2.column_name;" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": {}, 203 | "outputs": [], 204 | "source": [ 205 | "# INSERT statements are used to add a new row to a table.\n", 206 | "INSERT INTO table_name (column_1, column_2, column_3) \n", 207 | "VALUES \n", 208 | " (value_1, 'value_2', value_3);" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "# IS NULL and IS NOT NULL are operators used with the WHERE clause to test for empty values.\n", 218 | "SELECT \n", 219 | " column_name(s) \n", 220 | "FROM \n", 221 | " table_name \n", 222 | "WHERE \n", 223 | " column_name IS NULL;" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "# LIKE is a special operator used with the WHERE clause to search for \n", 233 | "# a specific pattern in a column.\n", 234 | "SELECT \n", 235 | " column_name(s) \n", 236 | "FROM \n", 237 | " table_name \n", 238 | "WHERE \n", 239 | " column_name LIKE pattern;" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": null, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [ 248 | "# LIMIT is a clause that lets you specify\n", 249 | "# the maximum number of rows the result set will have.\n", 250 | "SELECT \n", 251 | " column_name(s) \n", 252 | "FROM \n", 253 | " table_name \n", 254 | "LIMIT \n", 255 | " number;" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": null, 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [ 264 | "# MAX() is a function that takes the name of a column as an argument \n", 265 | "# and returns the largest value in that column.\n", 266 | "SELECT \n", 267 | " MAX(column_name) \n", 268 | "FROM \n", 269 | " table_name;" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 5, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [ 278 | "# MIN() is a function that takes the name of a column as an argument \n", 279 | "# and returns the smallest value in that column.\n", 280 | "SELECT \n", 281 | " MIN(column_name) \n", 282 | "FROM \n", 283 | " table_name;" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": null, 289 | "metadata": {}, 290 | "outputs": [], 291 | "source": [ 292 | "# OR is an operator that filters the result set to only include rows where \n", 293 | "# either condition is true.\n", 294 | "SELECT \n", 295 | " column_name \n", 296 | "FROM \n", 297 | " table_name \n", 298 | "WHERE \n", 299 | " column_name = value_1 \n", 300 | " OR column_name = value_2;" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": null, 306 | "metadata": {}, 307 | "outputs": [], 308 | "source": [ 309 | "# ORDER BY is a clause that indicates you want to sort the result set \n", 310 | "# by a particular column either alphabetically or numerically.\n", 311 | "SELECT \n", 312 | " column_name \n", 313 | "FROM \n", 314 | " table_name \n", 315 | "ORDER BY \n", 316 | " column_name ASC | DESC;" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": null, 322 | "metadata": {}, 323 | "outputs": [], 324 | "source": [ 325 | "# An outer JOIN will combine rows from different tables even if \n", 326 | "# the join condition is not met. \n", 327 | "# Every row in the left table is returned in the result set, \n", 328 | "# and if the join condition is not met, \n", 329 | "# then NULL values are used to fill in the columns from the right table.\n", 330 | "SELECT \n", 331 | " column_name(s) \n", 332 | "FROM \n", 333 | " table_1 \n", 334 | " LEFT JOIN table_2 ON table_1.column_name = table_2.column_name;" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": null, 340 | "metadata": {}, 341 | "outputs": [], 342 | "source": [ 343 | "# ROUND() is a function that takes a column name and an integer as arguments.\n", 344 | "# It rounds the values in the column to the number of decimal places specified by \n", 345 | "# the integer.\n", 346 | "SELECT \n", 347 | " ROUND(column_name, integer) \n", 348 | "FROM \n", 349 | " table_name;" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "metadata": {}, 356 | "outputs": [], 357 | "source": [ 358 | "# SELECT statements are used to fetch data from a database. \n", 359 | "# Every query will begin with SELECT.\n", 360 | "SELECT \n", 361 | " column_name \n", 362 | "FROM \n", 363 | " table_name;" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": null, 369 | "metadata": {}, 370 | "outputs": [], 371 | "source": [ 372 | "# SELECT DISTINCT specifies that the statement is going to be a query \n", 373 | "# that returns unique values in the specified column(s).\n", 374 | "SELECT \n", 375 | " DISTINCT column_name \n", 376 | "FROM \n", 377 | " table_name;" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": null, 383 | "metadata": {}, 384 | "outputs": [], 385 | "source": [ 386 | "# SUM() is a function that takes the name of a column as an argument \n", 387 | "# and returns the sum of all the values in that column.\n", 388 | "SELECT \n", 389 | " SUM(column_name) \n", 390 | "FROM \n", 391 | " table_name;" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": null, 397 | "metadata": {}, 398 | "outputs": [], 399 | "source": [ 400 | "# UPDATE statements allow you to edit rows in a table.\n", 401 | "UPDATE \n", 402 | " table_name \n", 403 | "SET \n", 404 | " some_column = some_value \n", 405 | "WHERE \n", 406 | " some_column = some_value;" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": null, 412 | "metadata": {}, 413 | "outputs": [], 414 | "source": [ 415 | "# WHERE is a clause that indicates you want to filter the result set to include \n", 416 | "# only rows where the following condition is true.\n", 417 | "SELECT \n", 418 | " column_name(s) \n", 419 | "FROM \n", 420 | " table_name \n", 421 | "WHERE \n", 422 | " column_name operator value;" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": null, 428 | "metadata": {}, 429 | "outputs": [], 430 | "source": [ 431 | "# WITH clause lets you store the result of a query in a temporary table using an alias. \n", 432 | "# You can also define multiple temporary tables using a comma \n", 433 | "# and with one instance of the WITH keyword.\n", 434 | "# The WITH clause is also known as common table expression (CTE) and subquery factoring.\n", 435 | "WITH temporary_name AS (\n", 436 | " SELECT \n", 437 | " * \n", 438 | " FROM \n", 439 | " table_name\n", 440 | ") \n", 441 | "SELECT \n", 442 | " * \n", 443 | "FROM \n", 444 | " temporary_name \n", 445 | "WHERE \n", 446 | " column_name operator value;" 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": null, 452 | "metadata": {}, 453 | "outputs": [], 454 | "source": [] 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "metadata": {}, 459 | "source": [ 460 | "LEARN SQL COURSE" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": null, 466 | "metadata": {}, 467 | "outputs": [], 468 | "source": [ 469 | "# LESSON 1 'MANIPULATION'" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": null, 475 | "metadata": {}, 476 | "outputs": [], 477 | "source": [ 478 | "# SELECT statements are used to fetch data from a database.\n", 479 | "SELECT \n", 480 | " * \n", 481 | "FROM \n", 482 | " celebs;" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": null, 488 | "metadata": {}, 489 | "outputs": [], 490 | "source": [ 491 | "# CREATE statements allow us to create a new table in the database.\n", 492 | "CREATE TABLE table_name (\n", 493 | " column_1 data_type, \n", 494 | " column_2 data_type, \n", 495 | " column_3 data_type\n", 496 | ");" 497 | ] 498 | }, 499 | { 500 | "cell_type": "code", 501 | "execution_count": null, 502 | "metadata": {}, 503 | "outputs": [], 504 | "source": [ 505 | "# CREATE example\n", 506 | "CREATE TABLE celebs (\n", 507 | " id INTEGER,\n", 508 | " name TEXT,\n", 509 | " age INTEGER\n", 510 | ");" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": null, 516 | "metadata": {}, 517 | "outputs": [], 518 | "source": [ 519 | "# The INSERT statement inserts a new row into a table\n", 520 | "INSERT INTO celebs (id, name, age) \n", 521 | "VALUES \n", 522 | " (1, 'Justin Bieber', 22), \n", 523 | " (2, 'Beyonce Knowles', 33), \n", 524 | " (3, 'Jeremy Lin', 26), \n", 525 | " (4, 'Taylor Swift', 26);" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": null, 531 | "metadata": {}, 532 | "outputs": [], 533 | "source": [ 534 | "# The ALTER TABLE statement adds a new column to a table.\n", 535 | "ALTER TABLE \n", 536 | " celebs \n", 537 | "ADD \n", 538 | " COLUMN twitter_handle TEXT;" 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": null, 544 | "metadata": {}, 545 | "outputs": [], 546 | "source": [ 547 | "# The UPDATE statement edits a row in a table. \n", 548 | "UPDATE \n", 549 | " celebs \n", 550 | "SET \n", 551 | " twitter_handle = '@taylorswift13' \n", 552 | "WHERE \n", 553 | " id = 4;" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": null, 559 | "metadata": {}, 560 | "outputs": [], 561 | "source": [ 562 | "# The DELETE FROM statement deletes one or more rows from a table.\n", 563 | "\n", 564 | "# The statement below deletes all records in the celeb table with no twitter_handle\n", 565 | "DELETE FROM \n", 566 | " celebs \n", 567 | "WHERE \n", 568 | " twitter_handle IS NULL;\n", 569 | "\n", 570 | "# To delete only a specific number of rows, you can utilize the LIMIT statement. \n", 571 | "# The value provided for LIMIT will be how many rows to affect.\n", 572 | "DELETE FROM \n", 573 | " table \n", 574 | "WHERE \n", 575 | " condition \n", 576 | "LIMIT \n", 577 | " 5;" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": null, 583 | "metadata": {}, 584 | "outputs": [], 585 | "source": [ 586 | "# CONSTRAINTS that add information about how a column can be used \n", 587 | "# are invoked after specifying the data type for a column.\n", 588 | "# They can be used to tell the database to reject inserted data that does not \n", 589 | "# adhere to a certain restriction. \n", 590 | "# The statement below sets constraints on the celebs table.\n", 591 | "CREATE TABLE celebs (\n", 592 | " id INTEGER PRIMARY KEY, \n", 593 | " name TEXT UNIQUE,\n", 594 | " date_of_birth TEXT NOT NULL,\n", 595 | " date_of_death TEXT DEFAULT 'Not Applicable'\n", 596 | ");" 597 | ] 598 | }, 599 | { 600 | "cell_type": "code", 601 | "execution_count": null, 602 | "metadata": {}, 603 | "outputs": [], 604 | "source": [ 605 | "# CONSTRAINTS examples:\n", 606 | "# -- PRIMARY KEY columns can be used to uniquely identify the row. \n", 607 | "# Attempts to insert a row with an identical value to a row already \n", 608 | "# in the table will result in a constraint violation which will not allow you \n", 609 | "# to insert the new row.\n", 610 | "# By using a constraint like the PRIMARY KEY, we can ensure that \n", 611 | "# every row has their own unique id value.\n", 612 | "# -- UNIQUE columns have a different value for every row. \n", 613 | "# This is similar to PRIMARY KEY except a table can have many different \n", 614 | "# UNIQUE columns.\n", 615 | "# -- NOT NULL columns must have a value. \n", 616 | "# Attempts to insert a row without a value for a NOT NULL column will result in \n", 617 | "# a constraint violation and the new row will not be inserted.\n", 618 | "# -- DEFAULT columns take an additional argument that will be the assumed value \n", 619 | "# for an inserted row if the new row does not specify a value for that column.\n", 620 | "\n", 621 | "# Example\n", 622 | "CREATE TABLE awards (\n", 623 | " id INTEGER PRIMARY KEY,\n", 624 | " recipient TEXT NOT NULL,\n", 625 | " award_name TEXT DEFAULT 'Grammy'\n", 626 | ");" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": null, 632 | "metadata": {}, 633 | "outputs": [], 634 | "source": [ 635 | "# LESSON 1 REVIEW:\n", 636 | "# -- CREATE TABLE creates a new table.\n", 637 | "# -- INSERT INTO adds a new row to a table.\n", 638 | "# -- SELECT queries data from a table.\n", 639 | "# -- ALTER TABLE changes an existing table.\n", 640 | "# -- UPDATE edits a row in a table.\n", 641 | "# -- DELETE FROM deletes rows from a table." 642 | ] 643 | }, 644 | { 645 | "cell_type": "code", 646 | "execution_count": null, 647 | "metadata": {}, 648 | "outputs": [], 649 | "source": [] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": null, 654 | "metadata": {}, 655 | "outputs": [], 656 | "source": [ 657 | "# LESSON 2 'QUERIES'" 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": null, 663 | "metadata": {}, 664 | "outputs": [], 665 | "source": [ 666 | "# SELECT all\n", 667 | "SELECT \n", 668 | " * \n", 669 | "FROM \n", 670 | " movies;" 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": null, 676 | "metadata": {}, 677 | "outputs": [], 678 | "source": [ 679 | "# SELECT specified columns (name, genre, year)\n", 680 | "SELECT \n", 681 | " name, \n", 682 | " genre, \n", 683 | " year \n", 684 | "FROM \n", 685 | " movies;" 686 | ] 687 | }, 688 | { 689 | "cell_type": "code", 690 | "execution_count": null, 691 | "metadata": {}, 692 | "outputs": [], 693 | "source": [ 694 | "# AS is a keyword in SQL that allows you to rename a column or table using an alias.\n", 695 | "# When using AS, the columns are not being renamed in the table. The aliases only appear in the result.\n", 696 | "SELECT \n", 697 | " imdb_rating as 'IMDb' \n", 698 | "FROM \n", 699 | " movies;" 700 | ] 701 | }, 702 | { 703 | "cell_type": "code", 704 | "execution_count": null, 705 | "metadata": {}, 706 | "outputs": [], 707 | "source": [ 708 | "# DISTINCT is used to return unique values in the output. It filters out all duplicate values in the specified column(s).\n", 709 | "SELECT \n", 710 | " DISTINCT genre \n", 711 | "FROM \n", 712 | " movies;" 713 | ] 714 | }, 715 | { 716 | "cell_type": "code", 717 | "execution_count": null, 718 | "metadata": {}, 719 | "outputs": [], 720 | "source": [ 721 | "# We can restrict our query results using the WHERE clause in order to obtain only the information we want.\n", 722 | "SELECT \n", 723 | " * \n", 724 | "FROM \n", 725 | " movies \n", 726 | "WHERE \n", 727 | " imdb_rating < 5;" 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": null, 733 | "metadata": {}, 734 | "outputs": [], 735 | "source": [ 736 | "# LIKE can be a useful operator when you want to compare similar values. \n", 737 | "# LIKE is a special operator used with the WHERE clause to search for \n", 738 | "# a specific pattern in a column.\n", 739 | "SELECT \n", 740 | " * \n", 741 | "FROM \n", 742 | " movies \n", 743 | "WHERE \n", 744 | " name LIKE 'Se_en'; # The names Seven and Se7en both match this pattern.\n", 745 | "\n", 746 | "# The _ means you can substitute any individual character here without breaking \n", 747 | "# the pattern. \n", 748 | "\n", 749 | "# % is a wildcard character that matches zero or more missing letters in the pattern.\n", 750 | "# For example:\n", 751 | "# -- A% matches all movies with names that begin with letter ‘A’\n", 752 | "# -- %a matches all movies that end with ‘a’\n", 753 | "# We can also use % both before and after a pattern:\n", 754 | "SELECT \n", 755 | " * \n", 756 | "FROM \n", 757 | " movies \n", 758 | "WHERE \n", 759 | " name LIKE '%man%';\n", 760 | "\n", 761 | "# LIKE is not case sensitive." 762 | ] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "execution_count": null, 767 | "metadata": {}, 768 | "outputs": [], 769 | "source": [ 770 | "# Unknown values are indicated by NULL.\n", 771 | "\n", 772 | "# It is not possible to test for NULL values with comparison operators, \n", 773 | "# such as = and !=. \n", 774 | "# Instead, we will have to use these operators:\n", 775 | "# -- IS NULL\n", 776 | "# -- IS NOT NULL\n", 777 | "SELECT \n", 778 | " name \n", 779 | "FROM \n", 780 | " movies \n", 781 | "WHERE \n", 782 | " imdb_rating IS NULL;" 783 | ] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "execution_count": null, 788 | "metadata": {}, 789 | "outputs": [], 790 | "source": [ 791 | "# The BETWEEN operator is used in a WHERE clause to filter the result \n", 792 | "# set within a certain range. \n", 793 | "# It accepts two values that are either numbers, text or dates.\n", 794 | "\n", 795 | "# Using the BETWEEN operator, write a query that selects all information \n", 796 | "# about movies whose name begins with the letters ‘D’, ‘E’, and ‘F’.\n", 797 | "SELECT \n", 798 | " * \n", 799 | "FROM \n", 800 | " movies \n", 801 | "WHERE \n", 802 | " name BETWEEN 'D' \n", 803 | " AND 'G';\n", 804 | "\n", 805 | "# Using the BETWEEN operator, write a new query that selects all information \n", 806 | "# about movies that were released in the 1970’s.\n", 807 | "SELECT \n", 808 | " * \n", 809 | "FROM \n", 810 | " movies \n", 811 | "WHERE \n", 812 | " year BETWEEN 1970 \n", 813 | " AND 1979;\n" 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "execution_count": null, 819 | "metadata": {}, 820 | "outputs": [], 821 | "source": [ 822 | "# With AND, both conditions must be true for the row to be included in the result.\n", 823 | "\n", 824 | "# let’s retrieve every movie released in the 70’s, that’s also well received. \n", 825 | "SELECT \n", 826 | " * \n", 827 | "FROM \n", 828 | " movies \n", 829 | "WHERE \n", 830 | " year BETWEEN 1990 \n", 831 | " AND 1999 \n", 832 | " AND genre = 'romance';\n", 833 | " \n", 834 | "# we have a picky friend who only wants to watch old horror films. \n", 835 | "SELECT \n", 836 | " * \n", 837 | "FROM \n", 838 | " movies \n", 839 | "WHERE \n", 840 | " year < 1985 \n", 841 | " AND genre = 'horror';" 842 | ] 843 | }, 844 | { 845 | "cell_type": "code", 846 | "execution_count": null, 847 | "metadata": {}, 848 | "outputs": [], 849 | "source": [ 850 | "# OR operator displays a row if any condition is true.\n", 851 | "\n", 852 | "# write a query that returns all movies that were released after 2014 or are action movies.\n", 853 | "SELECT \n", 854 | " * \n", 855 | "FROM \n", 856 | " movies \n", 857 | "WHERE \n", 858 | " year > 2014 \n", 859 | " OR genre = 'action'\n", 860 | "\n", 861 | "# write a query that returns all movies that are either a romance or a comedy.\n", 862 | "SELECT \n", 863 | " * \n", 864 | "FROM \n", 865 | " movies \n", 866 | "WHERE \n", 867 | " genre = 'romance' \n", 868 | " OR genre = 'comedy'" 869 | ] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": null, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "# Queries can combine multiple conditions using AND and OR without a real limit \n", 878 | "# to how many conditions you can combine\n", 879 | "\n", 880 | "# This will select movies with id values \n", 881 | "# from 10 to 20 inclusive, OR with id \n", 882 | "# values from 50 to 60 inclusive.\n", 883 | "SELECT \n", 884 | " * \n", 885 | "FROM \n", 886 | " movies \n", 887 | "WHERE \n", 888 | " (\n", 889 | " id > 10 \n", 890 | " AND id < 20\n", 891 | " ) \n", 892 | " OR (\n", 893 | " id > 50 \n", 894 | " AND id < 60\n", 895 | " );" 896 | ] 897 | }, 898 | { 899 | "cell_type": "code", 900 | "execution_count": null, 901 | "metadata": {}, 902 | "outputs": [], 903 | "source": [ 904 | "# We can sort the results using ORDER BY, either alphabetically or numerically. \n", 905 | "\n", 906 | "# ORDER BY always goes after WHERE (if WHERE is present).\n", 907 | "SELECT \n", 908 | " * \n", 909 | "FROM \n", 910 | " movies \n", 911 | "WHERE \n", 912 | " imdb_rating > 8 \n", 913 | "ORDER BY \n", 914 | " year DESC;\n", 915 | "\n", 916 | "# Suppose we want to retrieve the name and year columns of all the movies, \n", 917 | "# ordered by their name alphabetically.\n", 918 | "SELECT \n", 919 | " name, \n", 920 | " year \n", 921 | "FROM \n", 922 | " movies \n", 923 | "GROUP BY \n", 924 | " name\n", 925 | "\n", 926 | "# Write a new query that retrieves the name, year, and imdb_rating columns \n", 927 | "# of all the movies, ordered highest to lowest by their ratings.\n", 928 | "SELECT \n", 929 | " name, \n", 930 | " year, \n", 931 | " imdb_rating \n", 932 | "FROM \n", 933 | " movies \n", 934 | "ORDER BY \n", 935 | " imdb_rating DESC;\n", 936 | "\n", 937 | "# ORDER BY with multiple columns\n", 938 | "# Following the ORDER BY, you can list more than one column for which to order the data by.\n", 939 | "# When ordering by more than one column, it will first order the data on the first column,\n", 940 | "# then, keeping the previous column order, it will order on the next column, and so on." 941 | ] 942 | }, 943 | { 944 | "cell_type": "code", 945 | "execution_count": null, 946 | "metadata": {}, 947 | "outputs": [], 948 | "source": [ 949 | "# LIMIT lets you specify the maximum number of rows the result set will have.\n", 950 | "# LIMIT always goes at the very end of the query.\n", 951 | "# it is not supported in all SQL databases.\n", 952 | "\n", 953 | "# write a query that returns the top 3 highest rated movies.\n", 954 | "SELECT \n", 955 | " * \n", 956 | "FROM \n", 957 | " movies \n", 958 | "ORDER BY \n", 959 | " imdb_rating DESC \n", 960 | "LIMIT \n", 961 | " 3;" 962 | ] 963 | }, 964 | { 965 | "cell_type": "code", 966 | "execution_count": null, 967 | "metadata": {}, 968 | "outputs": [], 969 | "source": [ 970 | "# A CASE statement allows us to create different outputs \n", 971 | "# It is SQL’s way of handling if-then logic.\n", 972 | "\n", 973 | "SELECT name,\n", 974 | " CASE\n", 975 | " WHEN imdb_rating > 8 THEN 'Fantastic'\n", 976 | " WHEN imdb_rating > 6 THEN 'Poorly Received'\n", 977 | " ELSE 'Avoid at All Costs'\n", 978 | " END AS 'Review' # AS to shorten the name of the new column\n", 979 | "FROM movies;" 980 | ] 981 | }, 982 | { 983 | "cell_type": "code", 984 | "execution_count": null, 985 | "metadata": {}, 986 | "outputs": [], 987 | "source": [ 988 | "# Same queries:\n", 989 | "# Option 1:\n", 990 | "SELECT name,\n", 991 | " CASE\n", 992 | " WHEN genre = 'romance' THEN 'Chill'\n", 993 | " WHEN genre = 'comedy' THEN 'Chill'\n", 994 | " ELSE 'Intense'\n", 995 | " END AS 'Mood'\n", 996 | "FROM movies;\n", 997 | "\n", 998 | "# Option 2:\n", 999 | "SELECT name,\n", 1000 | " CASE\n", 1001 | " WHEN genre = 'romance' OR genre = 'comedy' \n", 1002 | " THEN 'Chill'\n", 1003 | " ELSE 'Intense'\n", 1004 | " END AS 'Mood'\n", 1005 | "FROM movies;" 1006 | ] 1007 | }, 1008 | { 1009 | "cell_type": "code", 1010 | "execution_count": null, 1011 | "metadata": {}, 1012 | "outputs": [], 1013 | "source": [ 1014 | "# LESSON 2 REVIEW:\n", 1015 | "# -- SELECT is the clause we use every time we want to query information from a database.\n", 1016 | "# -- AS renames a column or table.\n", 1017 | "# -- DISTINCT return unique values.\n", 1018 | "# -- WHERE is a popular command that lets you filter the results of the query based on conditions that you specify.\n", 1019 | "# -- LIKE and BETWEEN are special operators.\n", 1020 | "# -- AND and OR combines multiple conditions.\n", 1021 | "# -- ORDER BY sorts the result.\n", 1022 | "# -- LIMIT specifies the maximum number of rows that the query will return.\n", 1023 | "# -- CASE creates different outputs." 1024 | ] 1025 | }, 1026 | { 1027 | "cell_type": "code", 1028 | "execution_count": null, 1029 | "metadata": {}, 1030 | "outputs": [], 1031 | "source": [] 1032 | }, 1033 | { 1034 | "cell_type": "code", 1035 | "execution_count": null, 1036 | "metadata": {}, 1037 | "outputs": [], 1038 | "source": [ 1039 | "# LESSON 3 'AGGREGATE FUNCTIONS'" 1040 | ] 1041 | }, 1042 | { 1043 | "cell_type": "code", 1044 | "execution_count": null, 1045 | "metadata": {}, 1046 | "outputs": [], 1047 | "source": [ 1048 | "# Quick preview of some important aggregates:\n", 1049 | "# -- COUNT(): count the number of rows\n", 1050 | "# -- SUM(): the sum of the values in a column\n", 1051 | "# -- MAX()/MIN(): the largest/smallest value\n", 1052 | "# -- AVG(): the average of the values in a column\n", 1053 | "# -- ROUND(): round the values in the column" 1054 | ] 1055 | }, 1056 | { 1057 | "cell_type": "code", 1058 | "execution_count": null, 1059 | "metadata": {}, 1060 | "outputs": [], 1061 | "source": [ 1062 | "# COUNT() is a function that takes the name of a column as an argument and counts \n", 1063 | "# the number of non-empty values in that column.\n", 1064 | "\n", 1065 | "# count how many free apps are in the table.\n", 1066 | "SELECT \n", 1067 | " COUNT(*) \n", 1068 | "FROM \n", 1069 | " fake_apps \n", 1070 | "WHERE \n", 1071 | " price == 0;" 1072 | ] 1073 | }, 1074 | { 1075 | "cell_type": "code", 1076 | "execution_count": null, 1077 | "metadata": {}, 1078 | "outputs": [], 1079 | "source": [ 1080 | "# SUM() is a function that takes the name of a column as an argument \n", 1081 | "# and returns the sum of all the values in that column.\n", 1082 | "\n", 1083 | "# what is the total number of downloads for all of the apps combined?\n", 1084 | "SELECT \n", 1085 | " SUM(downloads) \n", 1086 | "FROM \n", 1087 | " fake_apps;" 1088 | ] 1089 | }, 1090 | { 1091 | "cell_type": "code", 1092 | "execution_count": null, 1093 | "metadata": {}, 1094 | "outputs": [], 1095 | "source": [ 1096 | "# MAX() and MIN() functions return the highest and lowest values in a column\n", 1097 | "\n", 1098 | "# what is the least number of times an app has been downloaded?\n", 1099 | "SELECT \n", 1100 | " MIN(downloads) \n", 1101 | "FROM \n", 1102 | " fake_apps;\n", 1103 | "\n", 1104 | "# How can you make the output show all rows that contain the maximum price, \n", 1105 | "# instead of just top one?\n", 1106 | "SELECT \n", 1107 | " name, \n", 1108 | " price \n", 1109 | "FROM \n", 1110 | " fake_apps \n", 1111 | "WHERE \n", 1112 | " price = (\n", 1113 | " SELECT \n", 1114 | " MAX(price) \n", 1115 | " FROM \n", 1116 | " fake_apps\n", 1117 | " );\n", 1118 | "# rather than SELECT-ing rows from the table where the price is exactly some number \n", 1119 | "# we specify, we compare price to the result of another query, \n", 1120 | "# in this case a MAX query." 1121 | ] 1122 | }, 1123 | { 1124 | "cell_type": "code", 1125 | "execution_count": null, 1126 | "metadata": {}, 1127 | "outputs": [], 1128 | "source": [ 1129 | "# AVG() function to quickly calculate the average value of a particular column.\n", 1130 | "\n", 1131 | "# the average number of downloads for an app in our database:\n", 1132 | "SELECT \n", 1133 | " AVG(downloads) \n", 1134 | "FROM \n", 1135 | " fake_apps;\n", 1136 | "\n", 1137 | "# get the average of only the unique values of a column:\n", 1138 | "SELECT \n", 1139 | " AVG(DISTINCT price) \n", 1140 | "FROM \n", 1141 | " fake_apps;" 1142 | ] 1143 | }, 1144 | { 1145 | "cell_type": "code", 1146 | "execution_count": null, 1147 | "metadata": {}, 1148 | "outputs": [], 1149 | "source": [ 1150 | "# ROUND() function takes 2 arguments inside the parenthesis: a column name; an integer.\n", 1151 | "# It rounds the values in the column to the number of decimal places specified by \n", 1152 | "# the integer. \n", 1153 | "\n", 1154 | "# return the name column and a rounded price column.\n", 1155 | "SELECT \n", 1156 | " name, \n", 1157 | " ROUND(price, 0) \n", 1158 | "FROM \n", 1159 | " fake_apps;\n", 1160 | "\n", 1161 | "# return the avg price of an app rounded up to 2 decimal places\n", 1162 | "SELECT \n", 1163 | " ROUND(\n", 1164 | " AVG(price), \n", 1165 | " 2) \n", 1166 | "FROM \n", 1167 | " fake_apps;" 1168 | ] 1169 | }, 1170 | { 1171 | "cell_type": "code", 1172 | "execution_count": null, 1173 | "metadata": {}, 1174 | "outputs": [], 1175 | "source": [ 1176 | "# GROUP BY is a clause in SQL that is used with aggregate functions. \n", 1177 | "# It is used in collaboration with the SELECT statement to arrange identical data \n", 1178 | "# into groups.\n", 1179 | "# The GROUP BY statement comes after any WHERE statements, but before ORDER BY or LIMIT.\n", 1180 | "\n", 1181 | "# The result contains the total number of apps for each price.\n", 1182 | "SELECT \n", 1183 | " price, \n", 1184 | " COUNT(*) \n", 1185 | "FROM \n", 1186 | " fake_apps \n", 1187 | "GROUP BY \n", 1188 | " price;\n", 1189 | "\n", 1190 | "# count the total number of apps that have been downloaded more than 20,000 times, \n", 1191 | "# at each price.\n", 1192 | "SELECT \n", 1193 | " price, \n", 1194 | " COUNT(*) \n", 1195 | "FROM \n", 1196 | " fake_apps \n", 1197 | "WHERE \n", 1198 | " downloads > 20000 \n", 1199 | "GROUP BY \n", 1200 | " price;\n", 1201 | "\n", 1202 | "# Write a new query that calculates the total number of downloads for each category.\n", 1203 | "SELECT \n", 1204 | " category, \n", 1205 | " SUM(downloads) \n", 1206 | "FROM \n", 1207 | " fake_apps \n", 1208 | "GROUP BY \n", 1209 | " category;" 1210 | ] 1211 | }, 1212 | { 1213 | "cell_type": "code", 1214 | "execution_count": null, 1215 | "metadata": {}, 1216 | "outputs": [], 1217 | "source": [ 1218 | "# SQL lets us use column reference(s) in our GROUP BY that will make our lives easier.\n", 1219 | "\n", 1220 | "# We might want to know how many movies have IMDb ratings that round to 1, 2, 3, 4, 5. \n", 1221 | "# Here, the 1 refers to the first column in our SELECT statement, ROUND(imdb_rating).\n", 1222 | "SELECT \n", 1223 | " ROUND(imdb_rating), \n", 1224 | " COUNT(name) \n", 1225 | "FROM \n", 1226 | " movies \n", 1227 | "GROUP BY \n", 1228 | " 1 \n", 1229 | "ORDER BY \n", 1230 | " 1;" 1231 | ] 1232 | }, 1233 | { 1234 | "cell_type": "code", 1235 | "execution_count": null, 1236 | "metadata": {}, 1237 | "outputs": [], 1238 | "source": [ 1239 | "# These are the same:\n", 1240 | "# Option 1:\n", 1241 | "SELECT \n", 1242 | " category, \n", 1243 | " price, \n", 1244 | " AVG(downloads) \n", 1245 | "FROM \n", 1246 | " fake_apps \n", 1247 | "GROUP BY \n", 1248 | " category, \n", 1249 | " price;\n", 1250 | "\n", 1251 | "# Option 2:\n", 1252 | "SELECT \n", 1253 | " category, \n", 1254 | " price, \n", 1255 | " AVG(downloads) \n", 1256 | "FROM \n", 1257 | " fake_apps \n", 1258 | "GROUP BY \n", 1259 | " 1, \n", 1260 | " 2;" 1261 | ] 1262 | }, 1263 | { 1264 | "cell_type": "code", 1265 | "execution_count": null, 1266 | "metadata": {}, 1267 | "outputs": [], 1268 | "source": [ 1269 | "# HAVING is very similar to WHERE. \n", 1270 | "# In fact, all types of WHERE clauses you learned about thus far \n", 1271 | "# can be used with HAVING.\n", 1272 | "\n", 1273 | "# When we want to limit the results of a query based on values of the individual rows, \n", 1274 | "# use WHERE.\n", 1275 | "\n", 1276 | "# When we want to limit the results of a query based on an aggregate property, \n", 1277 | "# use HAVING. \n", 1278 | "\n", 1279 | "# HAVING statement always comes after GROUP BY, but before ORDER BY and LIMIT.\n", 1280 | "SELECT \n", 1281 | " year, \n", 1282 | " genre, \n", 1283 | " COUNT(name) \n", 1284 | "FROM \n", 1285 | " movies \n", 1286 | "GROUP BY \n", 1287 | " 1, \n", 1288 | " 2 \n", 1289 | "HAVING \n", 1290 | " COUNT(name) > 10;\n", 1291 | "\n", 1292 | "# Example\n", 1293 | "# It returns the average downloads (rounded) and the number of apps at each price point.\n", 1294 | "# HAVING clause restricts the query to price points that have more than 10 apps.\n", 1295 | "SELECT \n", 1296 | " price, \n", 1297 | " ROUND(\n", 1298 | " AVG(downloads)\n", 1299 | " ), \n", 1300 | " COUNT(*) \n", 1301 | "FROM \n", 1302 | " fake_apps \n", 1303 | "GROUP BY \n", 1304 | " price \n", 1305 | "HAVING \n", 1306 | " COUNT(price) > 10;" 1307 | ] 1308 | }, 1309 | { 1310 | "cell_type": "code", 1311 | "execution_count": null, 1312 | "metadata": {}, 1313 | "outputs": [], 1314 | "source": [ 1315 | "# This will first filter the movies with a box_office > 500000.\n", 1316 | "# Then, it will group those results by genre, and finally restrict\n", 1317 | "# the query to genres that have more than 5 movies.\n", 1318 | "SELECT \n", 1319 | " genre, \n", 1320 | " ROUND(\n", 1321 | " AVG(score)\n", 1322 | " ) \n", 1323 | "FROM \n", 1324 | " movies \n", 1325 | "WHERE \n", 1326 | " box_office > 500000 \n", 1327 | "GROUP BY \n", 1328 | " genre \n", 1329 | "HAVING \n", 1330 | " COUNT(*) > 5;" 1331 | ] 1332 | }, 1333 | { 1334 | "cell_type": "code", 1335 | "execution_count": null, 1336 | "metadata": {}, 1337 | "outputs": [], 1338 | "source": [ 1339 | "# LESSON 3 REVIEW:\n", 1340 | "# -- COUNT(): count the number of rows\n", 1341 | "# -- SUM(): the sum of the values in a column\n", 1342 | "# -- MAX()/MIN(): the largest/smallest value\n", 1343 | "# -- AVG(): the average of the values in a column\n", 1344 | "# -- ROUND(): round the values in the column\n", 1345 | "\n", 1346 | "# Aggregate functions combine multiple rows together to form a single value\n", 1347 | "# of more meaningful information:\n", 1348 | "# -- GROUP BY is a clause used with aggregate functions to combine data \n", 1349 | "# from one or more columns.\n", 1350 | "# -- HAVING limit the results of a query based on an aggregate property." 1351 | ] 1352 | }, 1353 | { 1354 | "cell_type": "code", 1355 | "execution_count": null, 1356 | "metadata": {}, 1357 | "outputs": [], 1358 | "source": [] 1359 | }, 1360 | { 1361 | "cell_type": "code", 1362 | "execution_count": null, 1363 | "metadata": {}, 1364 | "outputs": [], 1365 | "source": [ 1366 | "# LESSON 4 'MULTIPLE TABLES'" 1367 | ] 1368 | }, 1369 | { 1370 | "cell_type": "code", 1371 | "execution_count": null, 1372 | "metadata": {}, 1373 | "outputs": [], 1374 | "source": [ 1375 | "# SQL gives us an easy way to combine tables: it’s called a JOIN.\n", 1376 | "# When we perform a simple JOIN (often called an inner join) \n", 1377 | "# our result only includes rows that match our ON condition.\n", 1378 | "\n", 1379 | "# If we want to combine table_1 and table_2 on customer_id column, we would type:\n", 1380 | "SELECT \n", 1381 | " * \n", 1382 | "FROM \n", 1383 | " table_1 \n", 1384 | " JOIN table_2 ON table_1.customer_id = table_2.customer_id;\n", 1385 | " \n", 1386 | "# Join orders table and subscriptions table and select all columns.\n", 1387 | "# Make sure to join on the subscription_id column \n", 1388 | "# where description is equal to ‘Fashion Magazine’..\n", 1389 | "SELECT \n", 1390 | " * \n", 1391 | "FROM \n", 1392 | " orders \n", 1393 | " JOIN subscriptions ON orders.subscription_id = subscriptions.subscription_id;\n", 1394 | "WHERE \n", 1395 | " description = 'Fashion Magazine';\n", 1396 | "\n", 1397 | "# count the number of subscribers of both services\n", 1398 | "SELECT \n", 1399 | " COUNT(*) AS subscribers_of_both \n", 1400 | "FROM \n", 1401 | " newspaper \n", 1402 | " JOIN online ON newspaper.id = online.id;" 1403 | ] 1404 | }, 1405 | { 1406 | "cell_type": "code", 1407 | "execution_count": null, 1408 | "metadata": {}, 1409 | "outputs": [], 1410 | "source": [ 1411 | "# LEFT JOIN will keep all rows from the first table, \n", 1412 | "# regardless of whether there is a matching row in the second table.\n", 1413 | "\n", 1414 | "# LEFT JOIN says: “Return all the data from the first table no matter what. \n", 1415 | "# If there are any matches with the second table, provide that information as well, \n", 1416 | "# but if not, just fill the missing data with NULL values.”\n", 1417 | "\n", 1418 | "# The final result will keep all rows of the first table \n", 1419 | "# but will omit the un-matched rows from the second table.\n", 1420 | "SELECT \n", 1421 | " * \n", 1422 | "FROM \n", 1423 | " table1 \n", 1424 | " LEFT JOIN table2 ON table1.c2 = table2.c2;\n", 1425 | " \n", 1426 | "# which users do not subscribe to the online edition\n", 1427 | "# (add where clause)\n", 1428 | "SELECT \n", 1429 | " * \n", 1430 | "FROM \n", 1431 | " newspaper \n", 1432 | " LEFT JOIN online ON newspaper.id = online.id \n", 1433 | "WHERE \n", 1434 | " online.id IS NULL;\n" 1435 | ] 1436 | }, 1437 | { 1438 | "cell_type": "code", 1439 | "execution_count": null, 1440 | "metadata": {}, 1441 | "outputs": [], 1442 | "source": [ 1443 | "# PRIMARY KEYS\n", 1444 | "# Primary keys are unique identifiers that have following requirements:\n", 1445 | "# -- None of the values can be NULL.\n", 1446 | "# -- Each value must be unique.\n", 1447 | "# -- A table can not have more than one primary key column." 1448 | ] 1449 | }, 1450 | { 1451 | "cell_type": "code", 1452 | "execution_count": null, 1453 | "metadata": {}, 1454 | "outputs": [], 1455 | "source": [ 1456 | "# FOREIGN KEYS\n", 1457 | "# When the primary key for one table appears in a different table, \n", 1458 | "# it is called a foreign key. \n", 1459 | "\n", 1460 | "# For example, an _id is a primary key when it appears in classes, \n", 1461 | "# but it will be a foreign key with a name class_id when it appears in students.\n", 1462 | "SELECT \n", 1463 | " * \n", 1464 | "FROM \n", 1465 | " classes \n", 1466 | " JOIN students ON classes.id = students.class_id;" 1467 | ] 1468 | }, 1469 | { 1470 | "cell_type": "code", 1471 | "execution_count": null, 1472 | "metadata": {}, 1473 | "outputs": [], 1474 | "source": [ 1475 | "# CROSS JOIN\n", 1476 | "# Sometimes, we just want to combine all rows of one table with all rows \n", 1477 | "# of another table. \n", 1478 | "\n", 1479 | "# For instance, if we had a table of shirts and a table of pants, \n", 1480 | "# we might want to know all the possible combinations to create different outfits.\n", 1481 | "SELECT \n", 1482 | " shirts.shirt_color, \n", 1483 | " pants.pants_color \n", 1484 | "FROM \n", 1485 | " shirts CROSS \n", 1486 | " JOIN pants;\n", 1487 | "\n", 1488 | "# A more common usage of CROSS JOIN is when we need to compare each row of a table \n", 1489 | "# to a list of values." 1490 | ] 1491 | }, 1492 | { 1493 | "cell_type": "code", 1494 | "execution_count": null, 1495 | "metadata": {}, 1496 | "outputs": [], 1497 | "source": [ 1498 | "# count the number of customers who were subscribed to the newspaper during March. \n", 1499 | "# “During March” means that the customer’s starting month was in or before March \n", 1500 | "# and final month was in or after March:\n", 1501 | "SELECT \n", 1502 | " COUNT(*) \n", 1503 | "FROM \n", 1504 | " newspaper \n", 1505 | "WHERE \n", 1506 | " start_month <= 3 \n", 1507 | " AND end_month >= 3;" 1508 | ] 1509 | }, 1510 | { 1511 | "cell_type": "code", 1512 | "execution_count": null, 1513 | "metadata": {}, 1514 | "outputs": [], 1515 | "source": [ 1516 | "# Create a final query where you aggregate over each month \n", 1517 | "# to count the number of subscribers.\n", 1518 | "SELECT \n", 1519 | " month, \n", 1520 | " COUNT(*) \n", 1521 | "FROM \n", 1522 | " newspaper CROSS \n", 1523 | " JOIN months \n", 1524 | "WHERE \n", 1525 | " start_month <= month \n", 1526 | " AND end_month >= month \n", 1527 | "GROUP BY \n", 1528 | " month;" 1529 | ] 1530 | }, 1531 | { 1532 | "cell_type": "code", 1533 | "execution_count": null, 1534 | "metadata": {}, 1535 | "outputs": [], 1536 | "source": [ 1537 | "# You can CROSS JOIN as many tables as you want.\n", 1538 | "# Example\n", 1539 | "SELECT \n", 1540 | " shirts.shirt_color, \n", 1541 | " pants.pants_color, \n", 1542 | " socks.sock_color \n", 1543 | "FROM \n", 1544 | " shirts CROSS \n", 1545 | " JOIN pants CROSS \n", 1546 | " JOIN socks;" 1547 | ] 1548 | }, 1549 | { 1550 | "cell_type": "code", 1551 | "execution_count": null, 1552 | "metadata": {}, 1553 | "outputs": [], 1554 | "source": [ 1555 | "# Sometimes we just want to stack one dataset on top of the other. The UNION operator\n", 1556 | "# allows us to do that.\n", 1557 | "# When you combine tables with UNION, duplicate rows will be excluded.\n", 1558 | "\n", 1559 | "# SQL has strict rules for appending data:\n", 1560 | "# -- Tables must have the same number of columns.\n", 1561 | "# -- The columns must have the same data types in the same order as the first table.\n", 1562 | "\n", 1563 | "# We’d like to create one big table with both sets of data. \n", 1564 | "SELECT \n", 1565 | " * \n", 1566 | "FROM \n", 1567 | " table1 \n", 1568 | "UNION \n", 1569 | "SELECT \n", 1570 | " * \n", 1571 | "FROM \n", 1572 | " table2;" 1573 | ] 1574 | }, 1575 | { 1576 | "cell_type": "code", 1577 | "execution_count": null, 1578 | "metadata": {}, 1579 | "outputs": [], 1580 | "source": [ 1581 | "# The WITH statement allows us to perform a separate query \n", 1582 | "# (such as aggregating customer’s subscriptions)\n", 1583 | "# previous_results is the alias that we will use to reference any columns \n", 1584 | "# from the query inside of the WITH clause\n", 1585 | "# We can then go on to do whatever we want with this temporary table \n", 1586 | "# (such as join the temporary table with another table)\n", 1587 | "WITH previous_results AS (\n", 1588 | " SELECT \n", 1589 | " ...\n", 1590 | " ...\n", 1591 | " ...\n", 1592 | " ...\n", 1593 | ")\n", 1594 | "SELECT \n", 1595 | " * \n", 1596 | "FROM \n", 1597 | " previous_results \n", 1598 | " JOIN customers ON _____ = _____;\n", 1599 | "\n", 1600 | "# Thus, we are putting a whole first query inside the parentheses () \n", 1601 | "# and giving it a name. After that, we can use this name as if it’s a table \n", 1602 | "# and write a new query using the first query." 1603 | ] 1604 | }, 1605 | { 1606 | "cell_type": "code", 1607 | "execution_count": null, 1608 | "metadata": {}, 1609 | "outputs": [], 1610 | "source": [ 1611 | "# EXAMPLE\n", 1612 | "WITH previous_query AS (\n", 1613 | " SELECT \n", 1614 | " customer_id, \n", 1615 | " COUNT(subscription_id) AS 'subscriptions' \n", 1616 | " FROM \n", 1617 | " orders \n", 1618 | " GROUP BY \n", 1619 | " customer_id\n", 1620 | ") \n", 1621 | "SELECT \n", 1622 | " customers.customer_name, \n", 1623 | " previous_query.subscriptions \n", 1624 | "FROM \n", 1625 | " previous_query \n", 1626 | " JOIN customers ON previous_query.customer_id = customers.customer_id;" 1627 | ] 1628 | }, 1629 | { 1630 | "cell_type": "code", 1631 | "execution_count": null, 1632 | "metadata": {}, 1633 | "outputs": [], 1634 | "source": [ 1635 | "# You can use WITH for more than one nested query. \n", 1636 | "# You can do so by listing each query using commas after the WITH.\n", 1637 | "WITH\n", 1638 | "query1 AS (SELECT column1 FROM table1 WHERE condition1),\n", 1639 | "query2 AS (SELECT column2 FROM table2 WHERE condition2)\n", 1640 | "…" 1641 | ] 1642 | }, 1643 | { 1644 | "cell_type": "code", 1645 | "execution_count": null, 1646 | "metadata": {}, 1647 | "outputs": [], 1648 | "source": [ 1649 | "# LESSON 4 REVIEW:\n", 1650 | "# -- JOIN will combine rows from different tables if the join condition is true.\n", 1651 | "# -- LEFT JOIN will return every row in the left table, \n", 1652 | "# and if the join condition is not met, NULL values are used to fill in the columns \n", 1653 | "# from the right table.\n", 1654 | "# -- Primary key is a column that serves a unique identifier for the rows in the table.\n", 1655 | "# -- Foreign key is a column that contains the primary key to another table.\n", 1656 | "# -- CROSS JOIN lets us combine all rows of one table with all rows of another table.\n", 1657 | "# -- UNION stacks one dataset on top of another.\n", 1658 | "# -- WITH allows us to define one or more temporary tables that can be used \n", 1659 | "# in the final query." 1660 | ] 1661 | } 1662 | ], 1663 | "metadata": { 1664 | "kernelspec": { 1665 | "display_name": "Python 3", 1666 | "language": "python", 1667 | "name": "python3" 1668 | }, 1669 | "language_info": { 1670 | "codemirror_mode": { 1671 | "name": "ipython", 1672 | "version": 3 1673 | }, 1674 | "file_extension": ".py", 1675 | "mimetype": "text/x-python", 1676 | "name": "python", 1677 | "nbconvert_exporter": "python", 1678 | "pygments_lexer": "ipython3", 1679 | "version": "3.7.6" 1680 | } 1681 | }, 1682 | "nbformat": 4, 1683 | "nbformat_minor": 4 1684 | } 1685 | -------------------------------------------------------------------------------- /2_sql_ex/.ipynb_checkpoints/sql_ex-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "SQL-EX.RU" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "DATABASE DESCRIPTION \"Computer firm\":\n", 15 | "\n", 16 | "The database scheme consists of four tables: \n", 17 | "* Product(maker, model, type) \n", 18 | "* PC(code, model, speed, ram, hd, cd, price) \n", 19 | "* Laptop(code, model, speed, ram, hd, screen, price) \n", 20 | "* Printer(code, model, color, type, price) \n", 21 | "\n", 22 | "The Product table contains data on the maker, model number, and type of product ('PC', 'Laptop', or 'Printer'). It is assumed that model numbers in the Product table are unique for all makers and product types. Each personal computer in the PC table is unambiguously identified by a unique code, and is additionally characterized by its model (foreign key referring to the Product table), processor speed (in MHz) – speed field, RAM capacity (in Mb) - ram, hard disk drive capacity (in Gb) – hd, CD-ROM speed (e.g, '4x') - cd, and its price. The Laptop table is similar to the PC table, except that instead of the CD-ROM speed, it contains the screen size (in inches) – screen. For each printer model in the Printer table, its output type (‘y’ for color and ‘n’ for monochrome) – color field, printing technology ('Laser', 'Jet', or 'Matrix') – type, and price are specified." 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "# Task 1\n", 32 | "# Find the model number, speed and hard drive capacity for all the PCs with prices \n", 33 | "# below $500.\n", 34 | "# Result set: model, speed, hd.\n", 35 | "SELECT \n", 36 | " model, \n", 37 | " speed, \n", 38 | " hd \n", 39 | "FROM \n", 40 | " pc \n", 41 | "WHERE \n", 42 | " price < 500;" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "# Task 2\n", 52 | "# List all printer makers. \n", 53 | "# Result set: maker. \n", 54 | "SELECT \n", 55 | " DISTINCT maker \n", 56 | "FROM \n", 57 | " product \n", 58 | "WHERE \n", 59 | " type = 'Printer';" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "# Task 3\n", 69 | "# Find the model number, RAM and screen size of the laptops with prices over $1000. \n", 70 | "SELECT \n", 71 | " model, \n", 72 | " ram, \n", 73 | " screen \n", 74 | "FROM \n", 75 | " laptop \n", 76 | "WHERE \n", 77 | " price > 1000;" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "# Task 4\n", 87 | "# Find all records from the Printer table containing data about color printers.\n", 88 | "SELECT \n", 89 | " * \n", 90 | "FROM \n", 91 | " printer \n", 92 | "WHERE \n", 93 | " color = 'y';" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# Task 5\n", 103 | "# Find the model number, speed and hard drive capacity of PCs cheaper than \n", 104 | "# $600 having a 12x or a 24x CD drive. \n", 105 | "SELECT \n", 106 | " model, \n", 107 | " speed, \n", 108 | " hd \n", 109 | "FROM \n", 110 | " pc \n", 111 | "WHERE \n", 112 | " (\n", 113 | " cd = '12x' \n", 114 | " OR cd = '24x'\n", 115 | " ) \n", 116 | " AND price < 600;" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "# Task 6\n", 126 | "# For each maker producing laptops with a hard drive capacity of 10 Gb or higher, \n", 127 | "# find the speed of such laptops. \n", 128 | "# Result set: maker, speed. \n", 129 | "SELECT \n", 130 | " DISTINCT maker, \n", 131 | " speed \n", 132 | "FROM \n", 133 | " product \n", 134 | " JOIN laptop ON product.model = laptop.model \n", 135 | "WHERE \n", 136 | " hd >= 10;" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "# Task 7\n", 146 | "# Get the models and prices for all commercially available products (of any type) \n", 147 | "# produced by maker B. \n", 148 | "SELECT \n", 149 | " DISTINCT product.model, \n", 150 | " price \n", 151 | "FROM \n", 152 | " product \n", 153 | " JOIN pc ON product.model = pc.model \n", 154 | "WHERE \n", 155 | " maker = 'B' \n", 156 | "UNION \n", 157 | "SELECT \n", 158 | " DISTINCT product.model, \n", 159 | " price \n", 160 | "FROM \n", 161 | " product \n", 162 | " JOIN laptop ON product.model = laptop.model \n", 163 | "WHERE \n", 164 | " maker = 'B' \n", 165 | "UNION \n", 166 | "SELECT \n", 167 | " DISTINCT product.model, \n", 168 | " price \n", 169 | "FROM \n", 170 | " product \n", 171 | " JOIN printer ON product.model = printer.model \n", 172 | "WHERE \n", 173 | " maker = 'B';" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "# Task 8\n", 183 | "# Find the makers producing PCs but not laptops. \n", 184 | "SELECT \n", 185 | " DISTINCT maker \n", 186 | "FROM \n", 187 | " product \n", 188 | "WHERE \n", 189 | " type = 'PC' \n", 190 | " AND maker NOT in (\n", 191 | " SELECT \n", 192 | " DISTINCT maker \n", 193 | " FROM \n", 194 | " product \n", 195 | " WHERE \n", 196 | " type = 'Laptop'\n", 197 | " );" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [ 206 | "# Task 9\n", 207 | "# Find the makers of PCs with a processor speed of 450 MHz or more. \n", 208 | "# Result set: maker. \n", 209 | "SELECT \n", 210 | " DISTINCT maker \n", 211 | "FROM \n", 212 | " product \n", 213 | " JOIN pc ON product.model = pc.model \n", 214 | "WHERE \n", 215 | " pc.speed >= 450;" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": {}, 222 | "outputs": [], 223 | "source": [ 224 | "# Task 10\n", 225 | "# Find the printer models having the highest price. \n", 226 | "# Result set: model, price.\n", 227 | "SELECT \n", 228 | " model, \n", 229 | " price \n", 230 | "FROM \n", 231 | " printer \n", 232 | "WHERE \n", 233 | " price = (\n", 234 | " SELECT \n", 235 | " MAX(price) \n", 236 | " FROM \n", 237 | " printer\n", 238 | " );" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": null, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "# Task 11\n", 248 | "# Find out the average speed of PCs. \n", 249 | "SELECT \n", 250 | " AVG(speed) AS avg_pc_speed \n", 251 | "FROM \n", 252 | " pc;" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "# Task 12\n", 262 | "# Find out the average speed of the laptops priced over $1000. \n", 263 | "SELECT \n", 264 | " AVG(speed) \n", 265 | "FROM \n", 266 | " laptop \n", 267 | "WHERE \n", 268 | " price > 1000;" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "# Task 13\n", 278 | "# Find out the average speed of the PCs produced by maker A. \n", 279 | "SELECT \n", 280 | " AVG(speed) \n", 281 | "FROM \n", 282 | " pc \n", 283 | " JOIN product ON pc.model = product.model \n", 284 | "WHERE \n", 285 | " maker = 'A';" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "DATABASE DESCRIPTION \"Ships\":\n", 293 | "\n", 294 | "The database of naval ships that took part in World War II is under consideration. The database consists of the following relations: \n", 295 | "* Classes(class, type, country, numGuns, bore, displacement) \n", 296 | "* Ships(name, class, launched) \n", 297 | "* Battles(name, date) \n", 298 | "* Outcomes(ship, battle, result) \n", 299 | "\n", 300 | "Ships in classes all have the same general design. A class is normally assigned either the name of the first ship built according to the corresponding design, or a name that is different from any ship name in the database. The ship whose name is assigned to a class is called a lead ship. \n", 301 | "The Classes relation includes the name of the class, type (can be either bb for a battle ship, or bc for a battle cruiser), country the ship was built in, the number of main guns, gun caliber (bore diameter in inches), and displacement (weight in tons). The Ships relation holds information about the ship name, the name of its corresponding class, and the year the ship was launched. The Battles relation contains names and dates of battles the ships participated in, and the Outcomes relation - the battle result for a given ship (may be sunk, damaged, or OK, the last value meaning the ship survived the battle unharmed). \n", 302 | "\n", 303 | "Notes: 1) The Outcomes relation may contain ships not present in the Ships relation. 2) A ship sunk can’t participate in later battles. 3) For historical reasons, lead ships are referred to as head ships in many exercises.4) A ship found in the Outcomes table but not in the Ships table is still considered in the database. This is true even if it is sunk." 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": null, 309 | "metadata": {}, 310 | "outputs": [], 311 | "source": [ 312 | "# Task 14\n", 313 | "# For the ships in the Ships table that have at least 10 guns, get the class, \n", 314 | "# name, and country.\n", 315 | "SELECT \n", 316 | " classes.class, \n", 317 | " name, \n", 318 | " country \n", 319 | "FROM \n", 320 | " classes \n", 321 | " JOIN ships ON classes.class = ships.class \n", 322 | "WHERE \n", 323 | " numGuns >= 10;" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": null, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "# Task 15\n", 333 | "# Get hard drive capacities that are identical for two or more PCs.\n", 334 | "# Result set: hd. \n", 335 | "SELECT \n", 336 | " hd \n", 337 | "FROM \n", 338 | " pc \n", 339 | "GROUP BY \n", 340 | " hd \n", 341 | "HAVING \n", 342 | " COUNT(model) >= 2;" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "# Task 16\n", 352 | "# Get pairs of PC models with identical speeds and the same RAM capacity. \n", 353 | "# Each resulting pair should be displayed only once, i.e. (i, j) but not (j, i).\n", 354 | "# Result set: model with the bigger number, model with the smaller number, speed, RAM. \n", 355 | "SELECT \n", 356 | " DISTINCT a.model, \n", 357 | " b.model, \n", 358 | " a.speed, \n", 359 | " a.ram \n", 360 | "FROM \n", 361 | " pc AS a, \n", 362 | " pc b \n", 363 | "WHERE \n", 364 | " (\n", 365 | " a.speed = b.speed \n", 366 | " AND a.ram = b.ram\n", 367 | " ) \n", 368 | " AND a.model > b.model;" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": null, 374 | "metadata": {}, 375 | "outputs": [], 376 | "source": [ 377 | "# Task 17\n", 378 | "# Get the laptop models that have a speed smaller than the speed of any PC.\n", 379 | "# Result set: type, model, speed. \n", 380 | "SELECT \n", 381 | " DISTINCT product.type AS Type, \n", 382 | " laptop.model AS Model, \n", 383 | " laptop.speed AS speed \n", 384 | "FROM \n", 385 | " product \n", 386 | " JOIN laptop ON product.model = laptop.model \n", 387 | "WHERE \n", 388 | " laptop.speed < (\n", 389 | " SELECT \n", 390 | " MIN (speed) \n", 391 | " FROM \n", 392 | " PC\n", 393 | " ) \n", 394 | " AND product.type = 'laptop';" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": null, 400 | "metadata": {}, 401 | "outputs": [], 402 | "source": [ 403 | "# Task 18\n", 404 | "# Find the makers of the cheapest color printers.\n", 405 | "# Result set: maker, price.\n", 406 | "SELECT \n", 407 | " DISTINCT product.maker, \n", 408 | " printer.price \n", 409 | "FROM \n", 410 | " product \n", 411 | " JOIN printer ON product.model = printer.model \n", 412 | "WHERE \n", 413 | " printer.price = (\n", 414 | " SELECT \n", 415 | " MIN(printer.price) \n", 416 | " FROM \n", 417 | " printer \n", 418 | " WHERE \n", 419 | " printer.color = 'y'\n", 420 | " ) \n", 421 | " AND printer.color = 'y';" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": null, 427 | "metadata": {}, 428 | "outputs": [], 429 | "source": [ 430 | "# Task 19\n", 431 | "# For each maker having models in the Laptop table, find out the average screen size \n", 432 | "# of the laptops he produces.\n", 433 | "# Result set: maker, average screen size. \n", 434 | "SELECT \n", 435 | " p.maker, \n", 436 | " AVG(l.screen) \n", 437 | "FROM \n", 438 | " product AS p \n", 439 | " JOIN laptop AS l ON p.model = l.model \n", 440 | "GROUP BY \n", 441 | " p.maker;" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": null, 447 | "metadata": {}, 448 | "outputs": [], 449 | "source": [ 450 | "# Task 20\n", 451 | "# Find the makers producing at least three distinct models of PCs.\n", 452 | "# Result set: maker, number of PC models.\n", 453 | "SELECT \n", 454 | " maker, \n", 455 | " COUNT(model) AS Count_Model \n", 456 | "FROM \n", 457 | " product \n", 458 | "WHERE \n", 459 | " type = 'pc' \n", 460 | "GROUP BY \n", 461 | " maker \n", 462 | "HAVING \n", 463 | " COUNT(DISTINCT model) >= 3;" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": null, 469 | "metadata": {}, 470 | "outputs": [], 471 | "source": [ 472 | "# Task 21\n", 473 | "# Find out the maximum PC price for each maker having models in the PC table. \n", 474 | "# Result set: maker, maximum price. \n", 475 | "SELECT \n", 476 | " maker, \n", 477 | " MAX(price) \n", 478 | "FROM \n", 479 | " pc \n", 480 | " JOIN product ON pc.model = product.model \n", 481 | "GROUP BY \n", 482 | " maker;" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": null, 488 | "metadata": {}, 489 | "outputs": [], 490 | "source": [ 491 | "# Task 22\n", 492 | "# For each value of PC speed that exceeds 600 MHz, find out the average price of PCs \n", 493 | "# with identical speeds.\n", 494 | "# Result set: speed, average price. \n", 495 | "SELECT \n", 496 | " speed, \n", 497 | " AVG(price) \n", 498 | "FROM \n", 499 | " pc \n", 500 | "GROUP BY \n", 501 | " speed \n", 502 | "HAVING \n", 503 | " speed > 600;" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": null, 509 | "metadata": {}, 510 | "outputs": [], 511 | "source": [ 512 | "# Task 23\n", 513 | "# Get the makers producing both PCs having a speed of 750 MHz or higher \n", 514 | "# and laptops with a speed of 750 MHz or higher.\n", 515 | "# Result set: maker \n", 516 | "SELECT \n", 517 | " DISTINCT maker \n", 518 | "FROM \n", 519 | " pc \n", 520 | " JOIN product ON pc.model = product.model \n", 521 | "WHERE \n", 522 | " pc.speed >= 750 \n", 523 | " AND maker IN (\n", 524 | " SELECT \n", 525 | " maker \n", 526 | " FROM \n", 527 | " laptop \n", 528 | " JOIN product ON laptop.model = product.model \n", 529 | " WHERE \n", 530 | " laptop.speed >= 750\n", 531 | " );" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": null, 537 | "metadata": {}, 538 | "outputs": [], 539 | "source": [ 540 | "# Task 24\n", 541 | "tbc.." 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": null, 547 | "metadata": {}, 548 | "outputs": [], 549 | "source": [] 550 | } 551 | ], 552 | "metadata": { 553 | "kernelspec": { 554 | "display_name": "Python 3", 555 | "language": "python", 556 | "name": "python3" 557 | }, 558 | "language_info": { 559 | "codemirror_mode": { 560 | "name": "ipython", 561 | "version": 3 562 | }, 563 | "file_extension": ".py", 564 | "mimetype": "text/x-python", 565 | "name": "python", 566 | "nbconvert_exporter": "python", 567 | "pygments_lexer": "ipython3", 568 | "version": "3.7.6" 569 | } 570 | }, 571 | "nbformat": 4, 572 | "nbformat_minor": 4 573 | } 574 | -------------------------------------------------------------------------------- /2_sql_ex/sql_ex.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "SQL-EX.RU" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "DATABASE DESCRIPTION \"Computer firm\":\n", 15 | "\n", 16 | "The database scheme consists of four tables: \n", 17 | "* Product(maker, model, type) \n", 18 | "* PC(code, model, speed, ram, hd, cd, price) \n", 19 | "* Laptop(code, model, speed, ram, hd, screen, price) \n", 20 | "* Printer(code, model, color, type, price) \n", 21 | "\n", 22 | "The Product table contains data on the maker, model number, and type of product ('PC', 'Laptop', or 'Printer'). It is assumed that model numbers in the Product table are unique for all makers and product types. Each personal computer in the PC table is unambiguously identified by a unique code, and is additionally characterized by its model (foreign key referring to the Product table), processor speed (in MHz) – speed field, RAM capacity (in Mb) - ram, hard disk drive capacity (in Gb) – hd, CD-ROM speed (e.g, '4x') - cd, and its price. The Laptop table is similar to the PC table, except that instead of the CD-ROM speed, it contains the screen size (in inches) – screen. For each printer model in the Printer table, its output type (‘y’ for color and ‘n’ for monochrome) – color field, printing technology ('Laser', 'Jet', or 'Matrix') – type, and price are specified." 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "# Task 1\n", 32 | "# Find the model number, speed and hard drive capacity for all the PCs with prices \n", 33 | "# below $500.\n", 34 | "# Result set: model, speed, hd.\n", 35 | "SELECT \n", 36 | " model, \n", 37 | " speed, \n", 38 | " hd \n", 39 | "FROM \n", 40 | " pc \n", 41 | "WHERE \n", 42 | " price < 500;" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "# Task 2\n", 52 | "# List all printer makers. \n", 53 | "# Result set: maker. \n", 54 | "SELECT \n", 55 | " DISTINCT maker \n", 56 | "FROM \n", 57 | " product \n", 58 | "WHERE \n", 59 | " type = 'Printer';" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "# Task 3\n", 69 | "# Find the model number, RAM and screen size of the laptops with prices over $1000. \n", 70 | "SELECT \n", 71 | " model, \n", 72 | " ram, \n", 73 | " screen \n", 74 | "FROM \n", 75 | " laptop \n", 76 | "WHERE \n", 77 | " price > 1000;" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "# Task 4\n", 87 | "# Find all records from the Printer table containing data about color printers.\n", 88 | "SELECT \n", 89 | " * \n", 90 | "FROM \n", 91 | " printer \n", 92 | "WHERE \n", 93 | " color = 'y';" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# Task 5\n", 103 | "# Find the model number, speed and hard drive capacity of PCs cheaper than \n", 104 | "# $600 having a 12x or a 24x CD drive. \n", 105 | "SELECT \n", 106 | " model, \n", 107 | " speed, \n", 108 | " hd \n", 109 | "FROM \n", 110 | " pc \n", 111 | "WHERE \n", 112 | " (\n", 113 | " cd = '12x' \n", 114 | " OR cd = '24x'\n", 115 | " ) \n", 116 | " AND price < 600;" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "# Task 6\n", 126 | "# For each maker producing laptops with a hard drive capacity of 10 Gb or higher, \n", 127 | "# find the speed of such laptops. \n", 128 | "# Result set: maker, speed. \n", 129 | "SELECT \n", 130 | " DISTINCT maker, \n", 131 | " speed \n", 132 | "FROM \n", 133 | " product \n", 134 | " JOIN laptop ON product.model = laptop.model \n", 135 | "WHERE \n", 136 | " hd >= 10;" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "# Task 7\n", 146 | "# Get the models and prices for all commercially available products (of any type) \n", 147 | "# produced by maker B. \n", 148 | "SELECT \n", 149 | " DISTINCT product.model, \n", 150 | " price \n", 151 | "FROM \n", 152 | " product \n", 153 | " JOIN pc ON product.model = pc.model \n", 154 | "WHERE \n", 155 | " maker = 'B' \n", 156 | "UNION \n", 157 | "SELECT \n", 158 | " DISTINCT product.model, \n", 159 | " price \n", 160 | "FROM \n", 161 | " product \n", 162 | " JOIN laptop ON product.model = laptop.model \n", 163 | "WHERE \n", 164 | " maker = 'B' \n", 165 | "UNION \n", 166 | "SELECT \n", 167 | " DISTINCT product.model, \n", 168 | " price \n", 169 | "FROM \n", 170 | " product \n", 171 | " JOIN printer ON product.model = printer.model \n", 172 | "WHERE \n", 173 | " maker = 'B';" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "# Task 8\n", 183 | "# Find the makers producing PCs but not laptops. \n", 184 | "SELECT \n", 185 | " DISTINCT maker \n", 186 | "FROM \n", 187 | " product \n", 188 | "WHERE \n", 189 | " type = 'PC' \n", 190 | " AND maker NOT in (\n", 191 | " SELECT \n", 192 | " DISTINCT maker \n", 193 | " FROM \n", 194 | " product \n", 195 | " WHERE \n", 196 | " type = 'Laptop'\n", 197 | " );" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [ 206 | "# Task 9\n", 207 | "# Find the makers of PCs with a processor speed of 450 MHz or more. \n", 208 | "# Result set: maker. \n", 209 | "SELECT \n", 210 | " DISTINCT maker \n", 211 | "FROM \n", 212 | " product \n", 213 | " JOIN pc ON product.model = pc.model \n", 214 | "WHERE \n", 215 | " pc.speed >= 450;" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": {}, 222 | "outputs": [], 223 | "source": [ 224 | "# Task 10\n", 225 | "# Find the printer models having the highest price. \n", 226 | "# Result set: model, price.\n", 227 | "SELECT \n", 228 | " model, \n", 229 | " price \n", 230 | "FROM \n", 231 | " printer \n", 232 | "WHERE \n", 233 | " price = (\n", 234 | " SELECT \n", 235 | " MAX(price) \n", 236 | " FROM \n", 237 | " printer\n", 238 | " );" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": null, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "# Task 11\n", 248 | "# Find out the average speed of PCs. \n", 249 | "SELECT \n", 250 | " AVG(speed) AS avg_pc_speed \n", 251 | "FROM \n", 252 | " pc;" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "# Task 12\n", 262 | "# Find out the average speed of the laptops priced over $1000. \n", 263 | "SELECT \n", 264 | " AVG(speed) \n", 265 | "FROM \n", 266 | " laptop \n", 267 | "WHERE \n", 268 | " price > 1000;" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "# Task 13\n", 278 | "# Find out the average speed of the PCs produced by maker A. \n", 279 | "SELECT \n", 280 | " AVG(speed) \n", 281 | "FROM \n", 282 | " pc \n", 283 | " JOIN product ON pc.model = product.model \n", 284 | "WHERE \n", 285 | " maker = 'A';" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "DATABASE DESCRIPTION \"Ships\":\n", 293 | "\n", 294 | "The database of naval ships that took part in World War II is under consideration. The database consists of the following relations: \n", 295 | "* Classes(class, type, country, numGuns, bore, displacement) \n", 296 | "* Ships(name, class, launched) \n", 297 | "* Battles(name, date) \n", 298 | "* Outcomes(ship, battle, result) \n", 299 | "\n", 300 | "Ships in classes all have the same general design. A class is normally assigned either the name of the first ship built according to the corresponding design, or a name that is different from any ship name in the database. The ship whose name is assigned to a class is called a lead ship. \n", 301 | "The Classes relation includes the name of the class, type (can be either bb for a battle ship, or bc for a battle cruiser), country the ship was built in, the number of main guns, gun caliber (bore diameter in inches), and displacement (weight in tons). The Ships relation holds information about the ship name, the name of its corresponding class, and the year the ship was launched. The Battles relation contains names and dates of battles the ships participated in, and the Outcomes relation - the battle result for a given ship (may be sunk, damaged, or OK, the last value meaning the ship survived the battle unharmed). \n", 302 | "\n", 303 | "Notes: 1) The Outcomes relation may contain ships not present in the Ships relation. 2) A ship sunk can’t participate in later battles. 3) For historical reasons, lead ships are referred to as head ships in many exercises.4) A ship found in the Outcomes table but not in the Ships table is still considered in the database. This is true even if it is sunk." 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": null, 309 | "metadata": {}, 310 | "outputs": [], 311 | "source": [ 312 | "# Task 14\n", 313 | "# For the ships in the Ships table that have at least 10 guns, get the class, \n", 314 | "# name, and country.\n", 315 | "SELECT \n", 316 | " classes.class, \n", 317 | " name, \n", 318 | " country \n", 319 | "FROM \n", 320 | " classes \n", 321 | " JOIN ships ON classes.class = ships.class \n", 322 | "WHERE \n", 323 | " numGuns >= 10;" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": null, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "# Task 15\n", 333 | "# Get hard drive capacities that are identical for two or more PCs.\n", 334 | "# Result set: hd. \n", 335 | "SELECT \n", 336 | " hd \n", 337 | "FROM \n", 338 | " pc \n", 339 | "GROUP BY \n", 340 | " hd \n", 341 | "HAVING \n", 342 | " COUNT(model) >= 2;" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "# Task 16\n", 352 | "# Get pairs of PC models with identical speeds and the same RAM capacity. \n", 353 | "# Each resulting pair should be displayed only once, i.e. (i, j) but not (j, i).\n", 354 | "# Result set: model with the bigger number, model with the smaller number, speed, RAM. \n", 355 | "SELECT \n", 356 | " DISTINCT a.model, \n", 357 | " b.model, \n", 358 | " a.speed, \n", 359 | " a.ram \n", 360 | "FROM \n", 361 | " pc AS a, \n", 362 | " pc b \n", 363 | "WHERE \n", 364 | " (\n", 365 | " a.speed = b.speed \n", 366 | " AND a.ram = b.ram\n", 367 | " ) \n", 368 | " AND a.model > b.model;" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": null, 374 | "metadata": {}, 375 | "outputs": [], 376 | "source": [ 377 | "# Task 17\n", 378 | "# Get the laptop models that have a speed smaller than the speed of any PC.\n", 379 | "# Result set: type, model, speed. \n", 380 | "SELECT \n", 381 | " DISTINCT product.type AS Type, \n", 382 | " laptop.model AS Model, \n", 383 | " laptop.speed AS speed \n", 384 | "FROM \n", 385 | " product \n", 386 | " JOIN laptop ON product.model = laptop.model \n", 387 | "WHERE \n", 388 | " laptop.speed < (\n", 389 | " SELECT \n", 390 | " MIN (speed) \n", 391 | " FROM \n", 392 | " PC\n", 393 | " ) \n", 394 | " AND product.type = 'laptop';" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": null, 400 | "metadata": {}, 401 | "outputs": [], 402 | "source": [ 403 | "# Task 18\n", 404 | "# Find the makers of the cheapest color printers.\n", 405 | "# Result set: maker, price.\n", 406 | "SELECT \n", 407 | " DISTINCT product.maker, \n", 408 | " printer.price \n", 409 | "FROM \n", 410 | " product \n", 411 | " JOIN printer ON product.model = printer.model \n", 412 | "WHERE \n", 413 | " printer.price = (\n", 414 | " SELECT \n", 415 | " MIN(printer.price) \n", 416 | " FROM \n", 417 | " printer \n", 418 | " WHERE \n", 419 | " printer.color = 'y'\n", 420 | " ) \n", 421 | " AND printer.color = 'y';" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": null, 427 | "metadata": {}, 428 | "outputs": [], 429 | "source": [ 430 | "# Task 19\n", 431 | "# For each maker having models in the Laptop table, find out the average screen size \n", 432 | "# of the laptops he produces.\n", 433 | "# Result set: maker, average screen size. \n", 434 | "SELECT \n", 435 | " p.maker, \n", 436 | " AVG(l.screen) \n", 437 | "FROM \n", 438 | " product AS p \n", 439 | " JOIN laptop AS l ON p.model = l.model \n", 440 | "GROUP BY \n", 441 | " p.maker;" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": null, 447 | "metadata": {}, 448 | "outputs": [], 449 | "source": [ 450 | "# Task 20\n", 451 | "# Find the makers producing at least three distinct models of PCs.\n", 452 | "# Result set: maker, number of PC models.\n", 453 | "SELECT \n", 454 | " maker, \n", 455 | " COUNT(model) AS Count_Model \n", 456 | "FROM \n", 457 | " product \n", 458 | "WHERE \n", 459 | " type = 'pc' \n", 460 | "GROUP BY \n", 461 | " maker \n", 462 | "HAVING \n", 463 | " COUNT(DISTINCT model) >= 3;" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": null, 469 | "metadata": {}, 470 | "outputs": [], 471 | "source": [ 472 | "# Task 21\n", 473 | "# Find out the maximum PC price for each maker having models in the PC table. \n", 474 | "# Result set: maker, maximum price. \n", 475 | "SELECT \n", 476 | " maker, \n", 477 | " MAX(price) \n", 478 | "FROM \n", 479 | " pc \n", 480 | " JOIN product ON pc.model = product.model \n", 481 | "GROUP BY \n", 482 | " maker;" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": null, 488 | "metadata": {}, 489 | "outputs": [], 490 | "source": [ 491 | "# Task 22\n", 492 | "# For each value of PC speed that exceeds 600 MHz, find out the average price of PCs \n", 493 | "# with identical speeds.\n", 494 | "# Result set: speed, average price. \n", 495 | "SELECT \n", 496 | " speed, \n", 497 | " AVG(price) \n", 498 | "FROM \n", 499 | " pc \n", 500 | "GROUP BY \n", 501 | " speed \n", 502 | "HAVING \n", 503 | " speed > 600;" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": null, 509 | "metadata": {}, 510 | "outputs": [], 511 | "source": [ 512 | "# Task 23\n", 513 | "# Get the makers producing both PCs having a speed of 750 MHz or higher \n", 514 | "# and laptops with a speed of 750 MHz or higher.\n", 515 | "# Result set: maker \n", 516 | "SELECT \n", 517 | " DISTINCT maker \n", 518 | "FROM \n", 519 | " pc \n", 520 | " JOIN product ON pc.model = product.model \n", 521 | "WHERE \n", 522 | " pc.speed >= 750 \n", 523 | " AND maker IN (\n", 524 | " SELECT \n", 525 | " maker \n", 526 | " FROM \n", 527 | " laptop \n", 528 | " JOIN product ON laptop.model = product.model \n", 529 | " WHERE \n", 530 | " laptop.speed >= 750\n", 531 | " );" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": null, 537 | "metadata": {}, 538 | "outputs": [], 539 | "source": [ 540 | "# Task 24\n", 541 | "tbc.." 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": null, 547 | "metadata": {}, 548 | "outputs": [], 549 | "source": [] 550 | } 551 | ], 552 | "metadata": { 553 | "kernelspec": { 554 | "display_name": "Python 3", 555 | "language": "python", 556 | "name": "python3" 557 | }, 558 | "language_info": { 559 | "codemirror_mode": { 560 | "name": "ipython", 561 | "version": 3 562 | }, 563 | "file_extension": ".py", 564 | "mimetype": "text/x-python", 565 | "name": "python", 566 | "nbconvert_exporter": "python", 567 | "pygments_lexer": "ipython3", 568 | "version": "3.7.6" 569 | } 570 | }, 571 | "nbformat": 4, 572 | "nbformat_minor": 4 573 | } 574 | -------------------------------------------------------------------------------- /3_sql_course_stepik/.ipynb_checkpoints/sql_course_stepik-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "ИНТЕРАКТИВНЫЙ ТРЕНАЖЕР ПО SQL (stepik.org) \n", 8 | "(Дальневосточный федеральный университет)\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "1. Основы Реляционной Модели и SQL" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "# LESSON 1. Отношение (таблица)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "# Типы данных:\n", 34 | "# INTEGER (INT) -- Целое число, могут принимать значения от -2147483648 до 2147483647\n", 35 | "\n", 36 | "# DECIMAL (4, 1) -- Вещественное число, в скобках указывается максимальная\n", 37 | "# длина числа (включает символы слева и справа от десятичной запятой) \n", 38 | "# и количество знаков после запятой. Принимают значения в диапазоне -10^38+1 до 10^38-1.\n", 39 | "\n", 40 | "# NUMERIC(6,3) -- аналогично DECIMAL\n", 41 | "\n", 42 | "# DATE -- дата в формате YYYY-MM-DD\n", 43 | "\n", 44 | "# VARCHAR(20) -- Строка длиной не более 255 символов, в скобках указывается максимальная \n", 45 | "# длина строки" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "# Для описания ключевого поля используйте описание:\n", 55 | "INT PRIMARY KEY AUTO_INCREMENT" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "# Task 1\n", 65 | "# Сформулируйте SQL запрос для создания таблицы book\n", 66 | "CREATE TABLE book (\n", 67 | " book_id INT PRIMARY KEY AUTO_INCREMENT, \n", 68 | " title VARCHAR(50), \n", 69 | " author VARCHAR(30), \n", 70 | " price DECIMAL(8, 2), \n", 71 | " amount INT\n", 72 | ");" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "# to comment a block of code in SQL use: /* some text */\n", 82 | "# to comment a block of code till the end of the line in SQL use: -- some text" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "# Task 2\n", 92 | "# Занесите новую строку в таблицу book \n", 93 | "INSERT INTO book (title, author, price, amount) \n", 94 | "VALUES \n", 95 | " (\n", 96 | " \"Мастер и Маргарита\", \n", 97 | " \"Булгаков М.А.\", \n", 98 | " 670.99, \n", 99 | " 3\n", 100 | " );" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "# Task 3\n", 110 | "# Занесите еще три записи в таблицу book. Каждая строка вставляется отдельным SQL \n", 111 | "# запросом, запросы обязательно разделять точкой с запятой\n", 112 | "INSERT INTO book (title, author, price, amount) \n", 113 | "VALUES ('Белая гвардия', 'Булгаков М.А.', 540.50, 5);\n", 114 | " \n", 115 | "INSERT INTO book (title, author, price, amount) \n", 116 | "VALUES ('Идиот', 'Достоевский Ф.М.', 460.00, 10);\n", 117 | " \n", 118 | "INSERT INTO book (title, author, price, amount) \n", 119 | "VALUES ('Братья Карамазовы', 'Достоевский Ф.М.', 799.01, 2);" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "# LESSON 2. Выборка данных" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "# В окне кода можно использовать комментарии для сохранения разных \n", 145 | "# вариантов запросов или пояснений. Комментарии заключаются в /* и */" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "# Task 1\n", 155 | "# Вывести информацию о всех книгах, хранящихся на складе. \n", 156 | "SELECT \n", 157 | " * \n", 158 | "FROM \n", 159 | " book;" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "# Task 2\n", 169 | "# Выбрать авторов, название книг и их цену из таблицы book.\n", 170 | "SELECT \n", 171 | " author, \n", 172 | " title, \n", 173 | " price \n", 174 | "FROM \n", 175 | " book;" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "# Task 3\n", 185 | "# Выбрать названия книг и авторов из таблицы book, \n", 186 | "# для поля title задать новое имя Название, для поля author – Автор.\n", 187 | "SELECT \n", 188 | " title AS 'Название', \n", 189 | " author AS 'Автор' \n", 190 | "FROM \n", 191 | " book;" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": null, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "# Task 4\n", 201 | "# Для упаковки каждой книги требуется 1 лист бумаги, \n", 202 | "# цена которого 1 рубль 65 копеек. \n", 203 | "# Посчитать стоимость упаковки для каждой книги \n", 204 | "# (сколько денег потребуется, чтобы упаковать все экземпляры книги). \n", 205 | "# В запросе вывести название книги, ее количество и стоимость упаковки, \n", 206 | "# последний столбец назвать pack.\n", 207 | "SELECT \n", 208 | " title, \n", 209 | " amount, \n", 210 | " amount * 1.65 AS pack \n", 211 | "FROM \n", 212 | " book;" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "metadata": {}, 219 | "outputs": [], 220 | "source": [ 221 | "# В SQL реализовано множество математических функций для работы \n", 222 | "# с числовыми данными.Некоторые из них.\n", 223 | "\n", 224 | "# CEILING(x) -- возвращает наименьшее число, большее или равное Х\n", 225 | "\n", 226 | "# ROUND(x, k) -- округляет значение Х до К знаков после запятой,\n", 227 | "# если К не указано, Х округляется до целого\n", 228 | "\n", 229 | "# FLOOR(x) -- возвращает наибольшее целое число, меньшее или равное Х\n", 230 | "# floor(4.2)=4; floor(-5.8)=-6\n", 231 | "\n", 232 | "# POWER(x, y) -- возведение Х в степень У\n", 233 | "\n", 234 | "# SQRT(X) -- квадратный корень из Х\n", 235 | "\n", 236 | "# DEGREES(x) -- конвертирует значение Х из радианы в градусы\n", 237 | "\n", 238 | "# RADIANS(x) -- конвертирует значение Х из градусов в радианы\n", 239 | "\n", 240 | "# ABS(x) -- модуль числа Х\n", 241 | "\n", 242 | "# PI() -- число pi = 3.1415926.." 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": null, 248 | "metadata": {}, 249 | "outputs": [], 250 | "source": [ 251 | "# Task 5\n", 252 | "# В конце года цену всех книг на складе пересчитывают – снижают ее на 30%. \n", 253 | "# Написать SQL запрос, который из таблицы book выбирает \n", 254 | "# названия, авторов, количества и вычисляет новые цены книг. \n", 255 | "# Столбец с новой ценой назвать new_price, цену округлить до 2-х знаков \n", 256 | "# после запятой.\n", 257 | "SELECT \n", 258 | " title, \n", 259 | " author, \n", 260 | " amount, \n", 261 | " ROUND(price * 0.7, 2) AS new_price \n", 262 | "FROM \n", 263 | " book;" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [ 272 | "# В SQL реализована возможность заносить в поле значение \n", 273 | "# в зависимости от условия:\n", 274 | "# IF(логическое_выражение, выражение_1, выражение_2)\n", 275 | "\n", 276 | "# Допускается использование вложенных функций, вместо выражения_1 \n", 277 | "# или выражения_2 может стоять новая функция IF." 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [ 286 | "# Example:\n", 287 | "# Вычисление скидки в зависимости от количества книг. \n", 288 | "# Если количество книг меньше 4 – то скидка 50%, меньше 11 – 30%, \n", 289 | "# в остальных случаях – 10%. \n", 290 | "# И еще укажем какая именно скидка на каждую книгу.\n", 291 | "SELECT \n", 292 | " title, \n", 293 | " amount, \n", 294 | " price, \n", 295 | " round(\n", 296 | " IF(\n", 297 | " amount < 4, \n", 298 | " price * 0.5, \n", 299 | " IF(amount < 11, price * 0.7, price * 0.9)\n", 300 | " ), \n", 301 | " 2\n", 302 | " ) AS sale, \n", 303 | " IF(\n", 304 | " amount < 4, \n", 305 | " 'скидка 50%', \n", 306 | " IF(\n", 307 | " amount < 11, 'скидка 30%', 'скидка 10%'\n", 308 | " )\n", 309 | " ) AS Ваша_скидка \n", 310 | "FROM \n", 311 | " book;" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [ 320 | "# Task 6\n", 321 | "# При анализе продаж книг выяснилось, что наибольшей популярностью пользуются\n", 322 | "# книги Михаила Булгакова, на втором месте книги Сергея Есенина.\n", 323 | "# Исходя из этого решили поднять цену книг Булгакова на 10%, \n", 324 | "# а цену книг Есенина - на 5%. Написать запрос, куда включить автора, \n", 325 | "# название книги и новую цену, последний столбец назвать new_price. \n", 326 | "# Значение округлить до двух знаков после запятой.\n", 327 | "SELECT \n", 328 | " author, \n", 329 | " title, \n", 330 | " round(\n", 331 | " IF(\n", 332 | " author = 'Булгаков М.А.', \n", 333 | " price * 1.1, \n", 334 | " IF(\n", 335 | " author = 'Есенин С.А.', price * 1.05, \n", 336 | " price\n", 337 | " )\n", 338 | " ), \n", 339 | " 2\n", 340 | " ) AS new_price \n", 341 | "FROM \n", 342 | " book;" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "# WHERE\n", 352 | "# Логическое выражение может включать операторы сравнения:\n", 353 | "# равно «=», \n", 354 | "# не равно «<>», \n", 355 | "# больше «>», \n", 356 | "# меньше «<», \n", 357 | "# больше или равно«>=», \n", 358 | "# меньше или равно «<=») \n", 359 | "# и выражения, допустимые в SQL.\n", 360 | "\n", 361 | "# В логическом выражении после WHERE нельзя использовать названия столбцов,\n", 362 | "# присвоенные им с помощью AS" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": null, 368 | "metadata": {}, 369 | "outputs": [], 370 | "source": [ 371 | "# Task 7\n", 372 | "# Вывести автора, название и цены тех книг, количество которых меньше 10.\n", 373 | "SELECT \n", 374 | " author, \n", 375 | " title, \n", 376 | " price \n", 377 | "FROM \n", 378 | " book \n", 379 | "WHERE \n", 380 | " amount < 10;" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": {}, 387 | "outputs": [], 388 | "source": [ 389 | "# Логическое выражение после ключевого слова WHERE \n", 390 | "# кроме операторов сравненияи выражений может включать логические операции \n", 391 | "# (И «and», ИЛИ «or», НЕ «not») и круглые скобки, \n", 392 | "# изменяющие приоритеты выполнения операций." 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": null, 398 | "metadata": {}, 399 | "outputs": [], 400 | "source": [ 401 | "# Приоритеты операций:\n", 402 | "# -- круглые скобки\n", 403 | "# -- умножение (*), деление (/)\n", 404 | "# -- сложение (+), вычитание (-)\n", 405 | "# -- операторы сравнения (=, >, <, >=, <=, <>)\n", 406 | "# -- NOT\n", 407 | "# -- AND\n", 408 | "# -- OR" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": null, 414 | "metadata": {}, 415 | "outputs": [], 416 | "source": [ 417 | "# Task 8\n", 418 | "# Вывести название, автора, цену и количество всех книг, \n", 419 | "# цена которых меньше 500 или больше 600, \n", 420 | "# а стоимость всех экземпляров этих книг больше или равна 5000.\n", 421 | "SELECT \n", 422 | " title, \n", 423 | " author, \n", 424 | " price, \n", 425 | " amount \n", 426 | "FROM \n", 427 | " book \n", 428 | "WHERE \n", 429 | " (\n", 430 | " price < 500 \n", 431 | " OR price > 600\n", 432 | " ) \n", 433 | " AND price * amount >= 5000;" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": null, 439 | "metadata": {}, 440 | "outputs": [], 441 | "source": [ 442 | "# Логическое выражение после ключевого слова WHERE может включать операторы\n", 443 | "# BETWEEN и IN. Эти операторы имеют самый низкий приоритет (как OR).\n", 444 | "\n", 445 | "# Example (BETWEEN - интервал включая его границы):\n", 446 | "SELECT title, amount \n", 447 | "FROM book\n", 448 | "WHERE amount BETWEEN 5 AND 14;\n", 449 | "\n", 450 | "# Example (IN):\n", 451 | "SELECT title, price \n", 452 | "FROM book\n", 453 | "WHERE author IN ('Булгаков М.А.', 'Достоевский Ф.М.');" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": null, 459 | "metadata": {}, 460 | "outputs": [], 461 | "source": [ 462 | "# Task 9\n", 463 | "# Вывести название и авторов тех книг, \n", 464 | "# цены которых принадлежат интервалу от 540.50 до 800 (включая границы), \n", 465 | "# а количество или 2, или 3, или 5, или 7.\n", 466 | "SELECT \n", 467 | " title, \n", 468 | " author \n", 469 | "FROM \n", 470 | " book \n", 471 | "WHERE \n", 472 | " (\n", 473 | " price BETWEEN 540.50 \n", 474 | " AND 800\n", 475 | " ) \n", 476 | " AND amount IN (2, 3, 5, 7);" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": null, 482 | "metadata": {}, 483 | "outputs": [], 484 | "source": [ 485 | "# Оператор LIKE используется для сравнения строк. \n", 486 | "# В отличие от операторов отношения равно (=) и не равно (<>), \n", 487 | "# LIKE позволяет сравнивать строки не на полное совпадение (не совпадение), \n", 488 | "# а в соответствии с шаблоном. \n", 489 | "# Шаблон может включать обычные символы и символы-шаблоны:\n", 490 | "# % -- Любая строка, содержащая ноль или более символов\n", 491 | "# _ -- Любой одиночный символ\n", 492 | "\n", 493 | "# Example:\n", 494 | "# выполняет поиск и выдает все книги, инициалы авторов которых содержат «М.»\n", 495 | "SELECT * FROM book WHERE author LIKE '%М.%'; \n", 496 | "\n", 497 | "# Example:\n", 498 | "# выполняет выдает книги, названия которых либо «Поэма», либо «Поэмы» и пр.\n", 499 | "SELECT * FROM book WHERE title LIKE 'Поэм_'; \n", 500 | "\n", 501 | "# Example:\n", 502 | "# Вывести название книг, состоящих ровно из 5 букв.\n", 503 | "SELECT title FROM book \n", 504 | "WHERE title LIKE \"_____\"\n", 505 | "\n", 506 | "# Example:\n", 507 | "# Вывести книги, название которых длиннее 5 символов:\n", 508 | "SELECT title FROM book \n", 509 | "WHERE title LIKE \"______%\";\n", 510 | "# эквивалентные условия \n", 511 | "# title LIKE \"%______\"\n", 512 | "# title LIKE \"%______%\"\n", 513 | "\n", 514 | "# Example:\n", 515 | "# Вывести названия книг, которые состоят ровно из одного слова, \n", 516 | "# если считать, что слова в названии отделяются друг от друга пробелами .\n", 517 | "SELECT title FROM book \n", 518 | "WHERE title NOT LIKE \"% %\"; \n", 519 | "\n", 520 | "\n", 521 | "# NB! Строчные и прописные буквы в строках эквивалентны." 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": null, 527 | "metadata": {}, 528 | "outputs": [], 529 | "source": [ 530 | "# Task 10\n", 531 | "# Вывести название и автора тех книг, название которых состоит из двух и более\n", 532 | "# слов, а инициалы автора содержат букву «С». \n", 533 | "# Считать, что в названии слова отделяются друг от друга пробелами и \n", 534 | "# не содержат знаков препинания, между фамилией автора и инициалами \n", 535 | "# обязателен пробел, инициалы записываются без пробела в формате: \n", 536 | "# буква, точка, буква, точка.\n", 537 | "SELECT \n", 538 | " title, \n", 539 | " author \n", 540 | "FROM \n", 541 | " book \n", 542 | "WHERE \n", 543 | " title LIKE '_% _%' \n", 544 | " AND author LIKE '%С.%';" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": null, 550 | "metadata": {}, 551 | "outputs": [], 552 | "source": [ 553 | "# При выборке можно указывать столбец или несколько столбцов, \n", 554 | "# по которым необходимо отсортировать отобранные строки. \n", 555 | "# Для этого используются ключевые слова ORDER BY, \n", 556 | "# после которых задаются имена столбцов. \n", 557 | "\n", 558 | "# По умолчанию ORDER BY выполняет сортировку по возрастанию. \n", 559 | "# Чтобы управлять направлением сортировки вручную, \n", 560 | "# после имени столбца указывается ключевое слово ASC (по возрастанию) \n", 561 | "# или DESC (по убыванию). \n", 562 | "\n", 563 | "# Example\n", 564 | "# Вывести автора, название и количество книг, \n", 565 | "# в отсортированном в алфавитном порядке по автору и по убыванию количества,\n", 566 | "# для тех книг, цены которых меньше 750 рублей.\n", 567 | "SELECT author, title, amount AS Количество\n", 568 | "FROM book\n", 569 | "WHERE price < 750\n", 570 | "ORDER BY author, amount DESC;" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": null, 576 | "metadata": {}, 577 | "outputs": [], 578 | "source": [ 579 | "# Task 11\n", 580 | "# Вывести автора и название книг, количество которых принадлежит \n", 581 | "# интервалу от 2 до 14 (включая границы). \n", 582 | "# Информацию отсортировать по авторам (в обратном алфавитном порядке) \n", 583 | "# и названиям (по алфавиту).\n", 584 | "SELECT \n", 585 | " author, \n", 586 | " title \n", 587 | "FROM \n", 588 | " book \n", 589 | "WHERE \n", 590 | " amount BETWEEN 2 \n", 591 | " AND 14 \n", 592 | "ORDER BY \n", 593 | " author DESC, \n", 594 | " title;" 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": null, 600 | "metadata": {}, 601 | "outputs": [], 602 | "source": [ 603 | "# Task 12\n", 604 | "# Your own query\n", 605 | "SELECT \n", 606 | " author, \n", 607 | " title, \n", 608 | " price * amount AS inventory_price \n", 609 | "FROM \n", 610 | " book \n", 611 | "WHERE \n", 612 | " price * amount BETWEEN 2000 \n", 613 | " AND 4000 \n", 614 | "ORDER BY \n", 615 | " inventory_price DESC;" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": null, 621 | "metadata": {}, 622 | "outputs": [], 623 | "source": [] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": null, 628 | "metadata": {}, 629 | "outputs": [], 630 | "source": [ 631 | "# LESSON 3. Запросы, групповые операции" 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "execution_count": null, 637 | "metadata": {}, 638 | "outputs": [], 639 | "source": [ 640 | "# Task 1" 641 | ] 642 | }, 643 | { 644 | "cell_type": "code", 645 | "execution_count": null, 646 | "metadata": {}, 647 | "outputs": [], 648 | "source": [] 649 | }, 650 | { 651 | "cell_type": "code", 652 | "execution_count": null, 653 | "metadata": {}, 654 | "outputs": [], 655 | "source": [] 656 | }, 657 | { 658 | "cell_type": "code", 659 | "execution_count": null, 660 | "metadata": {}, 661 | "outputs": [], 662 | "source": [] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": null, 667 | "metadata": {}, 668 | "outputs": [], 669 | "source": [] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "metadata": {}, 675 | "outputs": [], 676 | "source": [] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "execution_count": null, 681 | "metadata": {}, 682 | "outputs": [], 683 | "source": [] 684 | } 685 | ], 686 | "metadata": { 687 | "kernelspec": { 688 | "display_name": "Python 3", 689 | "language": "python", 690 | "name": "python3" 691 | }, 692 | "language_info": { 693 | "codemirror_mode": { 694 | "name": "ipython", 695 | "version": 3 696 | }, 697 | "file_extension": ".py", 698 | "mimetype": "text/x-python", 699 | "name": "python", 700 | "nbconvert_exporter": "python", 701 | "pygments_lexer": "ipython3", 702 | "version": "3.7.6" 703 | } 704 | }, 705 | "nbformat": 4, 706 | "nbformat_minor": 4 707 | } 708 | -------------------------------------------------------------------------------- /3_sql_course_stepik/sql_course_stepik.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "ИНТЕРАКТИВНЫЙ ТРЕНАЖЕР ПО SQL (stepik.org) \n", 8 | "(Дальневосточный федеральный университет)\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "1. Основы Реляционной Модели и SQL" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "# LESSON 1. Отношение (таблица)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "# Типы данных:\n", 34 | "# INTEGER (INT) -- Целое число, могут принимать значения от -2147483648 до 2147483647\n", 35 | "\n", 36 | "# DECIMAL (4, 1) -- Вещественное число, в скобках указывается максимальная\n", 37 | "# длина числа (включает символы слева и справа от десятичной запятой) \n", 38 | "# и количество знаков после запятой. Принимают значения в диапазоне -10^38+1 до 10^38-1.\n", 39 | "\n", 40 | "# NUMERIC(6,3) -- аналогично DECIMAL\n", 41 | "\n", 42 | "# DATE -- дата в формате YYYY-MM-DD\n", 43 | "\n", 44 | "# VARCHAR(20) -- Строка длиной не более 255 символов, в скобках указывается максимальная \n", 45 | "# длина строки" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "# Для описания ключевого поля используйте описание:\n", 55 | "INT PRIMARY KEY AUTO_INCREMENT" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "# Task 1\n", 65 | "# Сформулируйте SQL запрос для создания таблицы book\n", 66 | "CREATE TABLE book (\n", 67 | " book_id INT PRIMARY KEY AUTO_INCREMENT, \n", 68 | " title VARCHAR(50), \n", 69 | " author VARCHAR(30), \n", 70 | " price DECIMAL(8, 2), \n", 71 | " amount INT\n", 72 | ");" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "# to comment a block of code in SQL use: /* some text */\n", 82 | "# to comment a block of code till the end of the line in SQL use: -- some text" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "# Task 2\n", 92 | "# Занесите новую строку в таблицу book \n", 93 | "INSERT INTO book (title, author, price, amount) \n", 94 | "VALUES \n", 95 | " (\n", 96 | " \"Мастер и Маргарита\", \n", 97 | " \"Булгаков М.А.\", \n", 98 | " 670.99, \n", 99 | " 3\n", 100 | " );" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "# Task 3\n", 110 | "# Занесите еще три записи в таблицу book. Каждая строка вставляется отдельным SQL \n", 111 | "# запросом, запросы обязательно разделять точкой с запятой\n", 112 | "INSERT INTO book (title, author, price, amount) \n", 113 | "VALUES ('Белая гвардия', 'Булгаков М.А.', 540.50, 5);\n", 114 | " \n", 115 | "INSERT INTO book (title, author, price, amount) \n", 116 | "VALUES ('Идиот', 'Достоевский Ф.М.', 460.00, 10);\n", 117 | " \n", 118 | "INSERT INTO book (title, author, price, amount) \n", 119 | "VALUES ('Братья Карамазовы', 'Достоевский Ф.М.', 799.01, 2);" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "# LESSON 2. Выборка данных" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "# В окне кода можно использовать комментарии для сохранения разных \n", 145 | "# вариантов запросов или пояснений. Комментарии заключаются в /* и */" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "# Task 1\n", 155 | "# Вывести информацию о всех книгах, хранящихся на складе. \n", 156 | "SELECT \n", 157 | " * \n", 158 | "FROM \n", 159 | " book;" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "# Task 2\n", 169 | "# Выбрать авторов, название книг и их цену из таблицы book.\n", 170 | "SELECT \n", 171 | " author, \n", 172 | " title, \n", 173 | " price \n", 174 | "FROM \n", 175 | " book;" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "# Task 3\n", 185 | "# Выбрать названия книг и авторов из таблицы book, \n", 186 | "# для поля title задать новое имя Название, для поля author – Автор.\n", 187 | "SELECT \n", 188 | " title AS 'Название', \n", 189 | " author AS 'Автор' \n", 190 | "FROM \n", 191 | " book;" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": null, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "# Task 4\n", 201 | "# Для упаковки каждой книги требуется 1 лист бумаги, \n", 202 | "# цена которого 1 рубль 65 копеек. \n", 203 | "# Посчитать стоимость упаковки для каждой книги \n", 204 | "# (сколько денег потребуется, чтобы упаковать все экземпляры книги). \n", 205 | "# В запросе вывести название книги, ее количество и стоимость упаковки, \n", 206 | "# последний столбец назвать pack.\n", 207 | "SELECT \n", 208 | " title, \n", 209 | " amount, \n", 210 | " amount * 1.65 AS pack \n", 211 | "FROM \n", 212 | " book;" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "metadata": {}, 219 | "outputs": [], 220 | "source": [ 221 | "# В SQL реализовано множество математических функций для работы \n", 222 | "# с числовыми данными.Некоторые из них.\n", 223 | "\n", 224 | "# CEILING(x) -- возвращает наименьшее число, большее или равное Х\n", 225 | "\n", 226 | "# ROUND(x, k) -- округляет значение Х до К знаков после запятой,\n", 227 | "# если К не указано, Х округляется до целого\n", 228 | "\n", 229 | "# FLOOR(x) -- возвращает наибольшее целое число, меньшее или равное Х\n", 230 | "# floor(4.2)=4; floor(-5.8)=-6\n", 231 | "\n", 232 | "# POWER(x, y) -- возведение Х в степень У\n", 233 | "\n", 234 | "# SQRT(X) -- квадратный корень из Х\n", 235 | "\n", 236 | "# DEGREES(x) -- конвертирует значение Х из радианы в градусы\n", 237 | "\n", 238 | "# RADIANS(x) -- конвертирует значение Х из градусов в радианы\n", 239 | "\n", 240 | "# ABS(x) -- модуль числа Х\n", 241 | "\n", 242 | "# PI() -- число pi = 3.1415926.." 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": null, 248 | "metadata": {}, 249 | "outputs": [], 250 | "source": [ 251 | "# Task 5\n", 252 | "# В конце года цену всех книг на складе пересчитывают – снижают ее на 30%. \n", 253 | "# Написать SQL запрос, который из таблицы book выбирает \n", 254 | "# названия, авторов, количества и вычисляет новые цены книг. \n", 255 | "# Столбец с новой ценой назвать new_price, цену округлить до 2-х знаков \n", 256 | "# после запятой.\n", 257 | "SELECT \n", 258 | " title, \n", 259 | " author, \n", 260 | " amount, \n", 261 | " ROUND(price * 0.7, 2) AS new_price \n", 262 | "FROM \n", 263 | " book;" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [ 272 | "# В SQL реализована возможность заносить в поле значение \n", 273 | "# в зависимости от условия:\n", 274 | "# IF(логическое_выражение, выражение_1, выражение_2)\n", 275 | "\n", 276 | "# Допускается использование вложенных функций, вместо выражения_1 \n", 277 | "# или выражения_2 может стоять новая функция IF." 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [ 286 | "# Example:\n", 287 | "# Вычисление скидки в зависимости от количества книг. \n", 288 | "# Если количество книг меньше 4 – то скидка 50%, меньше 11 – 30%, \n", 289 | "# в остальных случаях – 10%. \n", 290 | "# И еще укажем какая именно скидка на каждую книгу.\n", 291 | "SELECT \n", 292 | " title, \n", 293 | " amount, \n", 294 | " price, \n", 295 | " round(\n", 296 | " IF(\n", 297 | " amount < 4, \n", 298 | " price * 0.5, \n", 299 | " IF(amount < 11, price * 0.7, price * 0.9)\n", 300 | " ), \n", 301 | " 2\n", 302 | " ) AS sale, \n", 303 | " IF(\n", 304 | " amount < 4, \n", 305 | " 'скидка 50%', \n", 306 | " IF(\n", 307 | " amount < 11, 'скидка 30%', 'скидка 10%'\n", 308 | " )\n", 309 | " ) AS Ваша_скидка \n", 310 | "FROM \n", 311 | " book;" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [ 320 | "# Task 6\n", 321 | "# При анализе продаж книг выяснилось, что наибольшей популярностью пользуются\n", 322 | "# книги Михаила Булгакова, на втором месте книги Сергея Есенина.\n", 323 | "# Исходя из этого решили поднять цену книг Булгакова на 10%, \n", 324 | "# а цену книг Есенина - на 5%. Написать запрос, куда включить автора, \n", 325 | "# название книги и новую цену, последний столбец назвать new_price. \n", 326 | "# Значение округлить до двух знаков после запятой.\n", 327 | "SELECT \n", 328 | " author, \n", 329 | " title, \n", 330 | " round(\n", 331 | " IF(\n", 332 | " author = 'Булгаков М.А.', \n", 333 | " price * 1.1, \n", 334 | " IF(\n", 335 | " author = 'Есенин С.А.', price * 1.05, \n", 336 | " price\n", 337 | " )\n", 338 | " ), \n", 339 | " 2\n", 340 | " ) AS new_price \n", 341 | "FROM \n", 342 | " book;" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "# WHERE\n", 352 | "# Логическое выражение может включать операторы сравнения:\n", 353 | "# равно «=», \n", 354 | "# не равно «<>», \n", 355 | "# больше «>», \n", 356 | "# меньше «<», \n", 357 | "# больше или равно«>=», \n", 358 | "# меньше или равно «<=») \n", 359 | "# и выражения, допустимые в SQL.\n", 360 | "\n", 361 | "# В логическом выражении после WHERE нельзя использовать названия столбцов,\n", 362 | "# присвоенные им с помощью AS" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": null, 368 | "metadata": {}, 369 | "outputs": [], 370 | "source": [ 371 | "# Task 7\n", 372 | "# Вывести автора, название и цены тех книг, количество которых меньше 10.\n", 373 | "SELECT \n", 374 | " author, \n", 375 | " title, \n", 376 | " price \n", 377 | "FROM \n", 378 | " book \n", 379 | "WHERE \n", 380 | " amount < 10;" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": {}, 387 | "outputs": [], 388 | "source": [ 389 | "# Логическое выражение после ключевого слова WHERE \n", 390 | "# кроме операторов сравненияи выражений может включать логические операции \n", 391 | "# (И «and», ИЛИ «or», НЕ «not») и круглые скобки, \n", 392 | "# изменяющие приоритеты выполнения операций." 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": null, 398 | "metadata": {}, 399 | "outputs": [], 400 | "source": [ 401 | "# Приоритеты операций:\n", 402 | "# -- круглые скобки\n", 403 | "# -- умножение (*), деление (/)\n", 404 | "# -- сложение (+), вычитание (-)\n", 405 | "# -- операторы сравнения (=, >, <, >=, <=, <>)\n", 406 | "# -- NOT\n", 407 | "# -- AND\n", 408 | "# -- OR" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": null, 414 | "metadata": {}, 415 | "outputs": [], 416 | "source": [ 417 | "# Task 8\n", 418 | "# Вывести название, автора, цену и количество всех книг, \n", 419 | "# цена которых меньше 500 или больше 600, \n", 420 | "# а стоимость всех экземпляров этих книг больше или равна 5000.\n", 421 | "SELECT \n", 422 | " title, \n", 423 | " author, \n", 424 | " price, \n", 425 | " amount \n", 426 | "FROM \n", 427 | " book \n", 428 | "WHERE \n", 429 | " (\n", 430 | " price < 500 \n", 431 | " OR price > 600\n", 432 | " ) \n", 433 | " AND price * amount >= 5000;" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": null, 439 | "metadata": {}, 440 | "outputs": [], 441 | "source": [ 442 | "# Логическое выражение после ключевого слова WHERE может включать операторы\n", 443 | "# BETWEEN и IN. Эти операторы имеют самый низкий приоритет (как OR).\n", 444 | "\n", 445 | "# Example (BETWEEN - интервал включая его границы):\n", 446 | "SELECT title, amount \n", 447 | "FROM book\n", 448 | "WHERE amount BETWEEN 5 AND 14;\n", 449 | "\n", 450 | "# Example (IN):\n", 451 | "SELECT title, price \n", 452 | "FROM book\n", 453 | "WHERE author IN ('Булгаков М.А.', 'Достоевский Ф.М.');" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": null, 459 | "metadata": {}, 460 | "outputs": [], 461 | "source": [ 462 | "# Task 9\n", 463 | "# Вывести название и авторов тех книг, \n", 464 | "# цены которых принадлежат интервалу от 540.50 до 800 (включая границы), \n", 465 | "# а количество или 2, или 3, или 5, или 7.\n", 466 | "SELECT \n", 467 | " title, \n", 468 | " author \n", 469 | "FROM \n", 470 | " book \n", 471 | "WHERE \n", 472 | " (\n", 473 | " price BETWEEN 540.50 \n", 474 | " AND 800\n", 475 | " ) \n", 476 | " AND amount IN (2, 3, 5, 7);" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": null, 482 | "metadata": {}, 483 | "outputs": [], 484 | "source": [ 485 | "# Оператор LIKE используется для сравнения строк. \n", 486 | "# В отличие от операторов отношения равно (=) и не равно (<>), \n", 487 | "# LIKE позволяет сравнивать строки не на полное совпадение (не совпадение), \n", 488 | "# а в соответствии с шаблоном. \n", 489 | "# Шаблон может включать обычные символы и символы-шаблоны:\n", 490 | "# % -- Любая строка, содержащая ноль или более символов\n", 491 | "# _ -- Любой одиночный символ\n", 492 | "\n", 493 | "# Example:\n", 494 | "# выполняет поиск и выдает все книги, инициалы авторов которых содержат «М.»\n", 495 | "SELECT * FROM book WHERE author LIKE '%М.%'; \n", 496 | "\n", 497 | "# Example:\n", 498 | "# выполняет выдает книги, названия которых либо «Поэма», либо «Поэмы» и пр.\n", 499 | "SELECT * FROM book WHERE title LIKE 'Поэм_'; \n", 500 | "\n", 501 | "# Example:\n", 502 | "# Вывести название книг, состоящих ровно из 5 букв.\n", 503 | "SELECT title FROM book \n", 504 | "WHERE title LIKE \"_____\"\n", 505 | "\n", 506 | "# Example:\n", 507 | "# Вывести книги, название которых длиннее 5 символов:\n", 508 | "SELECT title FROM book \n", 509 | "WHERE title LIKE \"______%\";\n", 510 | "# эквивалентные условия \n", 511 | "# title LIKE \"%______\"\n", 512 | "# title LIKE \"%______%\"\n", 513 | "\n", 514 | "# Example:\n", 515 | "# Вывести названия книг, которые состоят ровно из одного слова, \n", 516 | "# если считать, что слова в названии отделяются друг от друга пробелами .\n", 517 | "SELECT title FROM book \n", 518 | "WHERE title NOT LIKE \"% %\"; \n", 519 | "\n", 520 | "\n", 521 | "# NB! Строчные и прописные буквы в строках эквивалентны." 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": null, 527 | "metadata": {}, 528 | "outputs": [], 529 | "source": [ 530 | "# Task 10\n", 531 | "# Вывести название и автора тех книг, название которых состоит из двух и более\n", 532 | "# слов, а инициалы автора содержат букву «С». \n", 533 | "# Считать, что в названии слова отделяются друг от друга пробелами и \n", 534 | "# не содержат знаков препинания, между фамилией автора и инициалами \n", 535 | "# обязателен пробел, инициалы записываются без пробела в формате: \n", 536 | "# буква, точка, буква, точка.\n", 537 | "SELECT \n", 538 | " title, \n", 539 | " author \n", 540 | "FROM \n", 541 | " book \n", 542 | "WHERE \n", 543 | " title LIKE '_% _%' \n", 544 | " AND author LIKE '%С.%';" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": null, 550 | "metadata": {}, 551 | "outputs": [], 552 | "source": [ 553 | "# При выборке можно указывать столбец или несколько столбцов, \n", 554 | "# по которым необходимо отсортировать отобранные строки. \n", 555 | "# Для этого используются ключевые слова ORDER BY, \n", 556 | "# после которых задаются имена столбцов. \n", 557 | "\n", 558 | "# По умолчанию ORDER BY выполняет сортировку по возрастанию. \n", 559 | "# Чтобы управлять направлением сортировки вручную, \n", 560 | "# после имени столбца указывается ключевое слово ASC (по возрастанию) \n", 561 | "# или DESC (по убыванию). \n", 562 | "\n", 563 | "# Example\n", 564 | "# Вывести автора, название и количество книг, \n", 565 | "# в отсортированном в алфавитном порядке по автору и по убыванию количества,\n", 566 | "# для тех книг, цены которых меньше 750 рублей.\n", 567 | "SELECT author, title, amount AS Количество\n", 568 | "FROM book\n", 569 | "WHERE price < 750\n", 570 | "ORDER BY author, amount DESC;" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": null, 576 | "metadata": {}, 577 | "outputs": [], 578 | "source": [ 579 | "# Task 11\n", 580 | "# Вывести автора и название книг, количество которых принадлежит \n", 581 | "# интервалу от 2 до 14 (включая границы). \n", 582 | "# Информацию отсортировать по авторам (в обратном алфавитном порядке) \n", 583 | "# и названиям (по алфавиту).\n", 584 | "SELECT \n", 585 | " author, \n", 586 | " title \n", 587 | "FROM \n", 588 | " book \n", 589 | "WHERE \n", 590 | " amount BETWEEN 2 \n", 591 | " AND 14 \n", 592 | "ORDER BY \n", 593 | " author DESC, \n", 594 | " title;" 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": null, 600 | "metadata": {}, 601 | "outputs": [], 602 | "source": [ 603 | "# Task 12\n", 604 | "# Your own query\n", 605 | "SELECT \n", 606 | " author, \n", 607 | " title, \n", 608 | " price * amount AS inventory_price \n", 609 | "FROM \n", 610 | " book \n", 611 | "WHERE \n", 612 | " price * amount BETWEEN 2000 \n", 613 | " AND 4000 \n", 614 | "ORDER BY \n", 615 | " inventory_price DESC;" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": null, 621 | "metadata": {}, 622 | "outputs": [], 623 | "source": [] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": null, 628 | "metadata": {}, 629 | "outputs": [], 630 | "source": [ 631 | "# LESSON 3. Запросы, групповые операции" 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "execution_count": null, 637 | "metadata": {}, 638 | "outputs": [], 639 | "source": [ 640 | "# Task 1" 641 | ] 642 | }, 643 | { 644 | "cell_type": "code", 645 | "execution_count": null, 646 | "metadata": {}, 647 | "outputs": [], 648 | "source": [] 649 | }, 650 | { 651 | "cell_type": "code", 652 | "execution_count": null, 653 | "metadata": {}, 654 | "outputs": [], 655 | "source": [] 656 | }, 657 | { 658 | "cell_type": "code", 659 | "execution_count": null, 660 | "metadata": {}, 661 | "outputs": [], 662 | "source": [] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": null, 667 | "metadata": {}, 668 | "outputs": [], 669 | "source": [] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": null, 674 | "metadata": {}, 675 | "outputs": [], 676 | "source": [] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "execution_count": null, 681 | "metadata": {}, 682 | "outputs": [], 683 | "source": [] 684 | } 685 | ], 686 | "metadata": { 687 | "kernelspec": { 688 | "display_name": "Python 3", 689 | "language": "python", 690 | "name": "python3" 691 | }, 692 | "language_info": { 693 | "codemirror_mode": { 694 | "name": "ipython", 695 | "version": 3 696 | }, 697 | "file_extension": ".py", 698 | "mimetype": "text/x-python", 699 | "name": "python", 700 | "nbconvert_exporter": "python", 701 | "pygments_lexer": "ipython3", 702 | "version": "3.7.6" 703 | } 704 | }, 705 | "nbformat": 4, 706 | "nbformat_minor": 4 707 | } 708 | -------------------------------------------------------------------------------- /5_yandex_praktikum/sql_module.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### SQL Module From Yandex Praktikum Data Analyst Course" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Tasks were solved using **PostgreSQL**" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "###### SELECT" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "# Select columns: id_product, name, category, name_store \n", 31 | "# From: products_data_all\n", 32 | "SELECT \n", 33 | " id_product, \n", 34 | " name, \n", 35 | " category, \n", 36 | " name_store \n", 37 | "FROM \n", 38 | " products_data_all;" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# Retrieve all columns from products_data_all\n", 48 | "SELECT \n", 49 | " * \n", 50 | "FROM \n", 51 | " products_data_all;" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "# Retrieve all columns from transactions\n", 61 | "SELECT \n", 62 | " * \n", 63 | "FROM \n", 64 | " transactions;" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "# Retrieve all columns from weather\n", 74 | "SELECT \n", 75 | " * \n", 76 | "FROM \n", 77 | " weather;" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "###### WHERE" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "# Select data having category 'milk & cream' and update date 2019-06-01\n", 101 | "SELECT \n", 102 | " name, \n", 103 | " price, \n", 104 | " name_store, \n", 105 | " date_upd \n", 106 | "FROM \n", 107 | " products_data_all \n", 108 | "WHERE \n", 109 | " category = 'milk & cream' \n", 110 | " AND date_upd = '2019-06-01';" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "# Select data having category 'milk & cream' and update date 2019-06-08, \n", 120 | "# 2019-06-15, 2019-06-22, 2019-06-29\n", 121 | "SELECT \n", 122 | " name, \n", 123 | " price, \n", 124 | " name_store, \n", 125 | " date_upd \n", 126 | "FROM \n", 127 | " products_data_all \n", 128 | "WHERE \n", 129 | " category = 'milk & cream' \n", 130 | " AND date_upd IN (\n", 131 | " '2019-06-08', '2019-06-15', '2019-06-22', \n", 132 | " '2019-06-29'\n", 133 | " );" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "# Select data having id_product from a provided list and \n", 143 | "# date 2019-06-01 and 2019-06-02\n", 144 | "SELECT \n", 145 | " * \n", 146 | "FROM \n", 147 | " transactions \n", 148 | "WHERE \n", 149 | " date BETWEEN '2019-06-01' \n", 150 | " AND '2019-06-02' \n", 151 | " AND id_product IN (\n", 152 | " 0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 15, \n", 153 | " 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, \n", 154 | " 26, 28, 29, 30, 31, 32, 34, 35, 36, 37, \n", 155 | " 38, 39, 40, 42, 43, 44, 45, 47, 48, 49, \n", 156 | " 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, \n", 157 | " 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, \n", 158 | " 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, \n", 159 | " 82, 83, 84, 86, 88, 89, 90, 91, 92, 93, \n", 160 | " 95, 96, 97, 98, 99, 100, 102, 103, 104, \n", 161 | " 105, 106, 107, 108, 109, 110, 111, 112, \n", 162 | " 113, 114, 115, 116, 118, 119, 5, 14, 27, \n", 163 | " 33, 41, 46, 62, 79, 85, 87, 94, 101, 117\n", 164 | " );" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "###### Aggregate Functions" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [ 187 | "# Count an overall number of rows of the products_data_all table\n", 188 | "SELECT \n", 189 | " COUNT(*) AS cnt \n", 190 | "FROM \n", 191 | " products_data_all;" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": null, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "# Count an overall number of rows, number of values and unque values of\n", 201 | "# the name column in the products_data_all table\n", 202 | "SELECT \n", 203 | " COUNT(*) AS cnt, \n", 204 | " COUNT(name) AS name_cnt, \n", 205 | " COUNT(DISTINCT name) AS name_uniq_cnt \n", 206 | "FROM \n", 207 | " products_data_all;" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": null, 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [ 216 | "# Calculate an average price (products_data_all table) \n", 217 | "SELECT \n", 218 | " AVG(price) AS average \n", 219 | "FROM \n", 220 | " products_data_all;" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [ 229 | "# Calculate an average price of a product 'Мilk 2,5%, 930 ml' \n", 230 | "# in the 'Seventh store'\n", 231 | "SELECT \n", 232 | " AVG(price) AS average \n", 233 | "FROM \n", 234 | " products_data_all \n", 235 | "WHERE \n", 236 | " name = 'Мilk 2,5%, 930 ml' \n", 237 | " AND name_store = 'Seventh store';" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [ 246 | "# Calculate the total worth of all products in the 'Milky' store\n", 247 | "SELECT \n", 248 | " SUM(price) AS summa \n", 249 | "FROM \n", 250 | " products_data_all \n", 251 | "WHERE \n", 252 | " name_store = 'Milky';" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "# Find the max price in the products_data_all table\n", 262 | "SELECT \n", 263 | " MAX(price) AS max_price \n", 264 | "FROM \n", 265 | " products_data_all;" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": null, 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [ 274 | "# Find the difference between the max and min price of a product\n", 275 | "# 'Dairy Butter Ecotavush 99%, 500 g' in the 'TasteMall' store\n", 276 | "SELECT \n", 277 | " MAX(price) - MIN(price) AS max_min_diff \n", 278 | "FROM \n", 279 | " products_data_all \n", 280 | "WHERE \n", 281 | " name = 'Dairy Butter Ecotavush 99%, 500 g' \n", 282 | " AND name_store = 'TasteMall'" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [] 291 | }, 292 | { 293 | "cell_type": "markdown", 294 | "metadata": {}, 295 | "source": [ 296 | "###### Data Types CASTing" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": null, 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [ 305 | "# Calculate an average weight for products weighted in grams\n", 306 | "SELECT \n", 307 | " AVG(\n", 308 | " CAST(weight AS real)\n", 309 | " ) AS average \n", 310 | "FROM \n", 311 | " products_data_all \n", 312 | "WHERE \n", 313 | " units = 'г';" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "# Find the max weight of a product in the category 'milk & cream'\n", 323 | "SELECT \n", 324 | " MAX(\n", 325 | " CAST(weight AS real)\n", 326 | " ) AS max_weight \n", 327 | "FROM \n", 328 | " products_data_all \n", 329 | "WHERE \n", 330 | " category = 'milk & cream';" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": null, 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "metadata": {}, 343 | "source": [ 344 | "###### GROUP BY" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": null, 350 | "metadata": {}, 351 | "outputs": [], 352 | "source": [ 353 | "# Count number of products and unique products in various stores \n", 354 | "# (group the result by store names)\n", 355 | "SELECT \n", 356 | " name_store, \n", 357 | " COUNT(name) AS name_cnt, \n", 358 | " COUNT(DISTINCT name) AS name_uniq_cnt \n", 359 | "FROM \n", 360 | " products_data_all \n", 361 | "GROUP BY \n", 362 | " name_store;" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": null, 368 | "metadata": {}, 369 | "outputs": [], 370 | "source": [ 371 | "# Count the max weight of a product for each products' category\n", 372 | "SELECT \n", 373 | " category, \n", 374 | " MAX(\n", 375 | " CAST(weight AS real)\n", 376 | " ) \n", 377 | "FROM \n", 378 | " products_data_all \n", 379 | "GROUP BY \n", 380 | " category;" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": {}, 387 | "outputs": [], 388 | "source": [ 389 | "# Calculate max, min and average price for products in each store\n", 390 | "SELECT \n", 391 | " name_store, \n", 392 | " AVG(price) AS average_price, \n", 393 | " MAX(price) AS max_price, \n", 394 | " MIN(price) AS min_price \n", 395 | "FROM \n", 396 | " products_data_all \n", 397 | "GROUP BY \n", 398 | " name_store;" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": null, 404 | "metadata": {}, 405 | "outputs": [], 406 | "source": [ 407 | "# Find the difference between the max and min price for each product\n", 408 | "# in the category 'dairy butter' on the 2019-06-10\n", 409 | "SELECT \n", 410 | " name, \n", 411 | " MAX(price) - MIN(price) AS max_min_diff \n", 412 | "FROM \n", 413 | " products_data_all \n", 414 | "WHERE \n", 415 | " category = 'dairy butter' \n", 416 | " AND CAST(date_upd AS date) = '2019-06-10' \n", 417 | "GROUP BY \n", 418 | " name;" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": null, 424 | "metadata": {}, 425 | "outputs": [], 426 | "source": [] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "metadata": {}, 431 | "source": [ 432 | "###### ORDER BY" 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": null, 438 | "metadata": {}, 439 | "outputs": [], 440 | "source": [ 441 | "# Find the number of products in each category for the 2019-06-05.\n", 442 | "# Sort the result in ascending order.\n", 443 | "SELECT \n", 444 | " CAST(date_upd AS date) AS update_date, \n", 445 | " category, \n", 446 | " COUNT(name) AS name_cnt \n", 447 | "FROM \n", 448 | " products_data_all \n", 449 | "WHERE \n", 450 | " CAST(date_upd AS date) = '2019-06-05' \n", 451 | "GROUP BY \n", 452 | " category, \n", 453 | " update_date \n", 454 | "ORDER BY \n", 455 | " name_cnt ASC;" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": null, 461 | "metadata": {}, 462 | "outputs": [], 463 | "source": [ 464 | "# Count the number of unique products in each category \n", 465 | "# in the 'Lentro' store on the '2019-06-30'.\n", 466 | "# Sort the result in descending order\n", 467 | "SELECT \n", 468 | " CAST(date_upd AS date) AS update_date, \n", 469 | " name_store, \n", 470 | " category, \n", 471 | " COUNT(DISTINCT name) AS uniq_name_cnt \n", 472 | "FROM \n", 473 | " products_data_all \n", 474 | "WHERE \n", 475 | " name_store = 'Lentro' \n", 476 | " AND CAST(date_upd AS date) = '2019-06-30' \n", 477 | "GROUP BY \n", 478 | " update_date, \n", 479 | " name_store, \n", 480 | " category \n", 481 | "ORDER BY \n", 482 | " uniq_name_cnt DESC;" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": null, 488 | "metadata": {}, 489 | "outputs": [], 490 | "source": [] 491 | }, 492 | { 493 | "cell_type": "markdown", 494 | "metadata": {}, 495 | "source": [ 496 | "###### LIMIT" 497 | ] 498 | }, 499 | { 500 | "cell_type": "code", 501 | "execution_count": null, 502 | "metadata": {}, 503 | "outputs": [], 504 | "source": [ 505 | "# Find the top-5 most expensive products.\n", 506 | "SELECT \n", 507 | " name, \n", 508 | " MAX(price) AS max_price \n", 509 | "FROM \n", 510 | " products_data_all \n", 511 | "GROUP BY \n", 512 | " name \n", 513 | "ORDER BY \n", 514 | " max_price DESC \n", 515 | "LIMIT \n", 516 | " 5;" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": null, 522 | "metadata": {}, 523 | "outputs": [], 524 | "source": [] 525 | }, 526 | { 527 | "cell_type": "markdown", 528 | "metadata": {}, 529 | "source": [ 530 | "###### HAVING" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": null, 536 | "metadata": {}, 537 | "outputs": [], 538 | "source": [ 539 | "# Find the max price of each product. Show the name and the max price\n", 540 | "# of the products having the max price > 500.\n", 541 | "SELECT \n", 542 | " name, \n", 543 | " MAX(price) AS max_price \n", 544 | "FROM \n", 545 | " products_data_all \n", 546 | "GROUP BY \n", 547 | " name \n", 548 | "HAVING \n", 549 | " MAX(price) > 500;" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": null, 555 | "metadata": {}, 556 | "outputs": [], 557 | "source": [ 558 | "# Find the amount of products having weight > 900 g for the '2019-06-03'\n", 559 | "# Group the result by store name and show only products having the \n", 560 | "# amount < 10.\n", 561 | "SELECT \n", 562 | " CAST(date_upd AS date) AS update_date, \n", 563 | " name_store, \n", 564 | " COUNT(name) AS name_cnt \n", 565 | "FROM \n", 566 | " products_data_all \n", 567 | "WHERE \n", 568 | " (\n", 569 | " CAST(date_upd AS date) = '2019-06-03' \n", 570 | " AND units = 'г'\n", 571 | " ) \n", 572 | " AND CAST(weight AS real) > 900 \n", 573 | "GROUP BY \n", 574 | " name_store, \n", 575 | " CAST(date_upd AS date) \n", 576 | "HAVING \n", 577 | " COUNT(name) < 10;" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": null, 583 | "metadata": {}, 584 | "outputs": [], 585 | "source": [ 586 | "# Find the amount of unique products for each store. Reveal three\n", 587 | "# stores with the least amount of unique goods among stores having more\n", 588 | "# than 30 uniqe products.\n", 589 | "SELECT \n", 590 | " name_store, \n", 591 | " COUNT(DISTINCT name) AS name_uniq_cnt \n", 592 | "FROM \n", 593 | " products_data_all \n", 594 | "GROUP BY \n", 595 | " name_store \n", 596 | "HAVING \n", 597 | " COUNT(DISTINCT name) > 30 \n", 598 | "ORDER BY \n", 599 | " COUNT(DISTINCT name) ASC \n", 600 | "LIMIT \n", 601 | " 3;" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": null, 607 | "metadata": {}, 608 | "outputs": [], 609 | "source": [] 610 | }, 611 | { 612 | "cell_type": "markdown", 613 | "metadata": {}, 614 | "source": [ 615 | "###### EXTRACT" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": null, 621 | "metadata": {}, 622 | "outputs": [], 623 | "source": [ 624 | "# Extract hours from the date column (transactions table)\n", 625 | "SELECT \n", 626 | " EXTRACT(\n", 627 | " HOUR \n", 628 | " FROM \n", 629 | " date\n", 630 | " ) AS hours \n", 631 | "FROM \n", 632 | " transactions;" 633 | ] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": null, 638 | "metadata": {}, 639 | "outputs": [], 640 | "source": [ 641 | "# Extract hours from the date column (transactions table)\n", 642 | "# Group the data by hours and count the amount of products being sold.\n", 643 | "# Sort the data by hours in ascending order.\n", 644 | "SELECT \n", 645 | " EXTRACT(\n", 646 | " HOUR \n", 647 | " FROM \n", 648 | " date\n", 649 | " ) AS hours, \n", 650 | " COUNT(id_product) AS cnt \n", 651 | "FROM \n", 652 | " transactions \n", 653 | "GROUP BY \n", 654 | " hours \n", 655 | "ORDER BY \n", 656 | " hours;" 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": null, 662 | "metadata": {}, 663 | "outputs": [], 664 | "source": [ 665 | "# Extract day values from the date column (transactions table)\n", 666 | "# Group the data by days and count the amount of products being sold.\n", 667 | "# Sort the data by days in ascending order. \n", 668 | "SELECT \n", 669 | " EXTRACT(\n", 670 | " DAY \n", 671 | " FROM \n", 672 | " date\n", 673 | " ) AS days, \n", 674 | " COUNT(id_product) AS cnt \n", 675 | "FROM \n", 676 | " transactions \n", 677 | "GROUP BY \n", 678 | " days \n", 679 | "ORDER BY \n", 680 | " days ASC;" 681 | ] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "execution_count": null, 686 | "metadata": {}, 687 | "outputs": [], 688 | "source": [] 689 | }, 690 | { 691 | "cell_type": "markdown", 692 | "metadata": {}, 693 | "source": [ 694 | "###### DATE_TRUNC" 695 | ] 696 | }, 697 | { 698 | "cell_type": "code", 699 | "execution_count": null, 700 | "metadata": {}, 701 | "outputs": [], 702 | "source": [ 703 | "# Truncate the date to day. Group the data by days and \n", 704 | "# count the amount of products being sold.\n", 705 | "# Sort the data by days in ascending order. \n", 706 | "SELECT \n", 707 | " DATE_TRUNC('day', date) AS date_month, \n", 708 | " COUNT(id_product) AS cnt \n", 709 | "FROM \n", 710 | " transactions \n", 711 | "GROUP BY \n", 712 | " date_month \n", 713 | "ORDER BY \n", 714 | " date_month ASC;" 715 | ] 716 | }, 717 | { 718 | "cell_type": "code", 719 | "execution_count": null, 720 | "metadata": {}, 721 | "outputs": [], 722 | "source": [] 723 | }, 724 | { 725 | "cell_type": "markdown", 726 | "metadata": {}, 727 | "source": [ 728 | "###### Subqueries" 729 | ] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "execution_count": null, 734 | "metadata": {}, 735 | "outputs": [], 736 | "source": [ 737 | "# Query product ids from the table products_data_all\n", 738 | "# having category 'milk & cream' and the price > 120\n", 739 | "# or the category 'dairy butter' and the price > 354\n", 740 | "SELECT \n", 741 | " id_product \n", 742 | "FROM \n", 743 | " products_data_all \n", 744 | "WHERE \n", 745 | " (\n", 746 | " category = 'milk & cream' \n", 747 | " AND price > 120\n", 748 | " ) \n", 749 | " OR (\n", 750 | " category = 'dairy butter' \n", 751 | " AND price > 354\n", 752 | " );" 753 | ] 754 | }, 755 | { 756 | "cell_type": "code", 757 | "execution_count": null, 758 | "metadata": {}, 759 | "outputs": [], 760 | "source": [ 761 | "# Find unique user ids who has bought products from the category 'milk & cream' \n", 762 | "# and having the price > 120 or from the category 'dairy butter' \n", 763 | "# and having the price > 354\n", 764 | "SELECT \n", 765 | " DISTINCT(user_id) \n", 766 | "FROM \n", 767 | " transactions \n", 768 | "WHERE \n", 769 | " id_product IN (\n", 770 | " SELECT \n", 771 | " id_product \n", 772 | " FROM \n", 773 | " products_data_all \n", 774 | " WHERE \n", 775 | " (\n", 776 | " category = 'milk & cream' \n", 777 | " AND price > 120\n", 778 | " ) \n", 779 | " OR (\n", 780 | " category = 'dairy butter' \n", 781 | " AND price > 354\n", 782 | " )\n", 783 | " );" 784 | ] 785 | }, 786 | { 787 | "cell_type": "code", 788 | "execution_count": null, 789 | "metadata": {}, 790 | "outputs": [], 791 | "source": [ 792 | "# Truncate the date to day and count unique transactions\n", 793 | "SELECT \n", 794 | " COUNT(DISTINCT id_transaction) AS transaction_per_day, \n", 795 | " DATE_TRUNC('day', date) AS trunc_date \n", 796 | "FROM \n", 797 | " transactions \n", 798 | "GROUP BY \n", 799 | " trunc_date;" 800 | ] 801 | }, 802 | { 803 | "cell_type": "code", 804 | "execution_count": null, 805 | "metadata": {}, 806 | "outputs": [], 807 | "source": [ 808 | "# Use the previous query as a subquery to extract week numbers\n", 809 | "# and calculate an average number of transactions per day.\n", 810 | "# Group the result by week numbers.\n", 811 | "SELECT \n", 812 | " EXTRACT(\n", 813 | " WEEK \n", 814 | " FROM \n", 815 | " SUBQ.trunc_date\n", 816 | " ) AS week_number, \n", 817 | " AVG(SUBQ.transaction_per_day) AS avg_week_transaction \n", 818 | "FROM \n", 819 | " (\n", 820 | " SELECT \n", 821 | " COUNT(DISTINCT id_transaction) AS transaction_per_day, \n", 822 | " DATE_TRUNC('day', date) AS trunc_date \n", 823 | " FROM \n", 824 | " transactions \n", 825 | " GROUP BY \n", 826 | " trunc_date\n", 827 | " ) AS SUBQ \n", 828 | "GROUP BY \n", 829 | " week_number;" 830 | ] 831 | }, 832 | { 833 | "cell_type": "code", 834 | "execution_count": null, 835 | "metadata": {}, 836 | "outputs": [], 837 | "source": [] 838 | }, 839 | { 840 | "cell_type": "markdown", 841 | "metadata": {}, 842 | "source": [ 843 | "###### CASE WHEN ... THEN ... ELSE ... END" 844 | ] 845 | }, 846 | { 847 | "cell_type": "code", 848 | "execution_count": null, 849 | "metadata": {}, 850 | "outputs": [], 851 | "source": [ 852 | "# Check for NULL values in the column weight table products.\n", 853 | "SELECT \n", 854 | " id_product, \n", 855 | " units, \n", 856 | " weight \n", 857 | "FROM \n", 858 | " products \n", 859 | "WHERE \n", 860 | " weight IS NULL" 861 | ] 862 | }, 863 | { 864 | "cell_type": "code", 865 | "execution_count": null, 866 | "metadata": {}, 867 | "outputs": [], 868 | "source": [ 869 | "# Find the number of NULL values in the column weight table products.\n", 870 | "SELECT \n", 871 | " COUNT(*) \n", 872 | "FROM \n", 873 | " products \n", 874 | "WHERE \n", 875 | " weight IS NULL" 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "execution_count": null, 881 | "metadata": {}, 882 | "outputs": [], 883 | "source": [ 884 | "# Find an average weigth of products grouped by units of measurements (column units)\n", 885 | "SELECT \n", 886 | " AVG(\n", 887 | " CAST(weight AS real)\n", 888 | " ) AS avg_weight, \n", 889 | " units \n", 890 | "FROM \n", 891 | " products \n", 892 | "GROUP BY \n", 893 | " units" 894 | ] 895 | }, 896 | { 897 | "cell_type": "code", 898 | "execution_count": null, 899 | "metadata": {}, 900 | "outputs": [], 901 | "source": [ 902 | "# Substitute NULL values with average values calculted on the previous step.\n", 903 | "SELECT \n", 904 | " name, \n", 905 | " CASE WHEN units = '%' \n", 906 | " AND weight IS NULL THEN '72' WHEN units = 'kg' \n", 907 | " AND weight IS NULL THEN '1' WHEN units = 'l' \n", 908 | " AND weight IS NULL THEN '1' WHEN units = 'ml' \n", 909 | " AND weight IS NULL THEN '805' WHEN units = 'g' \n", 910 | " AND weight IS NULL THEN '402' ELSE weight END AS weight_info \n", 911 | "FROM \n", 912 | " products" 913 | ] 914 | }, 915 | { 916 | "cell_type": "code", 917 | "execution_count": null, 918 | "metadata": {}, 919 | "outputs": [], 920 | "source": [] 921 | }, 922 | { 923 | "cell_type": "markdown", 924 | "metadata": {}, 925 | "source": [ 926 | "###### LIKE" 927 | ] 928 | }, 929 | { 930 | "cell_type": "code", 931 | "execution_count": null, 932 | "metadata": {}, 933 | "outputs": [], 934 | "source": [ 935 | "# Find a row with '%' in units column\n", 936 | "SELECT \n", 937 | " * \n", 938 | "FROM \n", 939 | " products \n", 940 | "WHERE \n", 941 | " units LIKE '!%' ESCAPE '!';" 942 | ] 943 | }, 944 | { 945 | "cell_type": "code", 946 | "execution_count": null, 947 | "metadata": {}, 948 | "outputs": [], 949 | "source": [ 950 | "# Find products names having 'My' and 'My-My' in their names.\n", 951 | "SELECT \n", 952 | " * \n", 953 | "FROM \n", 954 | " products \n", 955 | "WHERE \n", 956 | " name LIKE '%Му%';" 957 | ] 958 | }, 959 | { 960 | "cell_type": "code", 961 | "execution_count": null, 962 | "metadata": {}, 963 | "outputs": [], 964 | "source": [ 965 | "# Calculate an average price of the products in category 'milk & cream' \n", 966 | "# sold on the 2019-06-01 and having 'My' and 'My-My' in their names.\n", 967 | "SELECT \n", 968 | " AVG(price) AS avg_price \n", 969 | "FROM \n", 970 | " products_stores \n", 971 | "WHERE \n", 972 | " date_upd = '2019-06-01' \n", 973 | " AND id_product IN (\n", 974 | " SELECT \n", 975 | " id_product \n", 976 | " FROM \n", 977 | " products \n", 978 | " WHERE \n", 979 | " name LIKE '%Му%' \n", 980 | " AND category = 'milk & cream'\n", 981 | " );" 982 | ] 983 | }, 984 | { 985 | "cell_type": "code", 986 | "execution_count": null, 987 | "metadata": {}, 988 | "outputs": [], 989 | "source": [] 990 | }, 991 | { 992 | "cell_type": "markdown", 993 | "metadata": {}, 994 | "source": [ 995 | "###### JOINS" 996 | ] 997 | }, 998 | { 999 | "cell_type": "code", 1000 | "execution_count": null, 1001 | "metadata": {}, 1002 | "outputs": [], 1003 | "source": [ 1004 | "# Inner join two tables on id_product field. Order the result by id_transaction (ascending)\n", 1005 | "# and limit the output to 10 items.\n", 1006 | "SELECT \n", 1007 | " id_transaction, \n", 1008 | " category, \n", 1009 | " name \n", 1010 | "FROM \n", 1011 | " transactions \n", 1012 | " INNER JOIN products ON products.id_product = transactions.id_product \n", 1013 | "ORDER BY \n", 1014 | " id_transaction ASC \n", 1015 | "LIMIT \n", 1016 | " 10" 1017 | ] 1018 | }, 1019 | { 1020 | "cell_type": "code", 1021 | "execution_count": null, 1022 | "metadata": {}, 1023 | "outputs": [], 1024 | "source": [ 1025 | "# Show temperature and rain values from the weather table for transactions stored \n", 1026 | "# in the transactions column. Sort the result by the date of transaction \n", 1027 | "# in descending order.\n", 1028 | "SELECT \n", 1029 | " DISTINCT transactions.date AS date, \n", 1030 | " weather.temp AS temp, \n", 1031 | " weather.rain AS rain, \n", 1032 | " transactions.id_transaction \n", 1033 | "FROM \n", 1034 | " transactions \n", 1035 | " INNER JOIN weather ON CAST(weather.date AS date) = CAST(transactions.date AS date) \n", 1036 | "ORDER BY \n", 1037 | " date DESC;" 1038 | ] 1039 | }, 1040 | { 1041 | "cell_type": "code", 1042 | "execution_count": null, 1043 | "metadata": {}, 1044 | "outputs": [], 1045 | "source": [ 1046 | "# Select unique products having price > 300 \n", 1047 | "SELECT \n", 1048 | " DISTINCT products.name AS name \n", 1049 | "FROM \n", 1050 | " products \n", 1051 | " INNER JOIN products_stores ON products_stores.id_product = products.id_product \n", 1052 | "WHERE \n", 1053 | " products_stores.price > 300;" 1054 | ] 1055 | }, 1056 | { 1057 | "cell_type": "code", 1058 | "execution_count": null, 1059 | "metadata": {}, 1060 | "outputs": [], 1061 | "source": [ 1062 | "# Select transactions in category 'dairy butter' made on the 2019-06-20. \n", 1063 | "SELECT \n", 1064 | " transactions.date AS date, \n", 1065 | " transactions.id_transaction AS id_transaction, \n", 1066 | " products.category AS category, \n", 1067 | " products.name AS name \n", 1068 | "FROM \n", 1069 | " transactions \n", 1070 | " INNER JOIN products ON products.id_product = transactions.id_product \n", 1071 | "WHERE \n", 1072 | " products.category = 'dairy butter' \n", 1073 | " AND CAST(transactions.date AS date) = '2019-06-20'" 1074 | ] 1075 | }, 1076 | { 1077 | "cell_type": "code", 1078 | "execution_count": null, 1079 | "metadata": {}, 1080 | "outputs": [], 1081 | "source": [ 1082 | "# Find the price of products sold on 2019-06-13 having unit of measurments 'ml'\n", 1083 | "SELECT \n", 1084 | " products.name AS name, \n", 1085 | " products.category AS category, \n", 1086 | " products.units AS units, \n", 1087 | " products.weight AS weight, \n", 1088 | " products_stores.price AS price \n", 1089 | "FROM \n", 1090 | " products \n", 1091 | " INNER JOIN products_stores ON products_stores.id_product = products.id_product \n", 1092 | "WHERE \n", 1093 | " products.units = 'мл' \n", 1094 | " AND CAST(products_stores.date_upd AS date) = '2019-06-13'" 1095 | ] 1096 | }, 1097 | { 1098 | "cell_type": "code", 1099 | "execution_count": null, 1100 | "metadata": {}, 1101 | "outputs": [], 1102 | "source": [ 1103 | "# Select unique id_products and name from the products table and id_store from\n", 1104 | "# the products_store table using left join.\n", 1105 | "SELECT \n", 1106 | " DISTINCT products.id_product AS id_product, \n", 1107 | " products.name AS name, \n", 1108 | " products_stores.id_store AS id_store \n", 1109 | "FROM \n", 1110 | " products \n", 1111 | " LEFT JOIN products_stores ON products_stores.id_product = products.id_product" 1112 | ] 1113 | }, 1114 | { 1115 | "cell_type": "code", 1116 | "execution_count": null, 1117 | "metadata": {}, 1118 | "outputs": [], 1119 | "source": [ 1120 | "# Show unique products names that haven't been sold in the store that has \n", 1121 | "# id_store equal to 3. Use left join to solve the task.\n", 1122 | "SELECT \n", 1123 | " DISTINCT products.name AS name \n", 1124 | "FROM \n", 1125 | " products \n", 1126 | " LEFT JOIN (\n", 1127 | " SELECT \n", 1128 | " DISTINCT id_product \n", 1129 | " FROM \n", 1130 | " transactions \n", 1131 | " WHERE \n", 1132 | " id_store = '3'\n", 1133 | " ) AS subquery ON subquery.id_product = products.id_product \n", 1134 | "WHERE \n", 1135 | " subquery.id_product IS NULL" 1136 | ] 1137 | }, 1138 | { 1139 | "cell_type": "code", 1140 | "execution_count": null, 1141 | "metadata": {}, 1142 | "outputs": [], 1143 | "source": [ 1144 | "# Show unique products names that haven't been sold in none of the stores\n", 1145 | "# on 2019-06-11\n", 1146 | "SELECT \n", 1147 | " DISTINCT products.name AS name \n", 1148 | "FROM \n", 1149 | " products \n", 1150 | " LEFT JOIN (\n", 1151 | " SELECT \n", 1152 | " DISTINCT id_product, \n", 1153 | " id_store \n", 1154 | " FROM \n", 1155 | " transactions \n", 1156 | " WHERE \n", 1157 | " CAST(transactions.date AS date) = '2019-06-11'\n", 1158 | " ) AS subquery ON subquery.id_product = products.id_product \n", 1159 | "WHERE \n", 1160 | " subquery.id_product IS NULL" 1161 | ] 1162 | }, 1163 | { 1164 | "cell_type": "code", 1165 | "execution_count": null, 1166 | "metadata": {}, 1167 | "outputs": [], 1168 | "source": [ 1169 | "# Retrieve dates when we have weather data but do not have any transactions data. Use\n", 1170 | "# right join.\n", 1171 | "SELECT \n", 1172 | " CAST(weather.date AS date) AS date \n", 1173 | "FROM \n", 1174 | " transactions \n", 1175 | " RIGHT JOIN weather ON CAST(weather.date AS date) = CAST(transactions.date AS date) \n", 1176 | "WHERE \n", 1177 | " transactions.date IS NULL" 1178 | ] 1179 | }, 1180 | { 1181 | "cell_type": "code", 1182 | "execution_count": null, 1183 | "metadata": {}, 1184 | "outputs": [], 1185 | "source": [ 1186 | "# Show unique products names that haven't been sold in the store that has \n", 1187 | "# id_store equal to 3. Use right join to solve the task.\n", 1188 | "SELECT \n", 1189 | " DISTINCT products.name AS name \n", 1190 | "FROM \n", 1191 | " (\n", 1192 | " SELECT \n", 1193 | " DISTINCT id_product \n", 1194 | " FROM \n", 1195 | " transactions \n", 1196 | " WHERE \n", 1197 | " id_store = '3'\n", 1198 | " ) AS subquery \n", 1199 | " RIGHT JOIN products ON products.id_product = subquery.id_product \n", 1200 | "WHERE \n", 1201 | " subquery.id_product IS NULL" 1202 | ] 1203 | }, 1204 | { 1205 | "cell_type": "code", 1206 | "execution_count": null, 1207 | "metadata": {}, 1208 | "outputs": [], 1209 | "source": [ 1210 | "# Retrieve id_transaction, name_store, category and products' name for all\n", 1211 | "# the transactions made on the 2019-06-05\n", 1212 | "SELECT \n", 1213 | " transactions.id_transaction AS id_transaction, \n", 1214 | " stores.name_store AS name_store, \n", 1215 | " products.category AS category, \n", 1216 | " products.name AS name \n", 1217 | "FROM \n", 1218 | " transactions \n", 1219 | " INNER JOIN products ON products.id_product = transactions.id_product \n", 1220 | " INNER JOIN stores ON stores.id_store = transactions.id_store \n", 1221 | "WHERE \n", 1222 | " CAST(transactions.date AS date) = '2019-06-05'" 1223 | ] 1224 | }, 1225 | { 1226 | "cell_type": "code", 1227 | "execution_count": null, 1228 | "metadata": {}, 1229 | "outputs": [], 1230 | "source": [ 1231 | "# Find the info about the weather and sold products.\n", 1232 | "# Sort the output by date (descending) and name (ascending) \n", 1233 | "# and limit the outup to 30 rows.\n", 1234 | "SELECT \n", 1235 | " CAST(weather.date AS date) AS date, \n", 1236 | " weather.temp AS temp, \n", 1237 | " weather.rain AS rain, \n", 1238 | " products.name AS name \n", 1239 | "FROM \n", 1240 | " weather \n", 1241 | " LEFT JOIN transactions ON CAST(transactions.date AS date) = CAST(weather.date AS date) \n", 1242 | " LEFT JOIN products ON transactions.id_product = products.id_product \n", 1243 | "ORDER BY \n", 1244 | " date DESC, \n", 1245 | " name ASC \n", 1246 | "LIMIT \n", 1247 | " 30" 1248 | ] 1249 | }, 1250 | { 1251 | "cell_type": "code", 1252 | "execution_count": null, 1253 | "metadata": {}, 1254 | "outputs": [], 1255 | "source": [ 1256 | "# Show id_transaction and products' name for items sold on days when there were no rain.\n", 1257 | "SELECT \n", 1258 | " transactions.id_transaction AS id_transaction, \n", 1259 | " products.name AS name \n", 1260 | "FROM \n", 1261 | " transactions \n", 1262 | " INNER JOIN products ON products.id_product = transactions.id_product \n", 1263 | " INNER JOIN weather ON CAST(weather.date AS date) = CAST(transactions.date AS date) \n", 1264 | "WHERE \n", 1265 | " weather.rain = '0'" 1266 | ] 1267 | }, 1268 | { 1269 | "cell_type": "code", 1270 | "execution_count": null, 1271 | "metadata": {}, 1272 | "outputs": [], 1273 | "source": [ 1274 | "# Count the number of products an the number of unique products for each transaction.\n", 1275 | "# Limit the output to 10 rows.\n", 1276 | "SELECT \n", 1277 | " transactions.id_transaction AS id_transaction, \n", 1278 | " COUNT(products.name) AS name_cnt, \n", 1279 | " COUNT(DISTINCT products.name) AS name_uniq_cnt \n", 1280 | "FROM \n", 1281 | " transactions \n", 1282 | " INNER JOIN products ON transactions.id_product = products.id_product \n", 1283 | "GROUP BY \n", 1284 | " transactions.id_transaction \n", 1285 | "LIMIT \n", 1286 | " 10" 1287 | ] 1288 | }, 1289 | { 1290 | "cell_type": "code", 1291 | "execution_count": null, 1292 | "metadata": {}, 1293 | "outputs": [], 1294 | "source": [ 1295 | "# Find transactions having non-unique products sold.\n", 1296 | "SELECT \n", 1297 | " transactions.id_transaction AS id_transaction, \n", 1298 | " COUNT(products.name) AS name_cnt, \n", 1299 | " COUNT(DISTINCT products.name) AS name_uniq_cnt \n", 1300 | "FROM \n", 1301 | " transactions \n", 1302 | " INNER JOIN products ON products.id_product = transactions.id_product \n", 1303 | "GROUP BY \n", 1304 | " id_transaction \n", 1305 | "HAVING \n", 1306 | " COUNT(products.name) != COUNT(DISTINCT products.name)" 1307 | ] 1308 | }, 1309 | { 1310 | "cell_type": "code", 1311 | "execution_count": null, 1312 | "metadata": {}, 1313 | "outputs": [], 1314 | "source": [ 1315 | "# Count the number of transaction made on sunny and on rainy days.\n", 1316 | "# Group the output by the column rain (weather table).\n", 1317 | "SELECT \n", 1318 | " weather.rain AS rain, \n", 1319 | " COUNT(\n", 1320 | " DISTINCT transactions.id_transaction\n", 1321 | " ) AS id_transaction \n", 1322 | "FROM \n", 1323 | " transactions \n", 1324 | " INNER JOIN weather ON CAST(weather.date AS date) = CAST(transactions.date AS date) \n", 1325 | "GROUP BY \n", 1326 | " weather.rain" 1327 | ] 1328 | }, 1329 | { 1330 | "cell_type": "code", 1331 | "execution_count": null, 1332 | "metadata": {}, 1333 | "outputs": [], 1334 | "source": [ 1335 | "# Retrieve the temperature and the number of transactions.\n", 1336 | "# Group the output by date and temperature.\n", 1337 | "# Sort the values by date in the ascending order.\n", 1338 | "SELECT \n", 1339 | " CAST(weather.date AS date) AS date, \n", 1340 | " weather.temp AS temp, \n", 1341 | " COUNT(\n", 1342 | " DISTINCT transactions.id_transaction\n", 1343 | " ) AS uniq_transactions \n", 1344 | "FROM \n", 1345 | " weather \n", 1346 | " LEFT JOIN transactions ON CAST(transactions.date AS date) = CAST(weather.date AS date) \n", 1347 | "GROUP BY \n", 1348 | " CAST(weather.date AS date), \n", 1349 | " weather.temp \n", 1350 | "ORDER BY \n", 1351 | " CAST(weather.date AS date) ASC" 1352 | ] 1353 | }, 1354 | { 1355 | "cell_type": "code", 1356 | "execution_count": null, 1357 | "metadata": {}, 1358 | "outputs": [], 1359 | "source": [ 1360 | "# Calculate the number of items and the total price for every transaction.\n", 1361 | "# Reveal only transactions having the total price > 1000.\n", 1362 | "SELECT \n", 1363 | " transactions.id_transaction AS id_transaction, \n", 1364 | " SUM(products_stores.price) AS total, \n", 1365 | " COUNT(products_stores.id_product) AS amount \n", 1366 | "FROM \n", 1367 | " transactions \n", 1368 | " LEFT JOIN products_stores ON CAST(products_stores.date_upd AS date) = CAST(transactions.date AS date) \n", 1369 | " AND products_stores.id_product = transactions.id_product \n", 1370 | " AND products_stores.id_store = transactions.id_store \n", 1371 | "GROUP BY \n", 1372 | " transactions.id_transaction \n", 1373 | "HAVING \n", 1374 | " SUM(products_stores.price) > 1000" 1375 | ] 1376 | }, 1377 | { 1378 | "cell_type": "code", 1379 | "execution_count": null, 1380 | "metadata": {}, 1381 | "outputs": [], 1382 | "source": [] 1383 | }, 1384 | { 1385 | "cell_type": "markdown", 1386 | "metadata": {}, 1387 | "source": [ 1388 | "###### UNION" 1389 | ] 1390 | }, 1391 | { 1392 | "cell_type": "code", 1393 | "execution_count": null, 1394 | "metadata": {}, 1395 | "outputs": [], 1396 | "source": [ 1397 | "# SELECT unique products' name for transactions made on 2019-06-01.\n", 1398 | "SELECT \n", 1399 | " DISTINCT products.name AS name \n", 1400 | "FROM \n", 1401 | " products \n", 1402 | " LEFT JOIN (\n", 1403 | " SELECT \n", 1404 | " transactions.id_product \n", 1405 | " FROM \n", 1406 | " transactions \n", 1407 | " WHERE \n", 1408 | " CAST(transactions.date AS date) = '2019-06-01'\n", 1409 | " ) AS subq ON products.id_product = subq.id_product \n", 1410 | "WHERE \n", 1411 | " subq.id_product IS NOT NULL;" 1412 | ] 1413 | }, 1414 | { 1415 | "cell_type": "code", 1416 | "execution_count": null, 1417 | "metadata": {}, 1418 | "outputs": [], 1419 | "source": [ 1420 | "# SELECT unique products' name for transactions made on 2019-06-08.\n", 1421 | "SELECT \n", 1422 | " DISTINCT products.name AS name \n", 1423 | "FROM \n", 1424 | " products \n", 1425 | " LEFT JOIN (\n", 1426 | " SELECT \n", 1427 | " transactions.id_product \n", 1428 | " FROM \n", 1429 | " transactions \n", 1430 | " WHERE \n", 1431 | " CAST(transactions.date AS date) = '2019-06-08'\n", 1432 | " ) AS subq ON products.id_product = subq.id_product \n", 1433 | "WHERE \n", 1434 | " subq.id_product IS NOT NULL;" 1435 | ] 1436 | }, 1437 | { 1438 | "cell_type": "code", 1439 | "execution_count": null, 1440 | "metadata": {}, 1441 | "outputs": [], 1442 | "source": [ 1443 | "# Merge two queries (from the previous steps) together.\n", 1444 | "# Remove duplicates.\n", 1445 | "SELECT \n", 1446 | " DISTINCT products.name AS name \n", 1447 | "FROM \n", 1448 | " products \n", 1449 | " LEFT JOIN (\n", 1450 | " SELECT \n", 1451 | " id_product \n", 1452 | " FROM \n", 1453 | " transactions \n", 1454 | " WHERE \n", 1455 | " CAST(transactions.date AS date) = '2019-06-01'\n", 1456 | " ) AS SUBQ1 ON products.id_product = SUBQ1.id_product \n", 1457 | "WHERE \n", 1458 | " SUBQ1.id_product IS NOT NULL \n", 1459 | "UNION \n", 1460 | "SELECT \n", 1461 | " DISTINCT products.name AS name \n", 1462 | "FROM \n", 1463 | " products \n", 1464 | " LEFT JOIN (\n", 1465 | " SELECT \n", 1466 | " id_product \n", 1467 | " FROM \n", 1468 | " transactions \n", 1469 | " WHERE \n", 1470 | " CAST(transactions.date AS date) = '2019-06-08'\n", 1471 | " ) AS SUBQ2 ON products.id_product = SUBQ2.id_product \n", 1472 | "WHERE \n", 1473 | " SUBQ2.id_product IS NOT NULL;" 1474 | ] 1475 | }, 1476 | { 1477 | "cell_type": "code", 1478 | "execution_count": null, 1479 | "metadata": {}, 1480 | "outputs": [], 1481 | "source": [ 1482 | "# Count the number of products using the previous query as a sub-query.\n", 1483 | "SELECT \n", 1484 | " COUNT(SUBQ.name) \n", 1485 | "FROM \n", 1486 | " (\n", 1487 | " SELECT \n", 1488 | " DISTINCT products.name AS name \n", 1489 | " FROM \n", 1490 | " products \n", 1491 | " LEFT JOIN (\n", 1492 | " SELECT \n", 1493 | " id_product \n", 1494 | " FROM \n", 1495 | " transactions \n", 1496 | " WHERE \n", 1497 | " CAST(transactions.date AS date) = '2019-06-01'\n", 1498 | " ) AS SUBQ1 ON products.id_product = SUBQ1.id_product \n", 1499 | " WHERE \n", 1500 | " SUBQ1.id_product IS NOT NULL \n", 1501 | " UNION \n", 1502 | " SELECT \n", 1503 | " DISTINCT products.name AS name \n", 1504 | " FROM \n", 1505 | " products \n", 1506 | " LEFT JOIN (\n", 1507 | " SELECT \n", 1508 | " id_product \n", 1509 | " FROM \n", 1510 | " transactions \n", 1511 | " WHERE \n", 1512 | " CAST(transactions.date AS date) = '2019-06-08'\n", 1513 | " ) AS SUBQ2 ON products.id_product = SUBQ2.id_product \n", 1514 | " WHERE \n", 1515 | " SUBQ2.id_product IS NOT NULL\n", 1516 | " ) AS SUBQ" 1517 | ] 1518 | }, 1519 | { 1520 | "cell_type": "code", 1521 | "execution_count": null, 1522 | "metadata": {}, 1523 | "outputs": [], 1524 | "source": [ 1525 | "# Merge two queries (from the previous steps) together without removing duplicates.\n", 1526 | "# Count the number of products using the merged queries as a sub-query.\n", 1527 | "SELECT \n", 1528 | " COUNT(SUBQ.name) \n", 1529 | "FROM \n", 1530 | " (\n", 1531 | " SELECT \n", 1532 | " DISTINCT products.name AS name \n", 1533 | " FROM \n", 1534 | " products \n", 1535 | " LEFT JOIN (\n", 1536 | " SELECT \n", 1537 | " id_product \n", 1538 | " FROM \n", 1539 | " transactions \n", 1540 | " WHERE \n", 1541 | " CAST(transactions.date AS date) = '2019-06-01'\n", 1542 | " ) AS SUBQ1 ON products.id_product = SUBQ1.id_product \n", 1543 | " WHERE \n", 1544 | " SUBQ1.id_product IS NOT NULL \n", 1545 | " UNION ALL \n", 1546 | " SELECT \n", 1547 | " DISTINCT products.name AS name \n", 1548 | " FROM \n", 1549 | " products \n", 1550 | " LEFT JOIN (\n", 1551 | " SELECT \n", 1552 | " id_product \n", 1553 | " FROM \n", 1554 | " transactions \n", 1555 | " WHERE \n", 1556 | " CAST(transactions.date AS date) = '2019-06-08'\n", 1557 | " ) AS SUBQ2 ON products.id_product = SUBQ2.id_product \n", 1558 | " WHERE \n", 1559 | " SUBQ2.id_product IS NOT NULL\n", 1560 | " ) AS SUBQ;" 1561 | ] 1562 | } 1563 | ], 1564 | "metadata": { 1565 | "kernelspec": { 1566 | "display_name": "Python 3", 1567 | "language": "python", 1568 | "name": "python3" 1569 | }, 1570 | "language_info": { 1571 | "codemirror_mode": { 1572 | "name": "ipython", 1573 | "version": 3 1574 | }, 1575 | "file_extension": ".py", 1576 | "mimetype": "text/x-python", 1577 | "name": "python", 1578 | "nbconvert_exporter": "python", 1579 | "pygments_lexer": "ipython3", 1580 | "version": "3.7.6" 1581 | } 1582 | }, 1583 | "nbformat": 4, 1584 | "nbformat_minor": 4 1585 | } 1586 | -------------------------------------------------------------------------------- /6_sql_bolt/sql_tasks_and_solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# SQL Bolt " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Interactive Tutorial" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "[https://sqlbolt.com/](https://sqlbolt.com/)" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "### Table of Contents \n", 29 | "[1. SELECT queries](#SQL-Lesson-1:-SELECT-queries) \n", 30 | "[2. Queries with constraints (Pt. 1)](#SQL-Lesson-2:-Queries-with-constraints-(Pt.-1)) \n", 31 | "[3. Queries with constraints (Pt. 2)](#SQL-Lesson-3:-Queries-with-constraints-(Pt.-2)) \n", 32 | "[4. Filtering and sorting Query results](#SQL-Lesson-4:-Filtering-and-sorting-Query-results) \n", 33 | "[5. Simple SELECT Queries Review](#SQL-Lesson-5:-Simple-SELECT-Queries-Review) \n", 34 | "[6. Multi-table queries with JOINs](#SQL-Lesson-6:-Multi-table-queries-with-JOINs) \n", 35 | "[7. OUTER JOINs](#SQL-Lesson-7:-OUTER-JOINs) \n", 36 | "[8. A short note on NULLs](#SQL-Lesson-8:-A-short-note-on-NULLs) \n", 37 | "[9. Queries with expressions](#SQL-Lesson-9:-Queries-with-expressions) \n", 38 | "[10. Queries with aggregates (Pt. 1)](#SQL-Lesson-10:-Queries-with-aggregates-(Pt.-1)) \n", 39 | "[11. Queries with aggregates (Pt. 2)](#SQL-Lesson-11:-Queries-with-aggregates-(Pt.-2)) \n", 40 | "[12. Order of execution of a Query](#SQL-Lesson-12:-Order-of-execution-of-a-Query) \n", 41 | "[13. Inserting rows](#SQL-Lesson-13:-Inserting-rows) \n", 42 | "[14. Updating rows](#SQL-Lesson-14:-Updating-rows) \n", 43 | "[15. Deleting rows](#SQL-Lesson-15:-Deleting-rows) \n", 44 | "[16. Creating tables](#SQL-Lesson-16:-Creating-tables) \n", 45 | "[17. Altering tables](#SQL-Lesson-17:-Altering-tables) \n", 46 | "[18. Dropping tables](#SQL-Lesson-18:-Dropping-tables) \n", 47 | "[19. Subqueries](#SQL-Lesson-19:-Subqueries) \n", 48 | "[20. Unions, Intersections & Exceptions](#SQL-Extra:-Unions,-Intersections-&-Exceptions) " 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "### SQL Lesson 1: SELECT queries" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "#### Task 1" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "Find the title of each film" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "`SELECT title \n", 77 | "FROM movies;`" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "#### Task 2" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "Find the director of each film " 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "`SELECT director \n", 99 | "FROM movies;`" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "#### Task 3" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "Find the title and director of each film" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "`SELECT \n", 121 | " title,\n", 122 | " director \n", 123 | "FROM movies;`" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "#### Task 4" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "Find the title and year of each film " 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "`SELECT \n", 145 | " title,\n", 146 | " year \n", 147 | "FROM movies;`" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "#### Task 5" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "Find all the information about each film" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "`SELECT * \n", 169 | "FROM movies;`" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "### SQL Lesson 2: Queries with constraints (Pt. 1) " 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "#### Task 1" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "Find the movie with a row id of 6 " 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "`\n", 198 | "SELECT *\n", 199 | "FROM \n", 200 | " movies\n", 201 | "WHERE \n", 202 | " id = 6;\n", 203 | "`" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "#### Task 2" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "Find the movies released in the years between 2000 and 2010 " 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "`SELECT * \n", 225 | "FROM \n", 226 | " movies\n", 227 | "WHERE \n", 228 | " year BETWEEN 2000 and 2010;`" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "#### Task 3" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": {}, 241 | "source": [ 242 | "Find the movies not released in the years between 2000 and 2010 " 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "`SELECT * \n", 250 | "FROM \n", 251 | " movies\n", 252 | "WHERE \n", 253 | " year NOT BETWEEN 2000 and 2010;`" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "#### Task 4" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "Find the first 5 Pixar movies and their release year" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "`SELECT \n", 275 | " title,\n", 276 | " year\n", 277 | "FROM \n", 278 | " movies\n", 279 | "WHERE \n", 280 | " year < 2004;`" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "### SQL Lesson 3: Queries with constraints (Pt. 2) " 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "#### Task 1" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | " Find all the Toy Story movies" 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "metadata": {}, 307 | "source": [ 308 | "`\n", 309 | "SELECT * \n", 310 | "FROM \n", 311 | " movies\n", 312 | "WHERE\n", 313 | " title LIKE 'Toy Story%';\n", 314 | "`" 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": {}, 320 | "source": [ 321 | "#### Task 2" 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": {}, 327 | "source": [ 328 | "Find all the movies directed by John Lasseter " 329 | ] 330 | }, 331 | { 332 | "cell_type": "markdown", 333 | "metadata": {}, 334 | "source": [ 335 | "`SELECT * \n", 336 | "FROM \n", 337 | " movies\n", 338 | "WHERE\n", 339 | " director = 'John Lasseter';`" 340 | ] 341 | }, 342 | { 343 | "cell_type": "markdown", 344 | "metadata": {}, 345 | "source": [ 346 | "#### Task 3" 347 | ] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "metadata": {}, 352 | "source": [ 353 | "Find all the movies (and director) not directed by John Lasseter " 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "`SELECT\n", 361 | " title,\n", 362 | " director\n", 363 | "FROM \n", 364 | " movies\n", 365 | "WHERE\n", 366 | " director != 'John Lasseter';`" 367 | ] 368 | }, 369 | { 370 | "cell_type": "markdown", 371 | "metadata": {}, 372 | "source": [ 373 | "#### Task 4" 374 | ] 375 | }, 376 | { 377 | "cell_type": "markdown", 378 | "metadata": {}, 379 | "source": [ 380 | "Find all the WALL-* movies " 381 | ] 382 | }, 383 | { 384 | "cell_type": "markdown", 385 | "metadata": {}, 386 | "source": [ 387 | "`SELECT *\n", 388 | "FROM \n", 389 | " movies\n", 390 | "WHERE\n", 391 | " title LIKE 'WALL-_';`" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": {}, 397 | "source": [ 398 | "### SQL Lesson 4: Filtering and sorting Query results " 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "metadata": {}, 404 | "source": [ 405 | "#### Task 1" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "List all directors of Pixar movies (alphabetically), without duplicates" 413 | ] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "metadata": {}, 418 | "source": [ 419 | "`\n", 420 | "SELECT\n", 421 | " DISTINCT(director)\n", 422 | "FROM \n", 423 | " movies\n", 424 | "ORDER BY director ASC;\n", 425 | "`" 426 | ] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "metadata": {}, 431 | "source": [ 432 | "#### Task 2" 433 | ] 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "metadata": {}, 438 | "source": [ 439 | "List the last four Pixar movies released (ordered from most recent to least)" 440 | ] 441 | }, 442 | { 443 | "cell_type": "markdown", 444 | "metadata": {}, 445 | "source": [ 446 | "`SELECT *\n", 447 | "FROM \n", 448 | " movies\n", 449 | "ORDER BY year DESC\n", 450 | "LIMIT 4;`" 451 | ] 452 | }, 453 | { 454 | "cell_type": "markdown", 455 | "metadata": {}, 456 | "source": [ 457 | "#### Task 3" 458 | ] 459 | }, 460 | { 461 | "cell_type": "markdown", 462 | "metadata": {}, 463 | "source": [ 464 | "List the first five Pixar movies sorted alphabetically " 465 | ] 466 | }, 467 | { 468 | "cell_type": "markdown", 469 | "metadata": {}, 470 | "source": [ 471 | "`SELECT *\n", 472 | "FROM \n", 473 | " movies\n", 474 | "ORDER BY title ASC\n", 475 | "LIMIT 5;`" 476 | ] 477 | }, 478 | { 479 | "cell_type": "markdown", 480 | "metadata": {}, 481 | "source": [ 482 | "#### Task 4" 483 | ] 484 | }, 485 | { 486 | "cell_type": "markdown", 487 | "metadata": {}, 488 | "source": [ 489 | "List the next five Pixar movies sorted alphabetically " 490 | ] 491 | }, 492 | { 493 | "cell_type": "markdown", 494 | "metadata": {}, 495 | "source": [ 496 | "`SELECT *\n", 497 | "FROM \n", 498 | " movies\n", 499 | "ORDER BY title ASC\n", 500 | "LIMIT 5 OFFSET 5;`" 501 | ] 502 | }, 503 | { 504 | "cell_type": "markdown", 505 | "metadata": {}, 506 | "source": [ 507 | "### SQL Lesson 5: Simple SELECT Queries Review" 508 | ] 509 | }, 510 | { 511 | "cell_type": "markdown", 512 | "metadata": {}, 513 | "source": [ 514 | "#### Task 1" 515 | ] 516 | }, 517 | { 518 | "cell_type": "markdown", 519 | "metadata": {}, 520 | "source": [ 521 | "List all the Canadian cities and their populations " 522 | ] 523 | }, 524 | { 525 | "cell_type": "markdown", 526 | "metadata": {}, 527 | "source": [ 528 | "`SELECT\n", 529 | " city, \n", 530 | " country, \n", 531 | " population\n", 532 | "FROM \n", 533 | " north_american_cities\n", 534 | "WHERE\n", 535 | " country = 'Canada';`" 536 | ] 537 | }, 538 | { 539 | "cell_type": "markdown", 540 | "metadata": {}, 541 | "source": [ 542 | "#### Task 2" 543 | ] 544 | }, 545 | { 546 | "cell_type": "markdown", 547 | "metadata": {}, 548 | "source": [ 549 | "Order all the cities in the United States by their latitude from north to south" 550 | ] 551 | }, 552 | { 553 | "cell_type": "markdown", 554 | "metadata": {}, 555 | "source": [ 556 | "`SELECT\n", 557 | " city, \n", 558 | " country, \n", 559 | " latitude\n", 560 | "FROM \n", 561 | " north_american_cities\n", 562 | "WHERE\n", 563 | " country = 'United States'\n", 564 | "ORDER BY\n", 565 | " latitude DESC;`" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": {}, 571 | "source": [ 572 | "#### Task 3" 573 | ] 574 | }, 575 | { 576 | "cell_type": "markdown", 577 | "metadata": {}, 578 | "source": [ 579 | "List all the cities west of Chicago, ordered from west to east " 580 | ] 581 | }, 582 | { 583 | "cell_type": "markdown", 584 | "metadata": {}, 585 | "source": [ 586 | "`SELECT\n", 587 | " city, \n", 588 | " country, \n", 589 | " longitude\n", 590 | "FROM \n", 591 | " north_american_cities\n", 592 | "WHERE\n", 593 | " longitude < -87.629798\n", 594 | "ORDER BY\n", 595 | " longitude ASC;`" 596 | ] 597 | }, 598 | { 599 | "cell_type": "markdown", 600 | "metadata": {}, 601 | "source": [ 602 | "#### Task 4" 603 | ] 604 | }, 605 | { 606 | "cell_type": "markdown", 607 | "metadata": {}, 608 | "source": [ 609 | "List the two largest cities in Mexico (by population) " 610 | ] 611 | }, 612 | { 613 | "cell_type": "markdown", 614 | "metadata": {}, 615 | "source": [ 616 | "`SELECT\n", 617 | " city, \n", 618 | " country, \n", 619 | " population\n", 620 | "FROM \n", 621 | " north_american_cities\n", 622 | "WHERE\n", 623 | " country = 'Mexico'\n", 624 | "ORDER BY\n", 625 | " population DESC\n", 626 | "LIMIT \n", 627 | " 2;`" 628 | ] 629 | }, 630 | { 631 | "cell_type": "markdown", 632 | "metadata": {}, 633 | "source": [ 634 | "#### Task 5" 635 | ] 636 | }, 637 | { 638 | "cell_type": "markdown", 639 | "metadata": {}, 640 | "source": [ 641 | "List the third and fourth largest cities (by population) in the United States and their population " 642 | ] 643 | }, 644 | { 645 | "cell_type": "markdown", 646 | "metadata": {}, 647 | "source": [ 648 | "`SELECT\n", 649 | " city, \n", 650 | " country, \n", 651 | " population\n", 652 | "FROM \n", 653 | " north_american_cities\n", 654 | "WHERE\n", 655 | " country = 'United States'\n", 656 | "ORDER BY\n", 657 | " population DESC\n", 658 | "LIMIT \n", 659 | " 2\n", 660 | "OFFSET \n", 661 | " 2;`" 662 | ] 663 | }, 664 | { 665 | "cell_type": "markdown", 666 | "metadata": {}, 667 | "source": [ 668 | "### SQL Lesson 6: Multi-table queries with JOINs " 669 | ] 670 | }, 671 | { 672 | "cell_type": "markdown", 673 | "metadata": {}, 674 | "source": [ 675 | "#### Task 1" 676 | ] 677 | }, 678 | { 679 | "cell_type": "markdown", 680 | "metadata": {}, 681 | "source": [ 682 | "Find the domestic and international sales for each movie " 683 | ] 684 | }, 685 | { 686 | "cell_type": "markdown", 687 | "metadata": {}, 688 | "source": [ 689 | "`SELECT \n", 690 | " title,\n", 691 | " domestic_sales,\n", 692 | " international_sales\n", 693 | "FROM \n", 694 | " movies\n", 695 | "INNER JOIN boxoffice\n", 696 | " ON movies.id = boxoffice.movie_id;`" 697 | ] 698 | }, 699 | { 700 | "cell_type": "markdown", 701 | "metadata": {}, 702 | "source": [ 703 | "#### Task 2" 704 | ] 705 | }, 706 | { 707 | "cell_type": "markdown", 708 | "metadata": {}, 709 | "source": [ 710 | "Show the sales numbers for each movie that did better internationally rather than domestically " 711 | ] 712 | }, 713 | { 714 | "cell_type": "markdown", 715 | "metadata": {}, 716 | "source": [ 717 | "`SELECT \n", 718 | " title,\n", 719 | " domestic_sales,\n", 720 | " international_sales\n", 721 | "FROM \n", 722 | " movies\n", 723 | "INNER JOIN boxoffice\n", 724 | " ON movies.id = boxoffice.movie_id\n", 725 | "WHERE \n", 726 | " international_sales > domestic_sales;`" 727 | ] 728 | }, 729 | { 730 | "cell_type": "markdown", 731 | "metadata": {}, 732 | "source": [ 733 | "#### Task 3" 734 | ] 735 | }, 736 | { 737 | "cell_type": "markdown", 738 | "metadata": {}, 739 | "source": [ 740 | "List all the movies by their ratings in descending order " 741 | ] 742 | }, 743 | { 744 | "cell_type": "markdown", 745 | "metadata": {}, 746 | "source": [ 747 | "`SELECT \n", 748 | " title,\n", 749 | " rating\n", 750 | "FROM \n", 751 | " movies\n", 752 | "INNER JOIN boxoffice\n", 753 | " ON movies.id = boxoffice.movie_id\n", 754 | "ORDER BY\n", 755 | " rating DESC;`" 756 | ] 757 | }, 758 | { 759 | "cell_type": "markdown", 760 | "metadata": {}, 761 | "source": [ 762 | "### SQL Lesson 7: OUTER JOINs " 763 | ] 764 | }, 765 | { 766 | "cell_type": "markdown", 767 | "metadata": {}, 768 | "source": [ 769 | "#### Task 1" 770 | ] 771 | }, 772 | { 773 | "cell_type": "markdown", 774 | "metadata": {}, 775 | "source": [ 776 | "Find the list of all buildings that have employees " 777 | ] 778 | }, 779 | { 780 | "cell_type": "markdown", 781 | "metadata": {}, 782 | "source": [ 783 | "`SELECT \n", 784 | " DISTINCT(building_name)\n", 785 | "FROM \n", 786 | " buildings AS l\n", 787 | "INNER JOIN employees AS r\n", 788 | " ON l.building_name = r.building;`" 789 | ] 790 | }, 791 | { 792 | "cell_type": "markdown", 793 | "metadata": {}, 794 | "source": [ 795 | "#### Task 2" 796 | ] 797 | }, 798 | { 799 | "cell_type": "markdown", 800 | "metadata": {}, 801 | "source": [ 802 | "Find the list of all buildings and their capacity" 803 | ] 804 | }, 805 | { 806 | "cell_type": "markdown", 807 | "metadata": {}, 808 | "source": [ 809 | "`SELECT \n", 810 | " DISTINCT(building_name),\n", 811 | " capacity\n", 812 | "FROM \n", 813 | " buildings AS l\n", 814 | "LEFT JOIN employees AS r\n", 815 | " ON l.building_name = r.building;`" 816 | ] 817 | }, 818 | { 819 | "cell_type": "markdown", 820 | "metadata": {}, 821 | "source": [ 822 | "#### Task 3" 823 | ] 824 | }, 825 | { 826 | "cell_type": "markdown", 827 | "metadata": {}, 828 | "source": [ 829 | "List all buildings and the distinct employee roles in each building (including empty buildings)" 830 | ] 831 | }, 832 | { 833 | "cell_type": "markdown", 834 | "metadata": {}, 835 | "source": [ 836 | "`SELECT \n", 837 | " DISTINCT(building_name),\n", 838 | " role\n", 839 | "FROM \n", 840 | " buildings AS l\n", 841 | "LEFT JOIN employees AS r\n", 842 | " ON l.building_name = r.building;`" 843 | ] 844 | }, 845 | { 846 | "cell_type": "markdown", 847 | "metadata": {}, 848 | "source": [ 849 | "### SQL Lesson 8: A short note on NULLs " 850 | ] 851 | }, 852 | { 853 | "cell_type": "markdown", 854 | "metadata": {}, 855 | "source": [ 856 | "#### Task 1" 857 | ] 858 | }, 859 | { 860 | "cell_type": "markdown", 861 | "metadata": {}, 862 | "source": [ 863 | "Find the name and role of all employees who have not been assigned to a building " 864 | ] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "metadata": {}, 869 | "source": [ 870 | "`SELECT\n", 871 | " name,\n", 872 | " role,\n", 873 | " building\n", 874 | "FROM \n", 875 | " employees\n", 876 | "WHERE\n", 877 | " building IS NULL;`" 878 | ] 879 | }, 880 | { 881 | "cell_type": "markdown", 882 | "metadata": {}, 883 | "source": [ 884 | "#### Task 2" 885 | ] 886 | }, 887 | { 888 | "cell_type": "markdown", 889 | "metadata": {}, 890 | "source": [ 891 | "Find the names of the buildings that hold no employees " 892 | ] 893 | }, 894 | { 895 | "cell_type": "markdown", 896 | "metadata": {}, 897 | "source": [ 898 | "`SELECT\n", 899 | " building_name,\n", 900 | " name\n", 901 | "FROM \n", 902 | " buildings AS l\n", 903 | "LEFT JOIN\n", 904 | " employees AS r\n", 905 | " ON l.building_name = r.building\n", 906 | "WHERE\n", 907 | " name IS NULL;`" 908 | ] 909 | }, 910 | { 911 | "cell_type": "markdown", 912 | "metadata": {}, 913 | "source": [ 914 | "### SQL Lesson 9: Queries with expressions " 915 | ] 916 | }, 917 | { 918 | "cell_type": "markdown", 919 | "metadata": {}, 920 | "source": [ 921 | "#### Task 1" 922 | ] 923 | }, 924 | { 925 | "cell_type": "markdown", 926 | "metadata": {}, 927 | "source": [ 928 | "List all movies and their combined sales in millions of dollars " 929 | ] 930 | }, 931 | { 932 | "cell_type": "markdown", 933 | "metadata": {}, 934 | "source": [ 935 | "`SELECT\n", 936 | " title,\n", 937 | " (domestic_sales + international_sales) / 1000000 AS combined_sales\n", 938 | "FROM \n", 939 | " movies\n", 940 | "INNER JOIN\n", 941 | " boxoffice\n", 942 | " ON movies.id = boxoffice.movie_id\n", 943 | "ORDER BY \n", 944 | " combined_sales DESC;`" 945 | ] 946 | }, 947 | { 948 | "cell_type": "markdown", 949 | "metadata": {}, 950 | "source": [ 951 | "#### Task 2" 952 | ] 953 | }, 954 | { 955 | "cell_type": "markdown", 956 | "metadata": {}, 957 | "source": [ 958 | "List all movies and their ratings in percent" 959 | ] 960 | }, 961 | { 962 | "cell_type": "markdown", 963 | "metadata": {}, 964 | "source": [ 965 | "`SELECT\n", 966 | " title,\n", 967 | " rating * 10 AS ratings_pct\n", 968 | "FROM \n", 969 | " movies\n", 970 | "INNER JOIN\n", 971 | " boxoffice\n", 972 | " ON movies.id = boxoffice.movie_id\n", 973 | "ORDER BY\n", 974 | " ratings_pct DESC;`" 975 | ] 976 | }, 977 | { 978 | "cell_type": "markdown", 979 | "metadata": {}, 980 | "source": [ 981 | "#### Task 3" 982 | ] 983 | }, 984 | { 985 | "cell_type": "markdown", 986 | "metadata": {}, 987 | "source": [ 988 | "List all movies that were released on even number years" 989 | ] 990 | }, 991 | { 992 | "cell_type": "markdown", 993 | "metadata": {}, 994 | "source": [ 995 | "`SELECT\n", 996 | " title,\n", 997 | " year\n", 998 | "FROM \n", 999 | " movies\n", 1000 | "WHERE\n", 1001 | " year % 2 = 0\n", 1002 | "ORDER BY \n", 1003 | " year ASC;`" 1004 | ] 1005 | }, 1006 | { 1007 | "cell_type": "markdown", 1008 | "metadata": {}, 1009 | "source": [ 1010 | "### SQL Lesson 10: Queries with aggregates (Pt. 1) " 1011 | ] 1012 | }, 1013 | { 1014 | "cell_type": "markdown", 1015 | "metadata": {}, 1016 | "source": [ 1017 | "#### Task 1" 1018 | ] 1019 | }, 1020 | { 1021 | "cell_type": "markdown", 1022 | "metadata": {}, 1023 | "source": [ 1024 | "Find the longest time that an employee has been at the studio " 1025 | ] 1026 | }, 1027 | { 1028 | "cell_type": "markdown", 1029 | "metadata": {}, 1030 | "source": [ 1031 | "`SELECT \n", 1032 | " MAX(years_employed) \n", 1033 | "FROM \n", 1034 | " employees;`" 1035 | ] 1036 | }, 1037 | { 1038 | "cell_type": "markdown", 1039 | "metadata": {}, 1040 | "source": [ 1041 | "#### Task 2" 1042 | ] 1043 | }, 1044 | { 1045 | "cell_type": "markdown", 1046 | "metadata": {}, 1047 | "source": [ 1048 | "For each role, find the average number of years employed by employees in that role " 1049 | ] 1050 | }, 1051 | { 1052 | "cell_type": "markdown", 1053 | "metadata": {}, 1054 | "source": [ 1055 | "`SELECT \n", 1056 | " role,\n", 1057 | " AVG(years_employed) AS avg_years_empl\n", 1058 | "FROM \n", 1059 | " employees\n", 1060 | "GROUP BY\n", 1061 | " role;`" 1062 | ] 1063 | }, 1064 | { 1065 | "cell_type": "markdown", 1066 | "metadata": {}, 1067 | "source": [ 1068 | "#### Task 3" 1069 | ] 1070 | }, 1071 | { 1072 | "cell_type": "markdown", 1073 | "metadata": {}, 1074 | "source": [ 1075 | "Find the total number of employee years worked in each building " 1076 | ] 1077 | }, 1078 | { 1079 | "cell_type": "markdown", 1080 | "metadata": {}, 1081 | "source": [ 1082 | "`SELECT \n", 1083 | " building,\n", 1084 | " SUM(years_employed)\n", 1085 | "FROM \n", 1086 | " employees\n", 1087 | "GROUP BY\n", 1088 | " building;`" 1089 | ] 1090 | }, 1091 | { 1092 | "cell_type": "markdown", 1093 | "metadata": {}, 1094 | "source": [ 1095 | "### SQL Lesson 11: Queries with aggregates (Pt. 2) " 1096 | ] 1097 | }, 1098 | { 1099 | "cell_type": "markdown", 1100 | "metadata": {}, 1101 | "source": [ 1102 | "#### Task 1" 1103 | ] 1104 | }, 1105 | { 1106 | "cell_type": "markdown", 1107 | "metadata": {}, 1108 | "source": [ 1109 | "Find the number of Artists in the studio (without a HAVING clause) " 1110 | ] 1111 | }, 1112 | { 1113 | "cell_type": "markdown", 1114 | "metadata": {}, 1115 | "source": [ 1116 | "`SELECT \n", 1117 | " COUNT(role)\n", 1118 | "FROM \n", 1119 | " employees\n", 1120 | "WHERE\n", 1121 | " role = 'Artist';`" 1122 | ] 1123 | }, 1124 | { 1125 | "cell_type": "markdown", 1126 | "metadata": {}, 1127 | "source": [ 1128 | "#### Task 2" 1129 | ] 1130 | }, 1131 | { 1132 | "cell_type": "markdown", 1133 | "metadata": {}, 1134 | "source": [ 1135 | "Find the number of Employees of each role in the studio " 1136 | ] 1137 | }, 1138 | { 1139 | "cell_type": "markdown", 1140 | "metadata": {}, 1141 | "source": [ 1142 | "`SELECT \n", 1143 | " role,\n", 1144 | " COUNT(role)\n", 1145 | "FROM \n", 1146 | " employees\n", 1147 | "GROUP BY\n", 1148 | " role;`" 1149 | ] 1150 | }, 1151 | { 1152 | "cell_type": "markdown", 1153 | "metadata": {}, 1154 | "source": [ 1155 | "#### Task 3" 1156 | ] 1157 | }, 1158 | { 1159 | "cell_type": "markdown", 1160 | "metadata": {}, 1161 | "source": [ 1162 | "Find the total number of years employed by all Engineers " 1163 | ] 1164 | }, 1165 | { 1166 | "cell_type": "markdown", 1167 | "metadata": {}, 1168 | "source": [ 1169 | "`SELECT \n", 1170 | " role,\n", 1171 | " SUM(years_employed)\n", 1172 | "FROM \n", 1173 | " employees\n", 1174 | "GROUP BY\n", 1175 | " role\n", 1176 | "HAVING\n", 1177 | " role = 'Engineer';`" 1178 | ] 1179 | }, 1180 | { 1181 | "cell_type": "markdown", 1182 | "metadata": {}, 1183 | "source": [ 1184 | "### SQL Lesson 12: Order of execution of a Query" 1185 | ] 1186 | }, 1187 | { 1188 | "cell_type": "markdown", 1189 | "metadata": {}, 1190 | "source": [ 1191 | "#### Task 1" 1192 | ] 1193 | }, 1194 | { 1195 | "cell_type": "markdown", 1196 | "metadata": {}, 1197 | "source": [ 1198 | "Find the number of movies each director has directed " 1199 | ] 1200 | }, 1201 | { 1202 | "cell_type": "markdown", 1203 | "metadata": {}, 1204 | "source": [ 1205 | "`SELECT\n", 1206 | " director,\n", 1207 | " COUNT(title) AS num_movies\n", 1208 | "FROM \n", 1209 | " movies\n", 1210 | "GROUP BY\n", 1211 | " director\n", 1212 | "ORDER BY\n", 1213 | " num_movies DESC;`" 1214 | ] 1215 | }, 1216 | { 1217 | "cell_type": "markdown", 1218 | "metadata": {}, 1219 | "source": [ 1220 | "#### Task 2" 1221 | ] 1222 | }, 1223 | { 1224 | "cell_type": "markdown", 1225 | "metadata": {}, 1226 | "source": [ 1227 | "Find the total domestic and international sales that can be attributed to each director " 1228 | ] 1229 | }, 1230 | { 1231 | "cell_type": "markdown", 1232 | "metadata": {}, 1233 | "source": [ 1234 | "`SELECT\n", 1235 | " director,\n", 1236 | " SUM(domestic_sales) + SUM(international_sales) AS total_revenue\n", 1237 | "FROM \n", 1238 | " movies AS l\n", 1239 | "LEFT JOIN boxoffice AS r\n", 1240 | " ON l.id = r.movie_id\n", 1241 | "GROUP BY\n", 1242 | " director\n", 1243 | "ORDER BY\n", 1244 | " total_revenue DESC;`" 1245 | ] 1246 | }, 1247 | { 1248 | "cell_type": "markdown", 1249 | "metadata": {}, 1250 | "source": [ 1251 | "### SQL Lesson 13: Inserting rows" 1252 | ] 1253 | }, 1254 | { 1255 | "cell_type": "markdown", 1256 | "metadata": {}, 1257 | "source": [ 1258 | "#### Task 1" 1259 | ] 1260 | }, 1261 | { 1262 | "cell_type": "markdown", 1263 | "metadata": {}, 1264 | "source": [ 1265 | "Add the studio's new production, Toy Story 4 to the list of movies (you can use any director)" 1266 | ] 1267 | }, 1268 | { 1269 | "cell_type": "markdown", 1270 | "metadata": {}, 1271 | "source": [ 1272 | "`INSERT INTO movies\n", 1273 | "(title, director, year, length_minutes)\n", 1274 | "VALUES ('Toy Story 4', 'Josh Cooley', 2019, 100);`" 1275 | ] 1276 | }, 1277 | { 1278 | "cell_type": "markdown", 1279 | "metadata": {}, 1280 | "source": [ 1281 | "#### Task 2" 1282 | ] 1283 | }, 1284 | { 1285 | "cell_type": "markdown", 1286 | "metadata": {}, 1287 | "source": [ 1288 | "Toy Story 4 has been released to critical acclaim! It had a rating of 8.7, and made 340 million domestically and 270 million internationally. Add the record to the BoxOffice table. " 1289 | ] 1290 | }, 1291 | { 1292 | "cell_type": "markdown", 1293 | "metadata": {}, 1294 | "source": [ 1295 | "`INSERT INTO boxoffice\n", 1296 | "(movie_id, rating, domestic_sales, international_sales)\n", 1297 | "VALUES (15, 8.7, 340000000, 270000000);`" 1298 | ] 1299 | }, 1300 | { 1301 | "cell_type": "markdown", 1302 | "metadata": {}, 1303 | "source": [ 1304 | "### SQL Lesson 14: Updating rows " 1305 | ] 1306 | }, 1307 | { 1308 | "cell_type": "markdown", 1309 | "metadata": {}, 1310 | "source": [ 1311 | "#### Task 1" 1312 | ] 1313 | }, 1314 | { 1315 | "cell_type": "markdown", 1316 | "metadata": {}, 1317 | "source": [ 1318 | "The director for A Bug's Life is incorrect, it was actually directed by John Lasseter" 1319 | ] 1320 | }, 1321 | { 1322 | "cell_type": "markdown", 1323 | "metadata": {}, 1324 | "source": [ 1325 | "`UPDATE \n", 1326 | " movies\n", 1327 | "SET\n", 1328 | " director = 'John Lasseter'\n", 1329 | "WHERE\n", 1330 | " title = \"A Bug's Life\";`" 1331 | ] 1332 | }, 1333 | { 1334 | "cell_type": "markdown", 1335 | "metadata": {}, 1336 | "source": [ 1337 | "#### Task 2" 1338 | ] 1339 | }, 1340 | { 1341 | "cell_type": "markdown", 1342 | "metadata": {}, 1343 | "source": [ 1344 | "The year that Toy Story 2 was released is incorrect, it was actually released in 1999" 1345 | ] 1346 | }, 1347 | { 1348 | "cell_type": "markdown", 1349 | "metadata": {}, 1350 | "source": [ 1351 | "`UPDATE\n", 1352 | " movies\n", 1353 | "SET\n", 1354 | " year = 1999\n", 1355 | "WHERE\n", 1356 | " title = 'Toy Story 2';`" 1357 | ] 1358 | }, 1359 | { 1360 | "cell_type": "markdown", 1361 | "metadata": {}, 1362 | "source": [ 1363 | "#### Task 3" 1364 | ] 1365 | }, 1366 | { 1367 | "cell_type": "markdown", 1368 | "metadata": {}, 1369 | "source": [ 1370 | "Both the title and director for Toy Story 8 is incorrect! The title should be \"Toy Story 3\" and it was directed by Lee Unkrich" 1371 | ] 1372 | }, 1373 | { 1374 | "cell_type": "markdown", 1375 | "metadata": {}, 1376 | "source": [ 1377 | "`UPDATE\n", 1378 | " movies\n", 1379 | "SET\n", 1380 | " title = 'Toy Story 3',\n", 1381 | " director = 'Lee Unkrich'\n", 1382 | "WHERE \n", 1383 | " title = 'Toy Story 8';`" 1384 | ] 1385 | }, 1386 | { 1387 | "cell_type": "markdown", 1388 | "metadata": {}, 1389 | "source": [ 1390 | "### SQL Lesson 15: Deleting rows " 1391 | ] 1392 | }, 1393 | { 1394 | "cell_type": "markdown", 1395 | "metadata": {}, 1396 | "source": [ 1397 | "#### Task 1" 1398 | ] 1399 | }, 1400 | { 1401 | "cell_type": "markdown", 1402 | "metadata": {}, 1403 | "source": [ 1404 | "This database is getting too big, lets remove all movies that were released before 2005" 1405 | ] 1406 | }, 1407 | { 1408 | "cell_type": "markdown", 1409 | "metadata": {}, 1410 | "source": [ 1411 | "`DELETE FROM \n", 1412 | " movies\n", 1413 | "WHERE \n", 1414 | " year < 2005;`" 1415 | ] 1416 | }, 1417 | { 1418 | "cell_type": "markdown", 1419 | "metadata": {}, 1420 | "source": [ 1421 | "#### Task 2" 1422 | ] 1423 | }, 1424 | { 1425 | "cell_type": "markdown", 1426 | "metadata": {}, 1427 | "source": [ 1428 | "Andrew Stanton has also left the studio, so please remove all movies directed by him" 1429 | ] 1430 | }, 1431 | { 1432 | "cell_type": "markdown", 1433 | "metadata": {}, 1434 | "source": [ 1435 | "`DELETE FROM\n", 1436 | " movies\n", 1437 | "WHERE\n", 1438 | " director = 'Andrew Stanton';`" 1439 | ] 1440 | }, 1441 | { 1442 | "cell_type": "markdown", 1443 | "metadata": {}, 1444 | "source": [ 1445 | "### SQL Lesson 16: Creating tables " 1446 | ] 1447 | }, 1448 | { 1449 | "cell_type": "markdown", 1450 | "metadata": {}, 1451 | "source": [ 1452 | "#### Task 1" 1453 | ] 1454 | }, 1455 | { 1456 | "cell_type": "markdown", 1457 | "metadata": {}, 1458 | "source": [ 1459 | "Create a new table named Database with the following columns:\n", 1460 | "* Name A string (text) describing the name of the database\n", 1461 | "* Version A number (floating point) of the latest version of this database\n", 1462 | "* Download_count An integer count of the number of times this database was downloaded \n", 1463 | "\n", 1464 | "This table has no constraints. " 1465 | ] 1466 | }, 1467 | { 1468 | "cell_type": "markdown", 1469 | "metadata": {}, 1470 | "source": [ 1471 | "`CREATE TABLE IF NOT EXISTS Database (\n", 1472 | " Name text,\n", 1473 | " Version float,\n", 1474 | " Download_count int\n", 1475 | ");`" 1476 | ] 1477 | }, 1478 | { 1479 | "cell_type": "markdown", 1480 | "metadata": {}, 1481 | "source": [ 1482 | "### SQL Lesson 17: Altering tables " 1483 | ] 1484 | }, 1485 | { 1486 | "cell_type": "markdown", 1487 | "metadata": {}, 1488 | "source": [ 1489 | "#### Task 1" 1490 | ] 1491 | }, 1492 | { 1493 | "cell_type": "markdown", 1494 | "metadata": {}, 1495 | "source": [ 1496 | "Add a column named Aspect_ratio with a FLOAT data type to store the aspect-ratio each movie was released in." 1497 | ] 1498 | }, 1499 | { 1500 | "cell_type": "markdown", 1501 | "metadata": {}, 1502 | "source": [ 1503 | "`ALTER TABLE\n", 1504 | " movies\n", 1505 | "ADD \n", 1506 | " Aspect_ratio float;`" 1507 | ] 1508 | }, 1509 | { 1510 | "cell_type": "markdown", 1511 | "metadata": {}, 1512 | "source": [ 1513 | "#### Task 2" 1514 | ] 1515 | }, 1516 | { 1517 | "cell_type": "markdown", 1518 | "metadata": {}, 1519 | "source": [ 1520 | "Add another column named Language with a TEXT data type to store the language that the movie was released in. Ensure that the default for this language is English. " 1521 | ] 1522 | }, 1523 | { 1524 | "cell_type": "markdown", 1525 | "metadata": {}, 1526 | "source": [ 1527 | "`ALTER TABLE\n", 1528 | " movies\n", 1529 | "ADD\n", 1530 | " Language text DEFAULT 'English';`" 1531 | ] 1532 | }, 1533 | { 1534 | "cell_type": "markdown", 1535 | "metadata": {}, 1536 | "source": [ 1537 | "#### Task 3" 1538 | ] 1539 | }, 1540 | { 1541 | "cell_type": "markdown", 1542 | "metadata": {}, 1543 | "source": [ 1544 | "Altering table to remove column(s)." 1545 | ] 1546 | }, 1547 | { 1548 | "cell_type": "markdown", 1549 | "metadata": {}, 1550 | "source": [ 1551 | "`ALTER TABLE \n", 1552 | " mytable\n", 1553 | "DROP \n", 1554 | " column_to_be_deleted;`" 1555 | ] 1556 | }, 1557 | { 1558 | "cell_type": "markdown", 1559 | "metadata": {}, 1560 | "source": [ 1561 | "#### Task 4" 1562 | ] 1563 | }, 1564 | { 1565 | "cell_type": "markdown", 1566 | "metadata": {}, 1567 | "source": [ 1568 | "Rename the table itself." 1569 | ] 1570 | }, 1571 | { 1572 | "cell_type": "markdown", 1573 | "metadata": {}, 1574 | "source": [ 1575 | "`ALTER TABLE \n", 1576 | " mytable\n", 1577 | "RENAME TO \n", 1578 | " new_table_name;`" 1579 | ] 1580 | }, 1581 | { 1582 | "cell_type": "markdown", 1583 | "metadata": {}, 1584 | "source": [ 1585 | "### SQL Lesson 18: Dropping tables " 1586 | ] 1587 | }, 1588 | { 1589 | "cell_type": "markdown", 1590 | "metadata": {}, 1591 | "source": [ 1592 | "#### Task 1" 1593 | ] 1594 | }, 1595 | { 1596 | "cell_type": "markdown", 1597 | "metadata": {}, 1598 | "source": [ 1599 | "We've sadly reached the end of our lessons, lets clean up by removing the Movies table " 1600 | ] 1601 | }, 1602 | { 1603 | "cell_type": "markdown", 1604 | "metadata": {}, 1605 | "source": [ 1606 | "`DROP TABLE IF EXISTS movies;`" 1607 | ] 1608 | }, 1609 | { 1610 | "cell_type": "markdown", 1611 | "metadata": {}, 1612 | "source": [ 1613 | "#### Task 2" 1614 | ] 1615 | }, 1616 | { 1617 | "cell_type": "markdown", 1618 | "metadata": {}, 1619 | "source": [ 1620 | "And drop the BoxOffice table as well " 1621 | ] 1622 | }, 1623 | { 1624 | "cell_type": "markdown", 1625 | "metadata": {}, 1626 | "source": [ 1627 | "`DROP TABLE IF EXISTS boxoffice;`" 1628 | ] 1629 | }, 1630 | { 1631 | "cell_type": "markdown", 1632 | "metadata": {}, 1633 | "source": [ 1634 | "### SQL Lesson 19: Subqueries " 1635 | ] 1636 | }, 1637 | { 1638 | "cell_type": "markdown", 1639 | "metadata": {}, 1640 | "source": [ 1641 | "#### Task 1" 1642 | ] 1643 | }, 1644 | { 1645 | "cell_type": "markdown", 1646 | "metadata": {}, 1647 | "source": [ 1648 | "Find out which of your Associates are costing the company more than the average revenue brought per Associate. " 1649 | ] 1650 | }, 1651 | { 1652 | "cell_type": "markdown", 1653 | "metadata": {}, 1654 | "source": [ 1655 | "`\n", 1656 | "SELECT *\n", 1657 | "FROM \n", 1658 | " sales_associates\n", 1659 | "WHERE \n", 1660 | " salary > (\n", 1661 | " SELECT \n", 1662 | " AVG(revenue_generated)\n", 1663 | " FROM \n", 1664 | " sales_associates\n", 1665 | " );\n", 1666 | "`" 1667 | ] 1668 | }, 1669 | { 1670 | "cell_type": "markdown", 1671 | "metadata": {}, 1672 | "source": [ 1673 | "#### Task 2" 1674 | ] 1675 | }, 1676 | { 1677 | "cell_type": "markdown", 1678 | "metadata": {}, 1679 | "source": [ 1680 | "For each employee, you would need to calculate their cost relative to the average revenue generated by all people in their department." 1681 | ] 1682 | }, 1683 | { 1684 | "cell_type": "markdown", 1685 | "metadata": {}, 1686 | "source": [ 1687 | "`SELECT * \n", 1688 | "FROM \n", 1689 | " employees \n", 1690 | "WHERE \n", 1691 | " salary > (\n", 1692 | " SELECT \n", 1693 | " AVG(revenue_generated) \n", 1694 | " FROM \n", 1695 | " employees AS dept_employees \n", 1696 | " WHERE \n", 1697 | " dept_employees.department = employees.department\n", 1698 | " );`" 1699 | ] 1700 | }, 1701 | { 1702 | "cell_type": "markdown", 1703 | "metadata": {}, 1704 | "source": [ 1705 | "### SQL Extra: Unions, Intersections & Exceptions " 1706 | ] 1707 | }, 1708 | { 1709 | "cell_type": "markdown", 1710 | "metadata": {}, 1711 | "source": [ 1712 | "The **UNION** and **UNION ALL** operator allows you to append the results of one query to another assuming that they have the same column count, order and data type. If you use the UNION without the ALL, duplicate rows between the tables will be removed from the result." 1713 | ] 1714 | }, 1715 | { 1716 | "cell_type": "markdown", 1717 | "metadata": {}, 1718 | "source": [ 1719 | "The **INTERSECT** operator will ensure that only rows that are identical in both result sets are returned." 1720 | ] 1721 | }, 1722 | { 1723 | "cell_type": "markdown", 1724 | "metadata": {}, 1725 | "source": [ 1726 | "The **EXCEPT** operator will ensure that only rows in the first result set that aren't in the second are returned." 1727 | ] 1728 | }, 1729 | { 1730 | "cell_type": "markdown", 1731 | "metadata": {}, 1732 | "source": [ 1733 | "Both **INTERSECT** and **EXCEPT** also discard duplicate rows after their respective operations, though some databases also support **INTERSECT ALL** and **EXCEPT ALL** to allow duplicates to be retained and returned." 1734 | ] 1735 | }, 1736 | { 1737 | "cell_type": "markdown", 1738 | "metadata": {}, 1739 | "source": [ 1740 | "-- Select query with set operators \n", 1741 | "\n", 1742 | "`\n", 1743 | "SELECT column, another_column\n", 1744 | " FROM mytable\n", 1745 | "UNION / UNION ALL / INTERSECT / EXCEPT\n", 1746 | "SELECT other_column, yet_another_column\n", 1747 | " FROM another_table\n", 1748 | "ORDER BY column DESC\n", 1749 | "LIMIT n;\n", 1750 | "`" 1751 | ] 1752 | } 1753 | ], 1754 | "metadata": { 1755 | "kernelspec": { 1756 | "display_name": "Python 3", 1757 | "language": "python", 1758 | "name": "python3" 1759 | }, 1760 | "language_info": { 1761 | "codemirror_mode": { 1762 | "name": "ipython", 1763 | "version": 3 1764 | }, 1765 | "file_extension": ".py", 1766 | "mimetype": "text/x-python", 1767 | "name": "python", 1768 | "nbconvert_exporter": "python", 1769 | "pygments_lexer": "ipython3", 1770 | "version": "3.7.6" 1771 | } 1772 | }, 1773 | "nbformat": 4, 1774 | "nbformat_minor": 4 1775 | } 1776 | -------------------------------------------------------------------------------- /7_oracle_db_foundations/210621_oracle_sql_foundations_certificate.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nktnlx/learning_SQL/796cb7a903c42f0a79a550cb915f4869ccd67ce7/7_oracle_db_foundations/210621_oracle_sql_foundations_certificate.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Hi there! 2 | 3 | 4 | I'm learning to code, interested in data analytics and data science, aiming to be hired by mid of 2021. 5 | This repository is for SQL exercises done by me while learning SQL on various platforms and courses. 6 | 7 | List of course: 8 | 1. **Learn SQL** -- on [codecademy.com](https://www.codecademy.com/learn/learn-sql) 9 | 2. **SQL exercises** -- on [sql-ex.ru](https://www.sql-ex.ru/?Lang=1) 10 | 3. **SQL course** -- on [stepik.org](https://stepik.org/course/63054/syllabus). **NB!** *The course is in Russian*. 11 | 4. **SQL data analysis course** -- data analysis course by [karpov.courses](https://karpov.courses/) 12 | 5. **SQL module** -- from data analyst course by [Yandex Praktikum](https://praktikum.yandex.ru/data-analyst/) 13 | 6. **SQL Bolt** -- a series of interactive lessons and exercises designed to help you quickly learn SQL right in your browser. [SQL Bolt](https://sqlbolt.com/) 14 | 7. **Databases for Developers: SQL Foundations** -- 12-part boot camp that helps to get started with Oracle Database and SQL. [SQL Foundations](https://devgym.oracle.com/pls/apex/dg/class/databases-for-developers-foundations.html) 15 | 16 | 17 | 18 | It's extremely important to write a readable code. 19 | Please, read and follow SQL style guide. I personally enjoyed the one by Simon Holywell that you can find here: [https://www.sqlstyle.guide/](https://www.sqlstyle.guide/) 20 | Also I find the web-site below being very helpful. It automatically formats your SQL queries returning a beautiful and clear code. [https://codebeautify.org/sqlformatter](https://codebeautify.org/sqlformatter) 21 | Enjoy using these tools! 22 | 23 | 24 | Hope this repo will help you to assess my SQL skills or will be just fun for you to look through. 25 | 26 | 27 | 28 | -------------------------------------------- 29 | Fell free to contact me via nktn.lx@gmal.com 30 | Follow me on twitter: @nktn_lx 31 | And here on github: github.com/nktnlx --------------------------------------------------------------------------------