Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Database Queries aka SQL (pronounced “sequel”) What is SQL? SQL = Standard Query Language 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” 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 SQL (Structured Query Language) allows a user to specify the conditions without writing complex code Basic SQL Commands Creating tables with CREATE Adding data with INSERT Viewing data with SELECT Removing data with DELETE Modifying data with UPDATE 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; – Display all records in the ‘contacts’ table SELECT contactid, name FROM contacts; – Display only the contactid and name SELECT contactname FROM contacts ORDER BY contactname; – Display only the contactname, sort by name The SELECT ALL Columns SELECT * FROM TABLE; End of Command No WHERE Clause so ALL Rows are Returned Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall Refining selections with WHERE The WHERE “clause” allows you to select records based on a condition SELECT * FROM contacts WHERE age<10; – Display records from contacts where age<10 SELECT * FROM contacts WHERE age BETWEEN 18 AND 35; – Display records where age is 18-35 WHERE with “AND” SELECT FROM WHERE AND * SALES_DATA Department = 'Water Sports' Buyer = 'Nancy Meyers'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall WHERE with “OR” SELECT FROM WHERE OR * SALES_DATA Department = 'Camping' Department = 'Climbing'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall WHERE with “IN” SELECT FROM WHERE * SALES_DATA Buyer 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 FROM WHERE * ORDERS ExtendedPrice BETWEEN 100 AND 200; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall WHERE with “LIKE” SELECT * FROM SALES_DATA WHERE Buyer LIKE 'Pete*'; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall WHERE with “LIKE” SELECT * FROM SALES_DATA WHERE SKU_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: – COUNT – SUM – AVG – MIN – MAX Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall SQL Built-in Functions SELECT SUM (ExtendedPrice) AS Order3000Sum FROM ORDERS WHERE OrderNumber = 3000; Column Heading Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall SQL Built-in Functions (Continued) SELECT FROM SUM (ExtendedPrice) AS OrderItemSum, AVG (ExtendedPrice) AS OrderItemAvg, MIN (ExtendedPrice) AS OrderItemMin, MAX (ExtendedPrice) AS OrderItemMax ORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall SQL Built-in Functions (Continued) SELECT COUNT(*) AS NumRows FROM ORDERS; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall Arithmetic in SELECT Statements SELECT Quantity * Price AS EP, ExtendedPrice FROM ORDERS; 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 Join Make a list of students including phone number and address Person Phone Address Joining together tables Phone Person PhoneID PersonID PhoneNum 100 1 5555532 200 1 1 5553211 400 2 5553421 600 3 3 Name AddressID 1 Joe 10 2 Jane 20 3 Chris 30 5552234 300 500 PersonID Address AddressID Company Street Zip 10 ABC 12 Road 12345 20 XYZ 45 Road 14454 30 PDQ 78 Road 14423 5552341 5556655 The SELECT with a JOIN Clause SELECT TABLE1.Column, TABLE2.Column FROM TABLE1, TABLE2 WHERE TABLE1.PrimaryKey = TABLE2.PrimaryKey; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall The SELECT with a JOIN Clause SELECT PERSON.Name, PHONE.PhoneNum FROM PERSON, PHONE WHERE PERSON.PersonID = PHONE.PersonID; Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall The SELECT with two JOIN Clauses SELECT PERSON.Name, PHONE.PhoneNum, ADDRESS.Zip FROM PERSON, PHONE, ADDRESS WHERE PERSON.PersonID = PHONE.PersonID AND PERSON.AddressID = ADDRESS.AddressID Adapted from DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall The SELECT with a JOIN Clause SELECT P.Name, PH.PhoneNum FROM PERSON P , PHONE PH WHERE P.PersonID = PH.PersonID Alias (Shorthand) for Table Name 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”