-
-
Notifications
You must be signed in to change notification settings - Fork 14
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 all commits
0cb4290
5dc7c48
3027614
14dc57c
1e2f7ee
dfa3570
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,138 @@ | ||||||
| # 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. | ||||||
| A result set contains values arranged in rows and columns. | ||||||
| Each column is a particular attribute of the data. | ||||||
| Each row contains a value for every column. | ||||||
|
|
||||||
| One way to think about it is that a set of columns represents a data structure, where each column represents a field of the data structure, and each row represents an instance of the data structure. | ||||||
|
|
||||||
| With a `SELECT` statement, you can specify the data you want to retrieve. | ||||||
| You can also optionally transform, filter, and/or modify the shape of the output data. | ||||||
|
|
||||||
| ## The basics | ||||||
|
|
||||||
| The anatomy of a basic `SELECT` statement is as follows: | ||||||
|
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. Why not
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 a complete sentence and complete idea as is.
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 agree it's a complete sentence, but I don't agree that it's a complete idea. I think the information following "as follows" is essential to understanding the sentence, hence the colon. Anyway, if there is an existing convention to use a period after "as follows," I will follow it. Otherwise, I would prefer to use a colon. |
||||||
|
|
||||||
| ```sql | ||||||
| SELECT <columns> | ||||||
| FROM <source> | ||||||
| WHERE <criteria>; | ||||||
|
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. If this is the first time someone is seeing a SELECT, it would be good to make it clear that the
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 in both SELECT <columns>
[FROM <source>
[WHERE <criteria>]];Not sure whether the square brackets would help or harm here... |
||||||
| ``` | ||||||
|
|
||||||
| Note that the line breaks are not required; any spacing will suffice to separate the different clauses (i.e. parts) of the `SELECT` statement. | ||||||
|
|
||||||
| ~~~~exercism/note | ||||||
| The `FROM` and `WHERE` clauses are optional. | ||||||
| A statement like `SELECT "Hello, world.";` is perfectly valid, and will generate the following result: | ||||||
|
|
||||||
| ``` | ||||||
| "Hello, world." | ||||||
| --------------- | ||||||
| Hello, world. | ||||||
| ``` | ||||||
| ~~~~ | ||||||
|
|
||||||
| 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 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 | ||||||
| ``` | ||||||
|
|
||||||
| 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 | ||||||
| ``` | ||||||
|
|
||||||
| ## 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, we can do the following: | ||||||
|
|
||||||
| ```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 | ||||||
|
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,138 @@ | ||
| # Introduction | ||
|
|
||
| In SQL, a `SELECT` statement allows you to retrieve data from a database. | ||
| The result of a `SELECT` statement is a result set. | ||
| A result set contains values arranged in rows and columns. | ||
| Each column is a particular attribute of the data. | ||
| Each row contains a value for every column. | ||
|
|
||
| One way to think about it is that a set of columns represents a data structure, where each column represents a field of the data structure, and each row represents an instance of the data structure. | ||
|
|
||
| With a `SELECT` statement, you can specify the data you want to retrieve. | ||
| You can also 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. | ||
|
|
||
| ~~~~exercism/note | ||
| The `FROM` and `WHERE` clauses are optional. | ||
| A statement like `SELECT "Hello, world.";` is perfectly valid, and will generate the following result: | ||
|
|
||
| ``` | ||
| "Hello, world." | ||
| --------------- | ||
| Hello, world. | ||
| ``` | ||
| ~~~~ | ||
|
|
||
| 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 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 | ||
| ``` | ||
|
|
||
| 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 | ||
| ``` | ||
|
|
||
| ## 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, we can do the following: | ||
|
|
||
| ```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 |
|---|---|---|
|
|
@@ -33,6 +33,17 @@ | |
| ] | ||
| }, | ||
| "exercises": { | ||
| "concept": [ | ||
| { | ||
| "slug": "intro-select", | ||
| "name": "SELECT Basics", | ||
|
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 noticed that exercises are typically named for their "story," not for what they teach. Maybe this one should be named "Weather Report." Other ideas? |
||
| "uuid": "1762549a-4622-442d-9aff-4c65f72a955c", | ||
| "concepts": [ | ||
| "select" | ||
| ], | ||
| "prerequisites": [] | ||
| } | ||
| ], | ||
| "practice": [ | ||
| { | ||
| "slug": "hello-world", | ||
|
|
@@ -716,6 +727,13 @@ | |
| "icon": "small" | ||
| } | ||
| ], | ||
| "concepts": [ | ||
| { | ||
| "uuid": "92187df0-fb68-4b46-af3b-cc17c2ef6fbc", | ||
| "slug": "select", | ||
| "name": "SELECT" | ||
| } | ||
| ], | ||
| "tags": [ | ||
| "execution_mode/interpreted", | ||
| "platform/linux", | ||
|
|
||
|
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. Are hints needed for this one? I'm open to suggestions... |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # 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. | ||
|
|
||
| * `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. | ||
|
|
||
| In each of the following tasks, your job is to retrieve 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. | ||
blackk-foxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| For now, don't worry about the `CREATE TABLE`; that will be covered in another lesson. | ||
| Just focus on adding the appropriate `SELECT` statement to accomplish the given task. | ||
|
|
||
| ## 1. Retrieve all of the data | ||
|
|
||
| Retrieve all of the data from the `weather_readings` table. | ||
|
|
||
| ## 2. Retrieve only the location and temperature data | ||
|
|
||
| Retrieve only the location and temperature values from the `weather_readings` table. | ||
|
|
||
| ## 3. Retrieve only data for Seattle | ||
|
|
||
| Retrieve only the data for the Seattle location. | ||
|
|
||
| ## 4. Retrieve only the data with humidity in a specific range | ||
|
|
||
| Retrieve only the data where the humidity is between 60% and 70%. | ||
Uh oh!
There was an error while loading. Please reload this page.