LAB #3 QUERYING DATA Delete and update Delete a record DELETE FROM vet_appt WHERE vet_appt_id = 3; Update a record UPDATE animal SET’ gender = “MS” WHERE animal_id = 3 What would happen if you didn’t include a where clause with a delete or update record command? 2 Query Data From Database We use the SELECT statement to retrieve information from the database. SELECT attribute/s FROM table/s WHERE (condition) Simple Select List all the record s (rows) and all attributes (columns) in table: * - WILDCARD SELECT * FROM tableName; Select * from animal Select Attributes/Fields SELECT fieldNameA, fieldNameB FROM tableName; The order in which you list the attributes will be the order the attributes are displayed. SELECT flname, ffname FROM faculty; SELECT ffname, flname FROM faculty; Select Distinct SELECT sclass FROM student; SC -SR SR JR SO SO FR SELECT DISTINCT sclass FROM student; SC -SR JR SO FR Filtering Data: Clause The WHERE SELECT fieldName FROM tableName WHERE fieldName [some condition]; Returns only those records that match the condition Example 1: Number SELECT sfname, slname, fid FROM student WHERE fid = 1; Example 2: String SELECT sfname, slname FROM student WHERE sclass= 'SO'; Remember Values in strings are Case sensitive Comparison Operators … A and B - Intersect A OR B –UNION: all of A (including yellow, gray and purple) all of B (including aqua, and purple and gray) C and B 12 Logical Operators - OR SELECT fieldNameA, fieldNameB FROM tableName WHERE attribute = X or attribute = Y; Must match EITHER Select client_fname, client_lname, add1, city, state, zip From vet_client Where state = ‘NJ’ OR state = ‘PA’ ; Arithmetic Operators – Rules of Precedence Arithmetic operators and rules of precedence Table 5.5 14 Logical Operators – AND SELECT fieldNameA, fieldNameB FROM tableName WHERE attribute = X AND attribute = Y; Must match BOTH Select client_fname, client_lname, add1, city, state, zip From vet_client Where state = ‘NJ’ AND state= ‘PA’ ; Would this work? Logical Operators – AND Select client_fname, client_lname, add1, city, state, zip From vet_client Where city= ‘Philadelphia’ AND state= ‘PA’ ; Comparison Operators Symbol > >= < <= !=, <> IN Description Greater Than Greater than or equal to Less Than Less than or equal to Not equal to Looks for records with a value which is equal to one of the values in a list BETWEEN Looks for records with a value between two different values EXAMPLES of Comparison Operator SELECT appt_date, appt_type, temperature FROM vet_appt WHERE (temperature >= 102 ; SELECT appt_date, appt_type, temperature FROM vet_appt WHERE appt_type in (1, 2, 3)’ Logical Operators – AND SELECT fieldNameA, fieldNameB FROM tableName WHERE criteria Select client_fname, client_lname, add1, city, state, zip From vet_client Where state = ‘NJ’ AND fieldNameA = ‘PA’ ; Would this work? Wildcard Characters _ : 1 character %: multiple characters SELECT client_lname FROM vet_client WHERE client_lname LIKE ‘_emple’; SELECT client_lname FROM vet_client WHERE client_lname LIKE ‘%d% NULL/NOT NULL Operator SELECT student_fname, student_lname, FROM student WHERE student_mname IS NULL SELECT student_fname, student_lname, FROM student WHERE student_mname IS NOT NULL Sorting the Output: ORDER BY SELECT client_lname, client_fname ORDER BY client_lname desc; Can Nest order ASC is default Example: SELECT bldg_code, room, capacity FROM location WHERE capacity >= 40 ORDER BY capacity DESC, bldg_code ASC ; BLDG_CODE ROOM CAPACITY -------------------------------------------------------SP 101 150 BUS 211 55 BUS 105 42 SP 202 40 To Summarize SELECT (distinct) fieldName/s FROM tableName WHERE fieldName [some condition] ORDER BY fieldName ASC/DESC;