Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Statements: Queries. Relational Databases Relational Databases organize the data in tables with rows and columns. This is similar to the organization.

Similar presentations


Presentation on theme: "SQL Statements: Queries. Relational Databases Relational Databases organize the data in tables with rows and columns. This is similar to the organization."— Presentation transcript:

1 SQL Statements: Queries

2 Relational Databases Relational Databases organize the data in tables with rows and columns. This is similar to the organization of spreadsheets. 111SmithJoe1 112ElSawaJane1 113ChiJill3 114MontiJack1 StudentNo.LastNameFirstNameStatus STUDENT TABLE Row Column

3 Relational Database Operations. There are three fundamental operations on pre- defined (already created) tables.  Query. Retrieval of data from one or more columns from one or more rows typically based on some condition.  Insert. Insert an entire row with all columns into the table.  Update. Modification of one or more columns from one or more rows typically based on some condition.  Delete. Removal of a row typically based on some condition

4 Language Note! One confusing term in Relational Database usage is update. When used as a general term it means any operation that can modify the state of a table. The means that SQL UPDATE statements, SQL INSERT statements and SQL DELETE statements can be considered as updates (in a general sense). As a results statements that alter pre-defined tables are divided into two categories, queries and updates.

5 Types of Statements. Queries. In SQL these are executed using the SELECT statements. Updates. In SQL these are executed typically using INSERT to add entire rows to a table or UPDATE to modify columns in pre-existing rows in a table or DELETE to delete entire rows from a table.

6 Querying a Database You can retrieve information from a database by executing a query. Queries involve requesting the return of one or all columns for each row that matches a condition. A query returns a result set which is a collection of rows with each row containing only the columns requested by query.

7 The Select Statement A select statement is used to query a database. Select statements typically (but not always) require:  Which columns in a table or tables you wish to retrieve.  Which tables you will retrieve the information from.  Any special conditions you may attach to the query. Select statements must end with a semi-colon.

8 SELECT Statement Select Lastname,Firstname From Student Where Status=1; Columns to Retrieve Table to Query Special Conditions to refine search.

9 SELECT Statement Select * From Student; Retrieve all columns Table to Query No Condition is supplied so columns from ALL rows in Student will be returned…i.e. unconditional query.

10 Special Syntax Notes. Enclose string constants used in comparisons in single quotes not double quotes.  Select * from tablename where sfield=‘Fred’; Numeric constants do not need quotes.  Select * from tablename where numfield=27;

11 Simple Queries/No Conditions 111SmithJoe1 112ElSawaJane1 113ChiJill3 114MontiJack1 StudentNo.LastNameFirstNameStatus STUDENT TABLE Select LastName,FirstName from Student; LastNameFirstName 4 Two-Column Rows in the Result Set SmithJoe ElSawaJane ChiJill MontiJack

12 Simple Queries w/Conditions 111SmithJoe1 112ElSawaJane1 113ChiJill3 114MontiJack1 StudentNo.LastNameFirstNameStatus STUDENT TABLE Select LastName,Status from Student Where Status=1 ; LastNameStatus 3 Two-Column Rows in the Result Set Smith1 ElSawa1 Monti1

13 Simple Queries w/ Compound Condition 111SmithJoe1 112ElSawaJane1 113ChiJill3 114MontiJack1 StudentNo.LastNameFirstNameStatus STUDENT TABLE Select LastName,Status from Student Where Status=2; LastNameStatus No rows returned that satisfy condition. NULL SET

14 Conditional Operations -Conventional ExpressionEvaluation A = BTrue if A equals B A != BTrue if A does not equal B A <= BTrue if A is less than or equal to B A > BTrue if A is greater than or equal to B A < BTrue if A is less than B A > BTrue if A is greater than B

15 Conditional Operations - Special ExpressionEvaluation A BTrue if A is equal to B (NULL Safe) A IS NULLTrue if A is NULL A IS NOT NULLTrue if A is not NULL A BETWEEN M AND NTrue if A is between values M and N A NOT BETWEEN M AND NTrue if A is not between values M and N A IN(value, value2,...)True if A is one of the listed values A NOT IN (value, value2,...)True if A is not one of the listed values

16 Compound Operators. And returns row only if both conditions in compound statement are true. Or returns row if either condition in compound statement is true. You can negate (NOT) a compound condition by enclosing the condition in parentheses.

17 Simple Queries w/ Compound Condition 111SmithJoe1 112ElSawaJane1 113ChiJill3 114MontiJack1 StudentNo.LastNameFirstNameStatus STUDENT TABLE Select LastName,Status from Student Where Status=1 And FirstName=‘Jane’ ; LastNameStatus 1 Two-Column Row in the Result Set ElSawa1

18 Simple Queries w/Conditions 111SmithJoe1 112ElSawaJane1 113ChiJill3 114MontiJack1 StudentNo.LastNameFirstNameStatus STUDENT TABLE Select LastName,Status from Student Where Status=3 Or FirstName=‘Jane’ ; LastNameStatus 2 Two-Column Rows in the Result Set ElSawa1 Chi3

19 Simple Queries w/ Compound Condition 111SmithJoe1 112ElSawaJane1 113ChiJill3 114MontiJack1 STUDENT TABLE Select LastName,Status from Student Where Status=1 And FirstName=‘Jill’ ; LastNameStatus No rows returned that satisfy condition. NULL SET


Download ppt "SQL Statements: Queries. Relational Databases Relational Databases organize the data in tables with rows and columns. This is similar to the organization."

Similar presentations


Ads by Google