Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects.

Similar presentations


Presentation on theme: "Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects."— Presentation transcript:

1 Single-Table Queries 1: Basics CS 320 Online

2 Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language (DML)  Used to insert, update, delete, and view the data in database objects

3  Query: question represented in a way a database can understand  SQL query components:  SELECT: columns whose values you want to retrieve  FROM: tables that contain the columns in the SELECT clause  WHERE: optional search condition to restrict which records are retrieved Database Queries

4 Programming Note: Before you can run SELECT queries, your database must contains data Run the scripts to insert the records in the CANDY database

5 CANDY database tables (with revised field names) CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

6 Retrieving Data From a Single Table Basic syntax: SELECT field1, field2, … FROM tablename WHERE search_condition SELECT cust_id, cust_name FROM candy_customer WHERE cust_id = 1

7 Retrieving All Fields or All Records  To retrieve all fields in the table: use the "*" wildcard character  To retrieve all records in a table: omit the search condition SELECT * FROM tablename WHERE search_condition

8 Examples of Queries that Retrieve all Fields and all Records SELECT * FROM candy_purchase; SELECT * FROM candy_cust_type;

9 Search Conditions General format:  Operators: =,, =, <> or !=, BETWEEN, IN, LIKE Examples: FieldName Operator TargetValue prod_id = 1 purch_pounds > 5 prod_cost >= 9.99 purch_status != 'PAID' purch_pounds BETWEEN 5 AND 10 prod_id IN (1, 3, 5)

10 SearchTarget Values Numbers  Just type the number Text strings  Enclose in single or double quotes  Search for strings with embedded single quotes by typing \' or \" Dates  Enter as a text string in 'yyyy-mm-dd' format prod_id = 1 prod_cost >= 9.99 cust_name = 'Jones, Joe' prod_desc = 'Millionaire\'s Macadamia Mix' purch_date = '2004-10-28'

11 Using IN and BETWEEN IN: retrieves all matching values within a set of values BETWEEN: retrieves all matching values within a range of values (inclusive) WHERE cust_zip IN ('91211', '91212') WHERE prod_id BETWEEN 1 AND 3

12 Partial-text search: searches for part of a string within a character field  Use the % wildcard character to match 0 or more characters  Examples: Partial-Text Searches Using LIKE WHERE cust_zip LIKE '9121%' WHERE cust_name LIKE '%s' WHERE cust_name LIKE '%s%'

13 Searching for NULL Values NULL: undefined Search conditions for NULL and non- NULL values: WHERE fieldname IS NULL WHERE fieldname IS NOT NULL WHERE cust_phone IS NULL WHERE purch_delivery_date IS NOT NULL

14 Compound Search Conditions  Formed by connecting two or more search conditions using the AND or OR operator  AND: query only retrieves records for which both conditions are true  OR: query retrieves records for which either condition is true

15 Example Compound Search Conditions WHERE Condition1 AND Condition2 WHERE Condition1 OR Condition2 WHERE prod_id = 1 AND purch_date = '2008-10-28' WHERE prod_id = 1 OR prod_id = 3

16 Using AND and OR in Search Conditions Every expression must be well-formed:  Do this:  Not this: WHERE purch_date > '2004-10-28' AND purch_date < '2004-11-1' WHERE purch_date > '2004-10-28' AND < '2004-11-1'

17 MySQL evaluates AND expressions first, then OR expressions To force a specific evaluation order, place conditions to be evaluated first in parentheses! Order of AND/OR Evaluation SELECT cust_id FROM candy_customer WHERE cust_type = 'W' AND (cust_zip = '91209' OR cust_zip = '91212')

18 Test Yourself: How many fields and how many records will the following query retrieve? a. 7 fields and 14 records b. 14 fields and 7 records c. 7 fields and 9 records d. None of the above SELECT * FROM candy_purchase;

19 Test Yourself: How many fields and how many records will the following query retrieve? a. 7 fields and 14 records b. 14 fields and 7 records c. 7 fields and 9 records d. None of the above SELECT * FROM candy_purchase;

20 a. 4 b. 8 c. 9 d. 10 e. None of the above Test Yourself: How many CANDY_CUSTOMER records will the following query retrieve? SELECT cust_id FROM candy_customer WHERE cust_name LIKE '%s'

21 a. 4 b. 8 c. 9 d. 10 e. None of the above Test Yourself: How many CANDY_CUSTOMER records will the following query retrieve? SELECT cust_id FROM candy_customer WHERE cust_name LIKE '%s'

22 Test Yourself: How many records will the following query retrieve? a. 0 b. 8 c. 14 d. None of the above SELECT * FROM candy_purchase WHERE purch_status LIKE '%Paid%'

23 Test Yourself: How many records will the following query retrieve? a. 0 b. 8 c. 14 d. None of the above SELECT * FROM candy_purchase WHERE purch_status LIKE '%Paid%'

24 Test Yourself: How many records will the following query retrieve? a. 0 b. 3 c. 5 d. 8 e. None of the above SELECT * FROM candy_purchase WHERE purch_delivery_date = NULL AND purch_status = 'PAID'

25 Test Yourself: How many records will the following query retrieve? a. 0 b. 3 c. 5 d. 8 e. None of the above SELECT * FROM candy_purchase WHERE purch_delivery_date = NULL AND purch_status = 'PAID'


Download ppt "Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects."

Similar presentations


Ads by Google