Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Queries aka SQL (pronounced “ sequel ” ).

Similar presentations


Presentation on theme: "Database Queries aka SQL (pronounced “ sequel ” )."— Presentation transcript:

1 Database Queries aka SQL (pronounced “ sequel ” )

2 What is SQL? SQL = Standard Query Language SQL = Standard Query Language SQL is a language for retrieving, creating, modifying, or removing data from just about any modern database SQL is a language for retrieving, creating, modifying, or removing data from just about any modern database

3 What is SQL? To get information from a database file, a user issues a “query” To get information from a database file, a user issues a “query” A “query” is a request to retrieve data A “query” is a request to retrieve data MS Access will go through all the records in the database and “select” those records that satisfy the search condition MS Access will go through all the records in the database and “select” those records that satisfy the search condition SQL (Structured Query Language) allows a user to specify the conditions without writing complex code SQL (Structured Query Language) allows a user to specify the conditions without writing complex code

4 Basic SQL Commands Creating tables with CREATE Creating tables with CREATE Adding data with INSERT Adding data with INSERT Viewing data with SELECT Viewing data with SELECT Removing data with DELETE Removing data with DELETE Modifying data with UPDATE Modifying data with UPDATE Destroying tables with DROP Destroying tables with DROP

5 Retrieving Data from One Table

6 The SELECT SELECT column1, column2, … FROM tablename {WHERE condition } {ORDER BY column }

7 A few simple SELECTs SELECT * FROM contacts; SELECT * FROM contacts; –Display all records in the ‘contacts’ table SELECT contactid, name FROM contacts; SELECT contactid, name FROM contacts; –Display only the contactid and name SELECT contactname FROM contacts ORDER BY contactname; SELECT contactname FROM contacts ORDER BY contactname; –Display only the contactname, sort by name

8 The SELECT SELECT* FROMTABLE; 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

9 Refining selections with WHERE The WHERE “clause” allows you to select records based on a condition The WHERE “clause” allows you to select records based on a condition SELECT * FROM contacts WHERE age<10; SELECT * FROM contacts WHERE age<10; –Display records from contacts where age<10 SELECT * FROM contacts WHERE age BETWEEN 18 AND 35; SELECT * FROM contacts WHERE age BETWEEN 18 AND 35; –Display records where age is 18-35

10 WHERE with “AND” SELECT* FROMSALES_DATA WHEREDepartment = 'Water Sports' ANDBuyer = 'Nancy Meyers'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

11 WHERE with “OR” SELECT* FROMSALES_DATA WHEREDepartment = 'Camping' ORDepartment = 'Climbing'; ORDepartment = 'Climbing'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

12 WHERE with “IN” SELECT* FROMSALES_DATA WHEREBuyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

13 WHERE with “BETWEEN” SELECT* FROMORDERS WHEREExtendedPrice BETWEEN 100 AND 200; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

14 WHERE with “LIKE” SELECT* FROMSALES_DATA WHEREBuyer LIKE 'Pete*'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

15 WHERE with “LIKE” SELECT* FROMSALES_DATA WHERESKU_Description LIKE ‘*Tent*'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

16 Time for Hands-On Photo © Charles Darwin University 2005

17 SQL Built-in Functions There are five SQL Built-in Functions: There are five SQL Built-in Functions: –COUNT –SUM –AVG –MIN –MAX Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

18 SQL Built-in Functions SELECTSUM (ExtendedPrice) ASOrder3000Sum FROMORDERS WHEREOrderNumber = 3000; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall Column Heading

19 SQL Built-in Functions (Continued) SELECTSUM (ExtendedPrice) AS OrderItemSum, AVG (ExtendedPrice) AS OrderItemAvg, MIN (ExtendedPrice) AS OrderItemMin, MAX (ExtendedPrice) AS OrderItemMax FROMORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

20 SQL Built-in Functions (Continued) SELECTCOUNT(*) AS NumRows FROMORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

21 Arithmetic in SELECT Statements SELECTQuantity * Price AS EP, ExtendedPrice FROMORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

22 Time for Hands-On Photo © Charles Darwin University 2005

23 Retrieving Data from More Than One Table

24 Make a list of students including phone number and address Make a list of students including phone number and address Join Person Phone Address

25 Joining together tables Person PersonIDNameAddressID 1Joe10 2Jane20 3Chris30 AddressAddressIDCompanyStreetZip 10ABC 12 Road 12345 20XYZ 45 Road 14454 30PDQ 78 Road 14423 PhonePhoneIDPersonIDPhoneNum 10015555532 20015552234 30015553211 40025553421 50035552341 60035556655

26 The SELECT with a JOIN Clause SELECTTABLE1.Column, TABLE2.Column TABLE2.Column FROMTABLE1, TABLE2 WHERETABLE1.PrimaryKey = TABLE2.PrimaryKey; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

27 The SELECT with a JOIN Clause SELECTPERSON.Name, PHONE.PhoneNum FROMPERSON, PHONE WHEREPERSON.PersonID = PHONE.PersonID; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

28 The SELECT with two JOIN Clauses SELECTPERSON.Name, PHONE.PhoneNum,ADDRESS.Zip FROMPERSON, PHONE, ADDRESS WHEREPERSON.PersonID = PHONE.PersonID ANDPERSON.AddressID = ADDRESS.AddressID Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

29 SELECTP.Name, PH.PhoneNum FROMPERSON P, PHONE PH WHEREP.PersonID = PH.PersonID Alias (Shorthand) for Table Name The SELECT with a JOIN Clause

30 Time for Hands-On Photo © Charles Darwin University 2005

31 Writing SQL in MS Access 2007

32 Click “Create”

33 Click “Query Design”

34 Click “Close”

35 Click “SQL”

36 Click “ ! Run”


Download ppt "Database Queries aka SQL (pronounced “ sequel ” )."

Similar presentations


Ads by Google