Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web...

13
CIS 50 Queries and SQL: Structured Query Language Debbie J. Fields Queries In the MS Access assignment, you were introduced to queries. You use queries to view, change, and analyze data in different ways. You can also use them as a source of records for forms, reports, and data access pages (a web page, published from Access, which has a connection to a database). There are several types of queries in Microsoft Access. A select query is the most common type of query. It retrieves data from one or more tables and displays the results in a datasheet where you can update the records (with some restrictions). You can also use a select query to group records and calculate sums, counts, averages, and other types of totals. In the MS Access assignment, you executed an existing select query (QUERY1 – Friends not in CA) to view your friends who did not live in CA. You created a new select query (Friends in CA) to view LastName, FirstName and HomePhone fields, sorted by LastName for those friends who did live in ca (=”ca”) What is SQL? Structured Query Language, SQL (pronounced "ess-que-el") is the most popular computer language used to create, modify, retrieve and manipulate data from relational database management systems. SQL is used to communicate with almost any database; it is the standard language for relational database management systems. When you created a query in Access, Access constructs the equivalent SQL statements behind the scenes for you. In fact, most query properties in the property sheet in query Design view have equivalent clauses and options available in SQL view. (SQL view: A window that displays the SQL statement for the current query). When you request information from a database, it is usually in the form of a question or a statement that implies a question. For example, you might formulate statements such as: “Which cities do our customers live in?” or “Show me a current list of our employees and their phone numbers” or “Give me the names of the folks on our staff and the dates they were hired”. After you know what you want to ask, you can translate your request into a more formal SQL statement. You compose the translation using this form: Select <item> from the <source> SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. Some common relational database management systems HW16: 8/2017 - Access Queries and SQL SQL - 1

Transcript of Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web...

Page 1: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

CIS 50 Queries and SQL: Structured Query LanguageDebbie J. Fields

Queries

In the MS Access assignment, you were introduced to queries. You use queries to view, change, and analyze data in different ways. You can also use them as a source of records for forms, reports, and data access pages (a web page, published from Access, which has a connection to a database). There are several types of queries in Microsoft Access. A select query is the most common type of query. It retrieves data from one or more tables and displays the results in a datasheet where you can update the records (with some restrictions). You can also use a select query to group records and calculate sums, counts, averages, and other types of totals.

In the MS Access assignment, you executed an existing select query (QUERY1 – Friends not in CA) to view your friends who did not live in CA. You created a new select query (Friends in CA) to view LastName, FirstName and HomePhone fields, sorted by LastName for those friends who did live in ca (=”ca”)

What is SQL?

Structured Query Language, SQL (pronounced "ess-que-el") is the most popular computer language used to create, modify, retrieve and manipulate data from relational database management systems. SQL is used to communicate with almost any database; it is the standard language for relational database management systems. When you created a query in Access, Access constructs the equivalent SQL statements behind the scenes for you. In fact, most query properties in the property sheet in query Design view have equivalent clauses and options available in SQL view. (SQL view: A window that displays the SQL statement for the current query).

When you request information from a database, it is usually in the form of a question or a statement that implies a question. For example, you might formulate statements such as: “Which cities do our customers live in?” or “Show me a current list of our employees and their phone numbers” or “Give me the names of the folks on our staff and the dates they were hired”. After you know what you want to ask, you can translate your request into a more formal SQL statement. You compose the translation using this form:

Select <item> from the <source>

SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. Some common relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc. Although most database systems use SQL, most of them also have their own additional proprietary extensions that are usually only used on their system. However, the standard SQL commands such as "Select", "Insert", "Update", "Delete", "Create", and "Drop" can be used to accomplish almost everything that one needs to do with a database.

HW16: 8/2017 - Access Queries and SQLSQL - 1

Page 2: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

In the Emerge with Computers website, view the Module 4/Unit 1/Unit 1-2 (Data Base Tools)/Unit1-2.3 Structured Query Language –Structured Query Language (SQL). Especially view the video:

And visit the W3shools tutorial on the web: http://www.w3schools.com/sql/

HW16: 8/2017 - Access Queries and SQLSQL - 2

Page 3: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

Also, view Khan Academy: https://www.khanacademy.org/computing/hour-of-code/hour-of-sql/v/welcome-to-sql

Create a document named: your_name_sql_query.docxType your nameType today’s dateType CIS 50 OnlineCopy and paste pages 6 thru 9 of this assignment into your documentAnswer the questions, submit via Canvas

HW16: 8/2017 - Access Queries and SQLSQL - 3

Page 4: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

 Record   toyInventory          ID Toy Brand Color Ordered Ordered By Quantity Wholesale Retail Stock

1 Wooden Tugboat Non-Toxic Toys Brown0625201

5 J.Mathers 50 $6.50 $8.78 23

2 Wooden Train Set Non-Toxic Toys Brown0625201

5 J.Mathers 25 $8.50 $11.48 1

3 Warp 10 U.F.O Galaxy Toys Silver/Blue0701201

5 T. Livingston 50 $2.50 $3.38 40

4 Vampire Fangs Plastic Pets Pink0705201

5 J.Mathers 50 $4.99 $6.74 22

5 Thinking Trees Plastic Pets Brown0705201

5 J.Mathers 50 $4.99 $6.74 13

6 Stegosaurus Sam Nature's Kids Brown/Olive0701201

5 T. Livingston 25 $2.50 $3.38 84

7 Space Station Galaxy Toys N/A0701201

5 T. Livingston 25 $12.50 $16.88 50

8 Singing Slugs Zap Toys Green/Olive0705201

5 J.Mathers 50 $4.99 $6.74 2

9 Satellite Launcher Flying Fun Silver/Taupe0624201

5 A. Hayes 50 $12.50 $16.88 10

10 Rock'n'Roll Ron Zap Toys Magenta/Red0705201

5 J.Mathers 50 $4.99 $6.74 28

11 Rocket Racer Flying Fun Red/Blue0714201

5 A. Hayes 50 $2.50 $3.38 32

12 Plastic Sushi Rich Folks Yellow/Green0705201

5 J.Mathers 50 $4.99 $6.74 33

13 Plastic Penguin Plastic Pets Black/White0705201

5 J.Mathers 50 $4.99 $6.74 20

14 Orbitron Galaxy Toys Magenta0623201

5 T. Livingston 25 $13.99 $18.89 3

15 Model Satellite Flying Fun Red/White0624201

5 A. Hayes 50 $5.50 $7.43 12

16 Model Mercedes Rich Folks Blue/Tan0705201

5 J.Mathers 50 $4.99 $6.74 4

17 Model Ferrari Rich Folks Red/Blue0705201

5 J.Mathers 50 $4.99 $6.74 25

18 Model Cessna Flying Fun Black0701201

5 A. Hayes 50 $8.99 $12.14 0

19 Micro Mice Plastic Pets White0705201

5 J.Mathers 50 $4.99 $6.74 21

20 Kaleidscope Galaxy Toys Purple0714201

5 T. Livingston 50 $3.99 $5.39 10

HW16: 8/2017 - Access Queries and SQL SQL - 4

Page 5: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

21 Inflatable Snails Plastic Pets Green/Olive0705201

5 J.Mathers 50 $4.99 $6.74 20

22 Inflatable Lobster Plastic Pets Pink0705201

5 J.Mathers 50 $4.99 $6.74 18

23 Inflatable Crab Plastic Pets Red0705201

5 J.Mathers 50 $4.99 $6.74 1

24 Fortran Learner Computer Fun Yellow0701201

5 A. Hayes 50 $3.99 $5.39 32

25 Ferdie Frog Nature's Kids Green0701201

5 T. Livingston 25 $2.50 $3.38 70

26 Expanding Expresso Rich Folks Brown0705201

5 J.Mathers 50 $4.99 $6.74 27

27 Eight-color Paint Set Non-Toxic Toys N/A0714201

5 J.Mathers 25 $2.99 $4.01 75

28 Day-GloFrisbees Flying Fun Lime/Pink0701201

5 A. Hayes 50 $2.50 $3.38 45

29 Brontosaurus Bruce Nature's Kids Olive0714201

5 T. Livingston 25 $4.99 $6.74 6

30 Agatha Alligator Nature's Kids Green0701201

5 T. Livingston 25 $4.99 $6.74 54

ANSWER THE FOLLOWING QUESTIONS:

1. Use the printout of the small toyInventory to determine the result of the queries. NOTE: 1. What is the name of the table? ______________________________ 2. How many fields in the table?? (names of columns)? ____________________________3. What are the names of the table’s fields (names of columns)? ____________________________4. How many records in the table?? ____________________________

ASSIGNMENT EXAMPLE:

Use the printout of the small toyInventory table to determine the result of the query.

QUERY: Write SQL code to create a report listing the fields Toy and Quantity; list only those records where Quantity is less than 50

a). Which record id’s will be selected for this query:

ANSWER: 8 records will be selected: Record ID: 2, 6, 7, 14, 25, 27, 29, 30

Write the SQL statements that will process this query

HW16: 8/2017 - Access Queries and SQL SQL - 5

Page 6: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

An example of an SQL query syntax is as follows:

SELECT name of columns to printFROM name of tableWHERE condition to select row

The ANSWER to the above query using SQL language would be;

b). Write the SQL statements that will process this query

SELECT Toy, Quantity FROM toyInventory

WHERE quantity < 50

SELECT Toy, Quantity must have column namesFROM toyInventory must have table name

WHERE quantity < 50 must have column name, comparison operator (> < =), must have value or column name

Note: your SQL queries MUST use the same format and syntax. Most students do NOT complete the WHERE clause correctly. Check your work. WHERE clause has 3 parts: column name comparison operator value or column name

CIS 50: NAME:

2. QUERY: Write the SQL code to create a report listing; Toy, Brand, and Wholesale price, list only those records where the Wholesale price is more than 2.50, also list only those records where the wholesale price is below 4.00 (do

not include items that wholesale for 2.50 or 4.00)

a). Which record id’s will be selected for this query:

ANSWER:

Write the SQL statements that will process this query (HINT: do not just copy/paste from the statement above, I want valid SQL programming statements (Select, From, Where).

b). Write the SQL statements that will process this query

ANSWER: SELECT

HW16: 8/2017 - Access Queries and SQL SQL - 6

Page 7: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

FROMWHERE

3. QUERY: Write the SQL code to create a report listing; Toy, Brand, and date ordered, list only those records who were ordered before July 1, 2015

a). Which record id’s will be selected for this query:

ANSWER:

Write the SQL statements that will process this query (HINT: do not just copy/paste from the statement above, I want valid SQL programming statements (Select, From, Where).

b). Write the SQL statements that will process this query

ANSWER: SELECTFROMWHERE

4. QUERY: Write the SQL code to create a report listing; Toy, Brand, and number of toys in stock; list only those records for Nature’s Kids, also list only those records for Plastic Pets

a). Which record id’s will be selected for this query:

ANSWER:

Write the SQL statements that will process this query (HINT: do not just copy/paste from the statement above, I want valid SQL programming statements (Select, From, Where).

b). Write the SQL statements that will process this query

ANSWER: ????????????

HW16: 8/2017 - Access Queries and SQL SQL - 7

Page 8: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

5. QUERY: Write the SQL code to create a report listing; Toy, Brand, and number of toys in stock; list only those records for Galaxy Toys and Flying Fun (this is a trick question, why?)

a). Which record id’s will be selected for this query:

ANSWER: ______ 0 (zero) records will be selected

WHY?

Write the SQL statements that will process this query (HINT: do not just copy/paste from the statement above, I want valid SQL programming statements (Select, From, Where).

b). Write the SQL statements that will process this query

ANSWER: ????????????

6. QUERY: Write the SQL code to create a report listing; Toy, and Brand; list only those records for Flying Fun or Nature’s Kids, both items must be less that 5.00 retail

a). Which record id’s will be selected for this query:

ANSWER:

Write the SQL statements that will process this query (HINT: do not just copy/paste from the statement above, I want valid SQL programming statements (Select, From, Where).

b). Write the SQL statements that will process this query

ANSWER: ????????????

HW16: 8/2017 - Access Queries and SQL SQL - 8

Page 9: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

7. QUERY: Write the SQL code to create a report listing; Toy, Brand and Quantity; list only those records where QTY is more than 24 or QTY is less than 50 - this is another trick question

a). Which record id’s will be selected for this query:

ANSWER: ______ 0 (zero) records will be selected

WHY?

Write the SQL statements that will process this query (HINT: do not just copy/paste from the statement above, I want valid SQL programming statements (Select, From, Where).

b). Write the SQL statements that will process this query

ANSWER: ????????????

Write the SQL statements and specify which record id’s will be selected.

QUERY SQL Which records will be selected? RecordID

1. All the toys ordered between July 1 and July 7

SELECT FROM WHERE

2. Any toys with a wholesale cost more than $4.99 that were not ordered by J. Mathers

SELECT FROM WHERE

3. All the Brown toys that retail for SELECT

HW16: 8/2017 - Access Queries and SQL SQL - 9

Page 10: Computer Information Systems - 50, 80lpc1.clpccd.cc.ca.us/lpc/dfields/cis50/HW_Assignment… · Web viewStructured Query Language, SQL (pronounced "ess-que-el") is the most popular

more than $10.00 FROM WHERE

4. Any toys that were ordered by T. Livingston or A. Hayes before 7/1/2015

SELECT FROM WHERE

5. Any purple toys that retail for less than $5.00 or any green toys that the store has fewer

than 10 in stock

SELECT FROM WHERE

HW16: 8/2017 - Access Queries and SQL SQL - 10