SQL Training SQL Statements – Part 1. Confidential & Proprietary Copyright © 2009 Cardinal...

32
SQL Training SQL Statements – Part 1

Transcript of SQL Training SQL Statements – Part 1. Confidential & Proprietary Copyright © 2009 Cardinal...

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

SQL Training

SQL Statements – Part 1

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

•Confidential & ProprietaryCopyright © 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:

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Lesson Plan

Page 3

What is SQL

Select Statement

Where Clause

In, Like, Between

Workshop

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

Structured Query Language

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Structured Query Language

SQL is the language most commonly used to create and process datain 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

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

•Confidential & ProprietaryCopyright © 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

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

•Confidential & ProprietaryCopyright © 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

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select – Specifying Columns

Problem: Get a list of all the products that use propane (fuelsourceid = 3).

select productcode, productdescription, productpricefrom product where fuelsourceid = 3

21 Rows

Page 8

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select - DistinctProblem: What are the voltages of the products sold?

SELECT DISTINCT voltage FROM Product

Page 9

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select - Concatenation of FieldsThe Concatenation Function can be used to combine multiple fields into a single field.

SELECT vendornamename, vendorfirstname || ' ' || vendorlastname as NameFROM vendor ORDER BY vendorname;

Page 10

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Adding Where Clauses

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select - Where ConditionThe 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

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select – Where Predicate

Problem: List of all vendors in California.

SELECT vendorname FROM Vendor

WHERE provinceabbreviation = ‘CA';

Page 13

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select – Compound WhereProblem: List of all the products that use Natural Gas, have a power rating of 7000 or 5000 and a frequency of 50.

Page 14

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

•Confidential & ProprietaryCopyright © 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

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select – Between PredicateProblem: List products where the product price is between $ 7063 and $ 7300.

Page 16

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

•Confidential & ProprietaryCopyright © 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;

SUBSCRIBEDPRODUCTID SUBSCRIBEDPRODUCTNAME STARTEFFECTIVEDATE42 Secret Shopper 4-Mar-0417 Market Decision Maker 10-Apr-04

140 Magazines 10-Apr-04110 Corporate| Order Track 2-May-0450 Television 15-May-04

118 Marketing Mix Management 22-May-04 6 Rows

Page 17

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select – Like PredicateProblem: Select all rows in the Fuelsource table that have a description containing the word ‘Gas’.

Page 18

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select – Like PredicateCaution: Like Predicate is Case Sensitive.

1 Row

SELECT RequiredProductID, RequiredProductNameFROM RequiredProductWHERE LOWER (RequiredProductName) Like '%web%';

SUBSCRIBEDPRODUCTID SUBSCRIBEDPRODUCTNAME

125 National Fast Affiliates - WEB file

LOWER Syntax LOWER(char) Returns char, with all letters lowercase.

UPPER Syntax 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.

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Select – In PredicateProblem: List all Vendors in CT, PA, FL;

Page 20

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Workshop

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

•Confidential & ProprietaryCopyright © 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

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

•Confidential & ProprietaryCopyright © 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 ORMissing 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 identifierMake sure the table you are referencing is in the From clause ORMake 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

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

•Confidential & ProprietaryCopyright © 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

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

List all products in the Countries in the country table.

Problem 1 – Simple Select

Page 25

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

Show all components with restockdaycount = 20.

35 Rows

Problem 2 – Simple Select with Where

Page 26

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

•Confidential & ProprietaryCopyright © 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

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

•Confidential & ProprietaryCopyright © 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

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

•Confidential & ProprietaryCopyright © 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

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

•Confidential & ProprietaryCopyright © 2009 Cardinal Directions, Inc.

List all the vendors that do not have a fax.

Problem 6 – Select using NULL Predicate

Page 30

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

•Confidential & ProprietaryCopyright © 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

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

•Confidential & ProprietaryCopyright © 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