Lab #3 Querying Data

24
LAB #3 QUERYING DATA

description

Lab #3 Querying Data. 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 - PowerPoint PPT Presentation

Transcript of Lab #3 Querying Data

Page 1: Lab #3 Querying Data

LAB #3QUERYING DATA

Page 2: Lab #3 Querying Data

Query Data From Database

We use the SELECT statement to retrieve information from the database.

SELECT attribute/s

FROM table/s

WHERE (condition)

Page 3: Lab #3 Querying Data

Simple Select

List all the record s (rows) and all attributes (columns) in table:

* - WILDCARD

SELECT * FROM tableName;

Select * from animal

Page 4: Lab #3 Querying Data

Select Attributes/Fields

SELECT fieldNameA, fieldNameBFROM tableName;

The order in which you list the attributes will be the order the attributes are displayed.

Page 5: Lab #3 Querying Data

SELECT flname, ffname FROM faculty; 

Page 6: Lab #3 Querying Data

SELECT ffname, flname FROM faculty; 

Page 7: Lab #3 Querying Data

Select Distinct

SELECT sclass FROM student;

SC--SRSRJRSOSOFR 

SELECT DISTINCT sclass FROM student;

SC--SRJRSOFR 

Page 8: Lab #3 Querying Data

Filtering Data: The WHERE Clause

SELECT fieldName FROM tableNameWHERE fieldName [some

condition];

Returns only those records that match the condition

Page 9: Lab #3 Querying Data

Example 1: Number

SELECT sfname, slname, fidFROM studentWHERE fid = 1;

 

Page 10: Lab #3 Querying Data

Example 2: String

SELECT sfname, slnameFROM studentWHERE sclass= 'SO';

RememberValues in strings are Case sensitive 

Page 12: Lab #3 Querying Data

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_clientWhere state = ‘NJ’ OR state = ‘PA’ ;

Page 13: Lab #3 Querying Data

13

Arithmetic Operators – Rules of Precedence

Arithmetic operators and rules of precedence

Table 5.5

Page 14: Lab #3 Querying Data

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_clientWhere state = ‘NJ’ AND state= ‘PA’ ;

Would this work?

Page 15: Lab #3 Querying Data

Logical Operators – AND

Select client_fname, client_lname, add1, city, state, zip

From vet_clientWhere city= ‘Philadelphia’ AND state=

‘PA’ ;

Page 16: Lab #3 Querying Data

Comparison Operators

Symbol Description> Greater Than>= Greater than or equal to< Less Than<= Less than or equal to!=, <> Not equal toIN Looks for records with a value which is

equal to one of the values in a listBETWEEN Looks for records with a value between

two different values

Page 17: Lab #3 Querying Data

SELECT appt_date, appt_type, temperatureFROM vet_apptWHERE (temperature >= 102 ;

SELECT appt_date, appt_type,temperatureFROM vet_apptWHERE appt_type in (1, 2, 3)’

EXAMPLES of Comparison Operator

Page 18: Lab #3 Querying Data

Logical Operators – AND

SELECT fieldNameA, fieldNameB FROM tableName WHERE criteria

Select client_fname, client_lname, add1, city, state, zip

From vet_clientWhere state = ‘NJ’ AND fieldNameA = ‘PA’ ;

Would this work?

Page 19: Lab #3 Querying Data

Wildcard Characters

_ : 1 character %: multiple characters

SELECT client_lnameFROM vet_client

WHERE client_lname LIKE ‘_emple’;

SELECT client_lnameFROM vet_client

WHERE client_lname LIKE ‘%d%

Page 20: Lab #3 Querying Data

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

Page 21: Lab #3 Querying Data

Sorting the Output: ORDER BY

SELECT client_lname, client_fnameORDER BY client_lname desc;

Can Nest orderASC is default

Page 22: Lab #3 Querying Data

Example:

SELECT bldg_code, room, capacityFROM location WHERE capacity >= 40ORDER BY capacity DESC, bldg_code ASC ;

BLDG_CODE ROOM CAPACITY--------------------------------------------------------SP 101 150BUS 211 55BUS 105 42SP 202 40

Page 23: Lab #3 Querying Data

To Summarize

SELECT (distinct)fieldName/s

FROM tableName

WHERE fieldName [some condition]

ORDER BY fieldName ASC/DESC;

Page 24: Lab #3 Querying Data

24

Delete and update

Delete a record

Update a recrod

DELETE FROM vet_apptWHERE vet_appt_id = 3;

UPDATE animalSET’ gender; = “MS”WHERE animal_id = 3