Database Queries aka SQL (pronounced “ sequel ” )
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
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
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
Retrieving Data from One Table
The SELECT SELECT column1, column2, … FROM tablename {WHERE condition } {ORDER BY column }
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
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
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
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
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
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
WHERE with “BETWEEN” SELECT* FROMORDERS WHEREExtendedPrice BETWEEN 100 AND 200; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall
WHERE with “LIKE” SELECT* FROMSALES_DATA WHEREBuyer LIKE 'Pete*'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall
WHERE with “LIKE” SELECT* FROMSALES_DATA WHERESKU_Description LIKE ‘*Tent*'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall
Time for Hands-On Photo © Charles Darwin University 2005
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
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
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
SQL Built-in Functions (Continued) SELECTCOUNT(*) AS NumRows FROMORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall
Arithmetic in SELECT Statements SELECTQuantity * Price AS EP, ExtendedPrice FROMORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall
Time for Hands-On Photo © Charles Darwin University 2005
Retrieving Data from More Than One Table
Make a list of students including phone number and address Make a list of students including phone number and address Join Person Phone Address
Joining together tables Person PersonIDNameAddressID 1Joe10 2Jane20 3Chris30 AddressAddressIDCompanyStreetZip 10ABC 12 Road XYZ 45 Road PDQ 78 Road PhonePhoneIDPersonIDPhoneNum
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
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
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
SELECTP.Name, PH.PhoneNum FROMPERSON P, PHONE PH WHEREP.PersonID = PH.PersonID Alias (Shorthand) for Table Name The SELECT with a JOIN Clause
Time for Hands-On Photo © Charles Darwin University 2005
Writing SQL in MS Access 2007
Click “Create”
Click “Query Design”
Click “Close”
Click “SQL”
Click “ ! Run”