database queries aka sql (pronounced “ sequel ” )

36
Database Queries aka SQL (pronounced “sequel”)

Upload: vernon-poole

Post on 21-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database Queries aka SQL (pronounced “ sequel ” )

Database Queriesaka

SQL (pronounced “sequel”)

Page 2: Database Queries aka SQL (pronounced “ sequel ” )

What is SQL?What is SQL?

SQL = SQL = SStandard tandard QQuery uery LLanguageanguage

SQL is a language for retrieving, SQL is a language for retrieving, creating, modifying, or removing data creating, modifying, or removing data from just about any modern databasefrom just about any modern database

Page 3: Database Queries aka SQL (pronounced “ sequel ” )

What is SQL?What is SQL? To get information from a database To get information from a database

file, a user issues a “query”file, a user issues a “query” A “query” is a request to retrieve dataA “query” is a request to retrieve data MS Access will go through all the MS Access will go through all the

records in the database and “select” records in the database and “select” those records that satisfy the search those records that satisfy the search conditioncondition

SQL (Structured Query Language) SQL (Structured Query Language) allows a user to specify the conditions allows a user to specify the conditions without writing complex codewithout writing complex code

Page 4: Database Queries aka SQL (pronounced “ sequel ” )

Basic SQL CommandsBasic SQL Commands

Creating tables with CREATECreating tables with CREATE Adding data with INSERTAdding data with INSERT

Viewing data with SELECTViewing data with SELECT Removing data with DELETERemoving data with DELETE Modifying data with UPDATEModifying data with UPDATE Destroying tables with DROPDestroying tables with DROP

Page 5: Database Queries aka SQL (pronounced “ sequel ” )

Retrieving Data from Retrieving Data from OneOne Table Table

Page 6: Database Queries aka SQL (pronounced “ sequel ” )

The SELECTThe SELECT

SELECTSELECT column1, column2, …column1, column2, …

FROMFROM tablenametablename

{WHERE{WHERE condition condition}}

{ORDER BY{ORDER BY column column}}

Page 7: Database Queries aka SQL (pronounced “ sequel ” )

A few simple SELECTsA few simple SELECTs

SELECT * FROM contacts;SELECT * FROM contacts;– Display all records in the ‘contacts’ tableDisplay all records in the ‘contacts’ table

SELECT contactid, name FROM contacts;SELECT contactid, name FROM contacts;– Display only the contactid and nameDisplay only the contactid and name

SELECT contactname FROM contacts SELECT contactname FROM contacts ORDER BY contactname;ORDER BY contactname;– Display only the contactname, sort by nameDisplay only the contactname, sort by name

Page 8: Database Queries aka SQL (pronounced “ sequel ” )

The SELECTThe SELECT

SELECTSELECT **

FROMFROM TABLE;TABLE;

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

ALL Columns

No WHERE Clause so ALL Rows are Returned

End of Command

Page 9: Database Queries aka SQL (pronounced “ sequel ” )

Refining selections with Refining selections with WHEREWHERE

The WHERE “clause” allows you to select The WHERE “clause” allows you to select records based on a conditionrecords based on a condition

SELECT * FROM contactsSELECT * FROM contactsWHERE age<10;WHERE age<10;

– Display records from contacts where Display records from contacts where age<10age<10

SELECT * FROM contactsSELECT * FROM contactsWHERE age BETWEEN 18 AND 35;WHERE age BETWEEN 18 AND 35;

– Display records where age is 18-35Display records where age is 18-35

Page 10: Database Queries aka SQL (pronounced “ sequel ” )

WHERE with “WHERE with “ANDAND””SELECTSELECT **

FROMFROM SALES_DATASALES_DATA

WHEREWHERE Department = 'Water Sports'Department = 'Water Sports'

ANDAND Buyer = 'Nancy Meyers';Buyer = 'Nancy Meyers';

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 11: Database Queries aka SQL (pronounced “ sequel ” )

WHERE with “WHERE with “OROR””

SELECTSELECT **FROMFROM SALES_DATASALES_DATAWHEREWHERE Department = 'Camping'Department = 'Camping' OROR Department = 'Climbing';Department = 'Climbing';

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 12: Database Queries aka SQL (pronounced “ sequel ” )

WHERE with “WHERE with “ININ””SELECTSELECT **FROMFROM SALES_DATASALES_DATAWHEREWHERE Buyer Buyer ININ ('Nancy Meyers', ('Nancy Meyers',

'Cindy Lo', 'Jerry Martin');'Cindy Lo', 'Jerry Martin');

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 13: Database Queries aka SQL (pronounced “ sequel ” )

WHERE with “WHERE with “BETWEENBETWEEN””SELECTSELECT **FROMFROM ORDERSORDERSWHEREWHERE ExtendedPriceExtendedPrice

BETWEENBETWEEN 100 AND 200; 100 AND 200;

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 14: Database Queries aka SQL (pronounced “ sequel ” )

WHERE with “WHERE with “LIKELIKE””

SELECTSELECT **

FROMFROM SALES_DATASALES_DATA

WHEREWHEREBuyer Buyer LIKELIKE 'Pete*'; 'Pete*';

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 15: Database Queries aka SQL (pronounced “ sequel ” )

WHERE with “WHERE with “LIKELIKE””SELECTSELECT **FROMFROM SALES_DATASALES_DATAWHEREWHERESKU_Description SKU_Description

LIKELIKE ‘*Tent*'; ‘*Tent*';

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 16: Database Queries aka SQL (pronounced “ sequel ” )

Time for Hands-OnTime for Hands-On

Photo © Charles Darwin University 2005

Page 17: Database Queries aka SQL (pronounced “ sequel ” )

SQL Built-in Functions SQL Built-in Functions

There are five SQL Built-in There are five SQL Built-in Functions:Functions:– COUNTCOUNT– SUMSUM– AVGAVG– MINMIN– MAXMAX

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 18: Database Queries aka SQL (pronounced “ sequel ” )

SQL Built-in FunctionsSQL Built-in Functions

SELECTSELECT SUM (ExtendedPrice)SUM (ExtendedPrice)

ASAS Order3000SumOrder3000Sum

FROMFROM ORDERSORDERS

WHEREWHEREOrderNumber = 3000;OrderNumber = 3000;

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Column Heading

Page 19: Database Queries aka SQL (pronounced “ sequel ” )

SQL Built-in Functions SQL Built-in Functions (Continued)(Continued)

SELECTSELECT SUM (ExtendedPrice) SUM (ExtendedPrice) AS OrderItemSum,AS OrderItemSum,

AVG (ExtendedPrice)AVG (ExtendedPrice)AS OrderItemAvg,AS OrderItemAvg,

MIN (ExtendedPrice)MIN (ExtendedPrice)AS OrderItemMin,AS OrderItemMin,

MAX (ExtendedPrice)MAX (ExtendedPrice)AS OrderItemMaxAS OrderItemMax

FROMFROM ORDERS;ORDERS;

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 20: Database Queries aka SQL (pronounced “ sequel ” )

SQL Built-in Functions SQL Built-in Functions (Continued)(Continued)

SELECTSELECT COUNT(*) AS NumRowsCOUNT(*) AS NumRows

FROMFROM ORDERS;ORDERS;

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 21: Database Queries aka SQL (pronounced “ sequel ” )

Arithmetic in SELECT Arithmetic in SELECT Statements Statements

SELECTSELECT Quantity * PriceQuantity * Price AS EP, AS EP,

ExtendedPriceExtendedPrice

FROMFROM ORDERS;ORDERS;

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 22: Database Queries aka SQL (pronounced “ sequel ” )

Time for Hands-OnTime for Hands-On

Photo © Charles Darwin University 2005

Page 23: Database Queries aka SQL (pronounced “ sequel ” )

Retrieving Data from Retrieving Data from More Than OneMore Than One Table Table

Page 24: Database Queries aka SQL (pronounced “ sequel ” )

Make a list of students including phone number Make a list of students including phone number and addressand address

JoinJoin

Person

Phone Address

Page 25: Database Queries aka SQL (pronounced “ sequel ” )

Joining together tablesJoining together tables

PersonPersonPersonIDPersonID NameName AddressIDAddressID

11 JoeJoe 1010

22 JaneJane 2020

33 ChrisChris 3030

AddressAddressAddressIDAddressID CompanyCompany StreetStreet ZipZip

1010 ABCABC 12 Road12 Road 1234512345

2020 XYZXYZ 45 Road45 Road 1445414454

3030 PDQPDQ 78 Road78 Road 1442314423

PhonePhone

PhoneIDPhoneID PersonIDPersonID PhoneNumPhoneNum

100100 11 55555325555532

200200 11 55522345552234

300300 11 55532115553211

400400 22 55534215553421

500500 33 55523415552341

600600 33 55566555556655

Page 26: Database Queries aka SQL (pronounced “ sequel ” )

The SELECT with a JOIN ClauseThe SELECT with a JOIN Clause

SELECTSELECT TABLE1.Column,TABLE1.Column,

TABLE2.ColumnTABLE2.Column

FROMFROM TABLE1, TABLE2TABLE1, TABLE2

WHEREWHERETABLE1.PrimaryKey =TABLE1.PrimaryKey =

TABLE2.PrimaryKey;TABLE2.PrimaryKey;

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 27: Database Queries aka SQL (pronounced “ sequel ” )

The SELECT with a JOIN ClauseThe SELECT with a JOIN Clause

SELECTSELECT PERSON.Name, PERSON.Name,

PHONE.PhoneNumPHONE.PhoneNum

FROMFROM PERSON, PHONEPERSON, PHONE

WHEREWHERE PERSON.PersonID = PERSON.PersonID = PHONE.PersonIDPHONE.PersonID;;

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 28: Database Queries aka SQL (pronounced “ sequel ” )

The SELECT with two JOIN The SELECT with two JOIN ClausesClauses

SELECTSELECT PERSON.Name, PERSON.Name,

PHONE.PhoneNum, PHONE.PhoneNum,

ADDRESS.ZipADDRESS.Zip

FROMFROM PERSON, PHONE, ADDRESSPERSON, PHONE, ADDRESS

WHEREWHEREPERSON.PersonID = PERSON.PersonID = PHONE.PersonID PHONE.PersonID

ANDAND PERSON.AddressID = PERSON.AddressID = ADDRESS.AddressIDADDRESS.AddressID

Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

Page 29: Database Queries aka SQL (pronounced “ sequel ” )

SELECTSELECT P.Name, PH.PhoneNumP.Name, PH.PhoneNum

FROMFROM PERSON P , PHONE PH PERSON P , PHONE PH

WHEREWHERE P.PersonID = P.PersonID = PH.PersonIDPH.PersonID

Alias (Shorthand) for Table Name

The SELECT with a JOIN ClauseThe SELECT with a JOIN Clause

Page 30: Database Queries aka SQL (pronounced “ sequel ” )

Time for Hands-OnTime for Hands-On

Photo © Charles Darwin University 2005

Page 31: Database Queries aka SQL (pronounced “ sequel ” )

Writing SQL in Writing SQL in MS Access 2007MS Access 2007

Page 32: Database Queries aka SQL (pronounced “ sequel ” )

Click Click “Create”“Create”

Page 33: Database Queries aka SQL (pronounced “ sequel ” )

Click Click “Query “Query Design”Design”

Page 34: Database Queries aka SQL (pronounced “ sequel ” )

Click Click “Close”“Close”

Page 35: Database Queries aka SQL (pronounced “ sequel ” )

Click Click “SQL”“SQL”

Page 36: Database Queries aka SQL (pronounced “ sequel ” )

Click Click

““!! Run” Run”