-
-
Notifications
You must be signed in to change notification settings - Fork 15
Add basic SELECT concept/exercise #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
0cb4290
5dc7c48
3027614
14dc57c
1e2f7ee
dfa3570
1ad14e9
8ea434d
b2e2313
42e44c8
7285bb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "blurb": "The SQL SELECT statement is the mechanism for querying a database.", | ||
| "authors": ["blackk-foxx"], | ||
| "contributors": ["blackk-foxx"] | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,142 @@ | ||||||||||||||||
| # The SELECT statement | ||||||||||||||||
|
|
||||||||||||||||
| In SQL, a `SELECT` statement allows you to retrieve data from a database. | ||||||||||||||||
| The result of a `SELECT` statement is a result set -- some number of values arranged in rows and columns, where each column is a particular attribute of the data and each row is a set of values for each column. | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| Roughly, you can think of the set of columns as a data structure, where each column represents a field of the data structure, and each row represents a concrete instance of the data structure. | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| With a `SELECT` statement, you can specify the data you want and optionally transform, filter, and/or modify the shape of the output data. | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| ## The basics | ||||||||||||||||
|
|
||||||||||||||||
| The anatomy of a basic `SELECT` statement is as follows: | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| ```sql | ||||||||||||||||
| SELECT <columns> | ||||||||||||||||
| FROM <source> | ||||||||||||||||
| WHERE <criteria>; | ||||||||||||||||
IsaacG marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| Note that the line breaks are not required; any spacing will suffice to separate the different | ||||||||||||||||
| clauses (i.e. parts) of the `SELECT` statement. | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| Immediately following the `SELECT` keyword is a list of the columns that you want in the result. | ||||||||||||||||
| The `FROM` clause identifies the source of the data, which is typically a table in the database. | ||||||||||||||||
| The `WHERE` clause filters the output data by one or more criteria. | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| For example, consider a database with a table named `inventory` containing the following data: | ||||||||||||||||
|
Comment on lines
+33
to
+35
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied here and elsewhere. |
||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| | upc | category | supplier | brand | product_name | weight | stock | | ||||||||||||||||
| |--------------|------------|----------------|-------------|----------------------------------|--------|-------| | ||||||||||||||||
| | 812345670019 | Cookware | KitchenCo | HearthStone | 10" Non-Stick Skillet | 1.8 | 42 | | ||||||||||||||||
| | 845678120334 | Utensils | HomePro Supply | PrepMaster | Stainless Steel Ladle | 0.4 | 120 | | ||||||||||||||||
| | 899001234556 | Appliances | Culinary Depot | QuickBrew | Single-Serve Coffee Maker | 4.2 | 18 | | ||||||||||||||||
| | 823450987112 | Cookware | KitchenCo | FreshKeep | 12-Piece Glass Container Set | 6 | 33 | | ||||||||||||||||
| | 867530900221 | Utensils | HomePro Supply | EdgeCraft | 8' Chef's Knife | 0.6 | 27 | | ||||||||||||||||
| | 880012349876 | Appliances | Culinary Depot | HeatWave | Compact Toaster Oven | 7.5 | 14 | | ||||||||||||||||
| | 833221109443 | Utensils | HomePro Supply | PureScrub | Heavy-Duty Kitchen Sponge (3-pk) | 0.2 | 200 | | ||||||||||||||||
| | 899998877665 | Cookware | KitchenCo | IronCraft | Cast-Iron Grill Pan | 5.3 | 21 | | ||||||||||||||||
| | 844110220987 | Appliances | Culinary Depot | BlendPro | High-Speed Personal Blender | 3.1 | 16 | | ||||||||||||||||
|
|
||||||||||||||||
| If we want to simply retrieve all of the data from the table, we could run the following query: | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| ```sql | ||||||||||||||||
| SELECT * FROM inventory; | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| Result: | ||||||||||||||||
|
|
||||||||||||||||
| ``` | ||||||||||||||||
| upc category supplier brand product_name weight stock | ||||||||||||||||
| ------------ ---------- -------------- ----------- -------------------------------- ------ ----- | ||||||||||||||||
| 812345670019 Cookware KitchenCo HearthStone 10" Non-Stick Skillet 1.8 42 | ||||||||||||||||
| 845678120334 Utensils HomePro Supply PrepMaster Stainless Steel Ladle 0.4 120 | ||||||||||||||||
| 899001234556 Appliances Culinary Depot QuickBrew Single-Serve Coffee Maker 4.2 18 | ||||||||||||||||
| 823450987112 Cookware KitchenCo FreshKeep 12-Piece Glass Container Set 6 33 | ||||||||||||||||
| 867530900221 Utensils HomePro Supply EdgeCraft 8' Chef's Knife 0.6 27 | ||||||||||||||||
| 880012349876 Appliances Culinary Depot HeatWave Compact Toaster Oven 7.5 14 | ||||||||||||||||
| 833221109443 Utensils HomePro Supply PureScrub Heavy-Duty Kitchen Sponge (3-pk) 0.2 200 | ||||||||||||||||
| 899998877665 Cookware KitchenCo IronCraft Cast-Iron Grill Pan 5.3 21 | ||||||||||||||||
| 844110220987 Appliances Culinary Depot BlendPro High-Speed Personal Blender 3.1 16 | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| But if we only want the category and product_name values, we could specify those columns: | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| ```sql | ||||||||||||||||
| SELECT category, product_name FROM inventory; | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| Result: | ||||||||||||||||
|
|
||||||||||||||||
| ``` | ||||||||||||||||
| category product_name | ||||||||||||||||
| ---------- -------------------------------- | ||||||||||||||||
| Cookware 10" Non-Stick Skillet | ||||||||||||||||
| Utensils Stainless Steel Ladle | ||||||||||||||||
| Appliances Single-Serve Coffee Maker | ||||||||||||||||
| Cookware 12-Piece Glass Container Set | ||||||||||||||||
| Utensils 8' Chef's Knife | ||||||||||||||||
| Appliances Compact Toaster Oven | ||||||||||||||||
| Utensils Heavy-Duty Kitchen Sponge (3-pk) | ||||||||||||||||
| Cookware Cast-Iron Grill Pan | ||||||||||||||||
| Appliances High-Speed Personal Blender | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| ~~~~exercism/note | ||||||||||||||||
| The `FROM` clause is optional. | ||||||||||||||||
| A statement like `SELECT "Hello, world.";` is perfectly valid, and will generate the following result: | ||||||||||||||||
|
|
||||||||||||||||
| ``` | ||||||||||||||||
| "Hello, world." | ||||||||||||||||
| --------------- | ||||||||||||||||
| Hello, world. | ||||||||||||||||
| ``` | ||||||||||||||||
| ~~~~ | ||||||||||||||||
|
|
||||||||||||||||
| ## Filtering data with the WHERE clause | ||||||||||||||||
|
|
||||||||||||||||
| The `WHERE` clause allows you to filter the data retrieved by a `SELECT` statement. | ||||||||||||||||
| For example, if we only want Appliances: | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| ```sql | ||||||||||||||||
| SELECT * FROM inventory | ||||||||||||||||
| WHERE category = "Appliances"; | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| Result: | ||||||||||||||||
| ``` | ||||||||||||||||
| upc category supplier brand product_name weight stock | ||||||||||||||||
| ------------ ---------- -------------- --------- --------------------------- ------ ----- | ||||||||||||||||
| 899001234556 Appliances Culinary Depot QuickBrew Single-Serve Coffee Maker 4.2 18 | ||||||||||||||||
| 880012349876 Appliances Culinary Depot HeatWave Compact Toaster Oven 7.5 14 | ||||||||||||||||
| 844110220987 Appliances Culinary Depot BlendPro High-Speed Personal Blender 3.1 16 | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| Or maybe we only want data where the weight is between 2.0 and 6.0: | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| ```sql | ||||||||||||||||
| SELECT * FROM inventory | ||||||||||||||||
| WHERE weight BETWEEN 2.0 AND 6.0; | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| Result: | ||||||||||||||||
| ``` | ||||||||||||||||
| upc category supplier brand product_name weight stock | ||||||||||||||||
| ------------ ---------- -------------- --------- ---------------------------- ------ ----- | ||||||||||||||||
| 899001234556 Appliances Culinary Depot QuickBrew Single-Serve Coffee Maker 4.2 18 | ||||||||||||||||
| 823450987112 Cookware KitchenCo FreshKeep 12-Piece Glass Container Set 6 33 | ||||||||||||||||
| 899998877665 Cookware KitchenCo IronCraft Cast-Iron Grill Pan 5.3 21 | ||||||||||||||||
| 844110220987 Appliances Culinary Depot BlendPro High-Speed Personal Blender 3.1 16 | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| In addition to `=` and `BETWEEN...AND`, the `WHERE` clause supports a wide range of expressions, including comparison (`<`, `<=`, `>`, `>=`), pattern matching (`LIKE`, `GLOB`, `REGEXP`, `MATCH`), and checking for membership in a list (`IN`, `NOT IN`). | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of the pattern matching operators are probably specific to sqlite. Did you want to stay more db-neutral?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really have a strong opinion here. I guess it would be nice to stay as generic as possible, but the concept docs do provide links to SQLite specific docs.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the SQLite track, not a generic SQL track. I think SQLite specific things are completely fine, and probably even preferred. |
||||||||||||||||
| See [SQL Language Expressions](sql-expr) for the complete documentation. | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| [sql-expr]: https://sqlite.org/lang_expr.html | ||||||||||||||||
blackk-foxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the same as the about.md?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, except for the initial heading. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # The SELECT statement | ||
|
|
||
| In SQL, a `SELECT` statement allows you to retrieve data from a database. | ||
| The result of a `SELECT` statement is a result set -- some number of values arranged in rows and columns, where each column is a particular attribute of the data and each row is a set of values for each column. | ||
|
|
||
| Roughly, you can think of the set of columns as a data structure, where each column represents a field of the data structure, and each row represents a concrete instance of the data structure. | ||
|
|
||
| With a `SELECT` statement, you can specify the data you want and optionally transform, filter, and/or modify the shape of the output data. | ||
|
|
||
|
|
||
| ## The basics | ||
|
|
||
| The anatomy of a basic `SELECT` statement is as follows: | ||
|
|
||
| ```sql | ||
| SELECT <columns> | ||
| FROM <source> | ||
| WHERE <criteria>; | ||
| ``` | ||
|
|
||
| Note that the line breaks are not required; any spacing will suffice to separate the different | ||
| clauses (i.e. parts) of the `SELECT` statement. | ||
|
|
||
| Immediately following the `SELECT` keyword is a list of the columns that you want in the result. | ||
| The `FROM` clause identifies the source of the data, which is typically a table in the database. | ||
| The `WHERE` clause filters the output data by one or more criteria. | ||
|
|
||
|
|
||
| For example, consider a database with a table named `inventory` containing the following data: | ||
|
|
||
|
|
||
| | upc | category | supplier | brand | product_name | weight | stock | | ||
| |--------------|------------|----------------|-------------|----------------------------------|--------|-------| | ||
| | 812345670019 | Cookware | KitchenCo | HearthStone | 10" Non-Stick Skillet | 1.8 | 42 | | ||
| | 845678120334 | Utensils | HomePro Supply | PrepMaster | Stainless Steel Ladle | 0.4 | 120 | | ||
| | 899001234556 | Appliances | Culinary Depot | QuickBrew | Single-Serve Coffee Maker | 4.2 | 18 | | ||
| | 823450987112 | Cookware | KitchenCo | FreshKeep | 12-Piece Glass Container Set | 6 | 33 | | ||
| | 867530900221 | Utensils | HomePro Supply | EdgeCraft | 8' Chef's Knife | 0.6 | 27 | | ||
| | 880012349876 | Appliances | Culinary Depot | HeatWave | Compact Toaster Oven | 7.5 | 14 | | ||
| | 833221109443 | Utensils | HomePro Supply | PureScrub | Heavy-Duty Kitchen Sponge (3-pk) | 0.2 | 200 | | ||
| | 899998877665 | Cookware | KitchenCo | IronCraft | Cast-Iron Grill Pan | 5.3 | 21 | | ||
| | 844110220987 | Appliances | Culinary Depot | BlendPro | High-Speed Personal Blender | 3.1 | 16 | | ||
|
|
||
| If we want to simply retrieve all of the data from the table, we could run the following query: | ||
|
|
||
| ```sql | ||
| SELECT * FROM inventory; | ||
| ``` | ||
|
|
||
| Result: | ||
|
|
||
| ``` | ||
| upc category supplier brand product_name weight stock | ||
| ------------ ---------- -------------- ----------- -------------------------------- ------ ----- | ||
| 812345670019 Cookware KitchenCo HearthStone 10" Non-Stick Skillet 1.8 42 | ||
| 845678120334 Utensils HomePro Supply PrepMaster Stainless Steel Ladle 0.4 120 | ||
| 899001234556 Appliances Culinary Depot QuickBrew Single-Serve Coffee Maker 4.2 18 | ||
| 823450987112 Cookware KitchenCo FreshKeep 12-Piece Glass Container Set 6 33 | ||
| 867530900221 Utensils HomePro Supply EdgeCraft 8' Chef's Knife 0.6 27 | ||
| 880012349876 Appliances Culinary Depot HeatWave Compact Toaster Oven 7.5 14 | ||
| 833221109443 Utensils HomePro Supply PureScrub Heavy-Duty Kitchen Sponge (3-pk) 0.2 200 | ||
| 899998877665 Cookware KitchenCo IronCraft Cast-Iron Grill Pan 5.3 21 | ||
| 844110220987 Appliances Culinary Depot BlendPro High-Speed Personal Blender 3.1 16 | ||
| ``` | ||
|
|
||
| But if we only want the category and product_name values, we could specify those columns: | ||
|
|
||
| ```sql | ||
| SELECT category, product_name FROM inventory; | ||
| ``` | ||
|
|
||
| Result: | ||
|
|
||
| ``` | ||
| category product_name | ||
| ---------- -------------------------------- | ||
| Cookware 10" Non-Stick Skillet | ||
| Utensils Stainless Steel Ladle | ||
| Appliances Single-Serve Coffee Maker | ||
| Cookware 12-Piece Glass Container Set | ||
| Utensils 8' Chef's Knife | ||
| Appliances Compact Toaster Oven | ||
| Utensils Heavy-Duty Kitchen Sponge (3-pk) | ||
| Cookware Cast-Iron Grill Pan | ||
| Appliances High-Speed Personal Blender | ||
| ``` | ||
|
|
||
| ~~~~exercism/note | ||
| The `FROM` clause is optional. | ||
| A statement like `SELECT "Hello, world.";` is perfectly valid, and will generate the following result: | ||
|
|
||
| ``` | ||
| "Hello, world." | ||
| --------------- | ||
| Hello, world. | ||
| ``` | ||
| ~~~~ | ||
|
|
||
| ## Filtering data with the WHERE clause | ||
|
|
||
| The `WHERE` clause allows you to filter the data retrieved by a `SELECT` statement. | ||
| For example, if we only want Appliances: | ||
|
|
||
| ```sql | ||
| SELECT * FROM inventory | ||
| WHERE category = "Appliances"; | ||
| ``` | ||
|
|
||
| Result: | ||
| ``` | ||
| upc category supplier brand product_name weight stock | ||
| ------------ ---------- -------------- --------- --------------------------- ------ ----- | ||
| 899001234556 Appliances Culinary Depot QuickBrew Single-Serve Coffee Maker 4.2 18 | ||
| 880012349876 Appliances Culinary Depot HeatWave Compact Toaster Oven 7.5 14 | ||
| 844110220987 Appliances Culinary Depot BlendPro High-Speed Personal Blender 3.1 16 | ||
| ``` | ||
|
|
||
|
|
||
| Or maybe we only want data where the weight is between 2.0 and 6.0: | ||
|
|
||
| ```sql | ||
| SELECT * FROM inventory | ||
| WHERE weight BETWEEN 2.0 AND 6.0; | ||
| ``` | ||
|
|
||
|
|
||
| Result: | ||
| ``` | ||
| upc category supplier brand product_name weight stock | ||
| ------------ ---------- -------------- --------- ---------------------------- ------ ----- | ||
| 899001234556 Appliances Culinary Depot QuickBrew Single-Serve Coffee Maker 4.2 18 | ||
| 823450987112 Cookware KitchenCo FreshKeep 12-Piece Glass Container Set 6 33 | ||
| 899998877665 Cookware KitchenCo IronCraft Cast-Iron Grill Pan 5.3 21 | ||
| 844110220987 Appliances Culinary Depot BlendPro High-Speed Personal Blender 3.1 16 | ||
| ``` | ||
|
|
||
|
|
||
| In addition to `=` and `BETWEEN...AND`, the `WHERE` clause supports a wide range of expressions, including comparison (`<`, `<=`, `>`, `>=`), pattern matching (`LIKE`, `GLOB`, `REGEXP`, `MATCH`), and checking for membership in a list (`IN`, `NOT IN`). | ||
| See [SQL Language Expressions](sql-expr) for the complete documentation. | ||
|
|
||
|
|
||
| [sql-expr]: https://sqlite.org/lang_expr.html |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [ | ||
| { | ||
| "url": "https://sqlite.org/lang_select.html#simple_select_processing", | ||
| "description": "Simple SELECT processing details" | ||
| }, | ||
| { | ||
| "url": "https://sqlite.org/lang_select.html", | ||
| "description": "Complete SELECT documentation for SQLite" | ||
| } | ||
| ] |
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Instructions | ||
|
|
||
| You are a data analyst at a firm that analyzes weather data in the Pacific Northwest. You will be working with a database containing a `weather_readings` table with the following columns: | ||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| * `date`: The date on which the sample was collected. | ||
| * `location`: The name of the city in which the sample was collected. | ||
| * `temperature`: The temperature, in degrees Fahrenheit. | ||
| * `humidity`: The relative humidity, as a percentage value. | ||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| In each of the following tasks, your job is to gather the specified result set, by using a `SELECT` statement. | ||
| Note that in each case, the result set will be stored in a new table -- one table for each task -- via a pre-written `CREATE TABLE` statement. | ||
| Your job for each task is to add the appropriate `SELECT` statement within the existing `CREATE TABLE` statement. | ||
|
|
||
| ## 1. Gather all of the data | ||
|
||
|
|
||
| Gather all of the data from the `weather_readings` table. | ||
|
|
||
| ## 2. Gather only the location and temperature data | ||
|
|
||
| Gather only the location and temperature values from the `weather_readings` table. | ||
|
|
||
| ## 3. Gather only data for Seattle | ||
|
|
||
| Gather only the data for the Seattle location. | ||
|
|
||
| ## 4. Gather only the data with humidity in a specific range | ||
|
|
||
| Gather only the data where the humidity is between 60% and 70%. | ||
|
||
Uh oh!
There was an error while loading. Please reload this page.