Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Training SQL Statements – Part 1. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Objectives Explain the role of SQL.

Similar presentations


Presentation on theme: "SQL Training SQL Statements – Part 1. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Objectives Explain the role of SQL."— Presentation transcript:

1 SQL Training SQL Statements – Part 1

2 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Objectives Explain the role of SQL Write basic SQL Select statements with compound where clauses Page 2 At the end of this section you will be able to:

3 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Plan Page 3 What is SQL Select Statement Where Clause In, Like, Between Workshop

4 Structured Query Language

5 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Structured Query Language SQL is the language most commonly used to create and process data in relational databases. SQL can be used with Access, DB2, MySQL, Oracle, MS SQL Server, Sybase, or any other relational database. Data Control (DCL) Grant Revoke User Privileges Data Definition (DDL) Create Alter Drop Tables, Views, Constraints Data Manipulation (DML) Select Insert Update Delete Retrieve and Manipulate Data Page 5

6 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select Syntax SELECT field, field, field FROM table, table, view WHERE condition and condition or condition GROUP BY field, field, field HAVING … ORDER BY field asc SELECT customerName, billingCity, count(incidentID) FROM Customer, Incident WHERE customerID = reportedByCustomerID and billingCity = 'New York' GROUP BY customername, billingCity HAVING count(incidentID) > 50 ORDER BY customerName desc Page 6

7 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – All Rows Problem: Get a list of all the records and fields in the Fuelsource table. SELECT * FROM Fuelsource; Page 7

8 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Specifying Columns Problem: Get a list of all the products that use propane (fuelsourceid = 3). select productcode, productdescription, productprice from product where fuelsourceid = 3 21 Rows Page 8

9 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select - Distinct Problem: What are the voltages of the products sold? SELECT DISTINCT voltage FROM Product Page 9

10 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select - Concatenation of Fields The Concatenation Function can be used to combine multiple fields into a single field. SELECT vendornamename, vendorfirstname || ' ' || vendorlastname as Name FROM vendor ORDER BY vendorname; Page 10

11 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Adding Where Clauses

12 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select - Where Condition The WHERE clause restricts the rows selected to those for which the condition is TRUE. If you omit this clause, Oracle returns all rows from the tables, views, or snapshots in the FROM clause. Examples: WHERE subscribedProductID > 5 WHERE city = 'New York' WHERE hourlyrate BETWEEN 60 and 80 WHERE subscribedProductName LIKE '%Shopper%' WHERE checkout IS NOT NULL WHERE customerID IN (2, 3, 6) Page 12

13 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Where Predicate Problem: List of all vendors in California. SELECT vendorname FROM Vendor WHERE provinceabbreviation = ‘CA'; Page 13

14 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Compound Where Problem: List of all the products that use Natural Gas, have a power rating of 7000 or 5000 and a frequency of 50. Page 14

15 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select - Null Predicate SELECT RequiredProductName, startEffectiveDate, endEffectiveDate FROM RequiredProduct WHERE endEffectiveDate IS NULL ORDER BY RequiredProductName; Problem: List all the RequiredProduct rows that do not have a endEffectiveDate. 161 Rows SELECT RequiredProductName, startEffectiveDate, endEffectiveDate FROM RequiredProduct WHERE endEffectiveDate IS NOT NULL ORDER BY RequiredProductName; 0 Rows Page 15

16 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Between Predicate Problem: List products where the product price is between $ 7063 and $ 7300. Page 16

17 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Between Predicate – Using Dates SELECT RequiredProductID, RequiredProductName,startEffectiveDate FROM RequiredProduct WHERE startEffectiveDate BETWEEN to_date('03/04/2004','MM/DD/YYYY') and to_date('06/01/2004', 'MM/DD/YYYY') ORDER BY startEffectiveDate; SUBSCRIBEDPRODUCTIDSUBSCRIBEDPRODUCTNAMESTARTEFFECTIVEDATE 42Secret Shopper4-Mar-04 17 Market Decision Maker10-Apr-04 140Magazines10-Apr-04 110Corporate| Order Track2-May-04 50Television15-May-04 118Marketing Mix Management22-May-04 6 Rows Page 17

18 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Like Predicate Problem: Select all rows in the Fuelsource table that have a description containing the word ‘Gas’. Page 18

19 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – Like Predicate Caution: Like Predicate is Case Sensitive. 1 Row SELECT RequiredProductID, RequiredProductName FROM RequiredProduct WHERE LOWER (RequiredProductName) Like '%web%'; SUBSCRIBEDPRODUCTIDSUBSCRIBEDPRODUCTNAME 125National Fast Affiliates - WEB file LOWERSyntax LOWER(char)Returns char, with all letters lowercase. UPPERSyntax UPPER(char) Returns char, with all letters uppercase. Page 19 Note: This function doesn’t work on some languages (Chinese). Not all SQL version support this function. The classroom server supports this function.

20 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Select – In Predicate Problem: List all Vendors in CT, PA, FL; Page 20

21 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Workshop

22 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Individual SQL Workshop 1 1.Using Oracle SQL Developer you will create the SQL statements required to produce the requested output. 2.You will begin by logging onto the training Database. 3.You have been provided with a list of frequent error messages. This is not a complete list and the resolution may not always one of the options listed as there are numerous causes for many of the errors. But these are the most frequent and will be helpful. Page 22

23 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Common Error Messages ORA-00918: column ambiguously defined If a column appears in multiple tables, you must qualify the fieldname with the table name. ORA-00933: SQL command not properly ended Look for missing single quotes around strings OR missing ‘and’ between Where clause statements OR Missing keywords such as Where, From, Group By, Order By ORA-00904: “String Expression": invalid identifier Make sure you are using single quotes and not double quotes. ORA-00904: “CUSTOMER"."CUSTOMERID": invalid identifier Make sure the table you are referencing is in the From clause OR Make sure the field you are referencing is in one of the tables in your From clause OR make sure you have spelled the table and/or field name correctly. Page 23

24 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Write the SQL to answer the business question. Only include the columns shown in the picture. Your answer should match the data shown. In some cases, only the first and last rows will be shown due to the size constraints of the page. Note: Oracle does not always print the entire column name – look at the Database Design for column names. Workshop Page 24

25 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. List all products in the Countries in the country table. Problem 1 – Simple Select Page 25

26 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Show all components with restockdaycount = 20. 35 Rows Problem 2 – Simple Select with Where Page 26

27 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. List all components with a weight between 18 and 30 ordered by the weight. 153 Rows Problem 3 – Select with Between Page 27

28 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. From the Vendor table, list all vendors and their userid when the vendor is in California or the userid is between 60 and 65. Problem 4 – Select with Compound Where Page 28

29 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. From the vendor table, list the information for provinceid = 5 (California) or = 32 (New York) or = 49 (Wisconsin) or = 35 (Ohio). Problem 5 – Select using the IN Predicate Page 29

30 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. List all the vendors that do not have a fax. Problem 6 – Select using NULL Predicate Page 30

31 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. From the Vendor table, list all the cities where there are vendors. Only list each city once. 30 Rows Problem 7 – Select using Distinct Page 31

32 Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. List all products with ‘9000’ or ‘7000’ in their name and the product name also has ‘110v’ as part of it. Problem 8 – Select using LIKE Predicate Page 32


Download ppt "SQL Training SQL Statements – Part 1. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. Lesson Objectives Explain the role of SQL."

Similar presentations


Ads by Google