+ All Categories
Home > Documents > LOGO 1 Lab_03: Basic SQL. 2 Outline SQL BETWEEN Operator SQL LIKE Operator SQL IN Operator.

LOGO 1 Lab_03: Basic SQL. 2 Outline SQL BETWEEN Operator SQL LIKE Operator SQL IN Operator.

Date post: 05-Jan-2016
Category:
Upload: aubrey-pierce
View: 234 times
Download: 4 times
Share this document with a friend
22
LOGO 1 Lab_03: Basic SQL Lab_03: Basic SQL
Transcript
Page 1: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

LOGO

1

Lab_03: Basic SQLLab_03: Basic SQLLab_03: Basic SQLLab_03: Basic SQL

Page 2: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

2

OutlineSQL BETWEEN OperatorSQL LIKE OperatorSQL IN Operator

Page 3: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

3

Operators Allowed in the WHERE Clause

Operator Description

= Equal

<> Not equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

BETWEEN Between an inclusive range

LIKE Search for a pattern

INIf you know the exact value you want to return for at least one of the columns

With the WHERE clause, the following operators can be used:

Note: In some versions of SQL the <> operator may be written as !=

Page 4: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

4

SQL AND & OR OperatorsThe AND & OR operators are

used to filter records based on more than one condition.

The AND & OR OperatorsThe AND operator displays a record if both the first condition and the second condition is true.The OR operator displays a record if either the first condition or the second condition is true.

Page 5: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

The "Persons" table:

Now we want to select only the persons with the first name equal to "Tove" AND the last name equal to "Svendson":

5

AND Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

3Pettersen

KariStorgt 20

Stavanger

Page 6: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

6

AND Operator Example

P_IdLastName

FirstName

Address City

2Svendson

ToveBorgvn 23

Sandnes

We use the following SELECT statement:

SELECT * FROM Persons

WHERE FirstName='Tove'

AND LastName='Svendson'

The result-set will look like this:

Page 7: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

7

OR Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

Now we want to select only the persons with the first name equal to "Tove" OR the first name equal to "Ola":We use the following SELECT statement:

SELECT * FROM PersonsWHERE FirstName='Tove'

OR FirstName='Ola'

The result-set will look like this:

Page 8: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

8

Combining AND & OR

P_IdLastName

FirstName

Address City

2Svendson

ToveBorgvn 23

Sandnes

You can also combine AND and OR (use parenthesis to form complex expressions).

Now we want to select only the persons with the last name equal to "Svendson" AND the first name equal to "Tove" OR to "Ola":We use the following SELECT statement:

SELECT * FROM Persons WHERE

LastName='Svendson'AND (FirstName='Tove' OR

FirstName='Ola')

The result-set will look like this:

Page 9: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

9

SQL BETWEEN Operator

SQL BETWEEN OperatorThe BETWEEN operator is used in a WHERE clause to select a range of data between two values.

The BETWEEN OperatorThe BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates.

SQL BETWEEN Syntax

SELECT column_name(s)FROM table_nameWHERE column_nameBETWEEN value1 AND value2

Page 10: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

10

BETWEEN Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

3Pettersen

KariStorgt 20

Stavanger

The "Persons" table:

Now we want to select the persons with a last name alphabetically between "Hansen" and "Pettersen" from the table above.

Page 11: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

11

BETWEEN Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

We use the following SELECT statement:

SELECT * FROM PersonsWHERE LastNameBETWEEN 'Hansen' AND 'Pettersen'

The result-set will look like this:

Page 12: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

12

BETWEEN Operator

Note: The BETWEEN operator is treated differently in different databases!In some databases, persons with the LastName of "Hansen" or

"Pettersen" will not be listed, because the BETWEEN operator only selects fields that are between and excluding the test values.

In other databases, persons with the LastName of "Hansen" or "Pettersen" will be listed, because the BETWEEN operator selects fields that are between and including the test values.

And in other databases, persons with the LastName of "Hansen" will be listed, but "Pettersen" will not be listed (like the example above), because the BETWEEN operator selects fields between the test values, including the first test value and excluding the last test value.Therefore: Check how your database treats the BETWEEN operator.

Page 13: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

13

BETWEEN Operator Example

P_IdLastName

FirstName

Address City

2Svendson

ToveBorgvn 23

Sandnes

3Pettersen

KariStorgt 20

Stavanger

To display the persons outside the range in the previous example, use NOT BETWEEN:

SELECT * FROM PersonsWHERE LastNameNOT BETWEEN 'Hansen'

AND 'Pettersen'

The result-set will look like this:

Page 14: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

14

SQL LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

The LIKE OperatorThe LIKE operator is used to search for a specified pattern in a column.

SQL LIKE Syntax

SELECT column_name(s)FROM table_nameWHERE column_name LIKE pattern

Page 15: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

15

LIKE Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

3Pettersen

KariStorgt 20

Stavanger

The "Persons" table:

Now we want to select the persons living in a city that starts with "s" from the table above.

Page 16: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

16

LIKE Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

3Pettersen

KariStorgt 20

Stavanger

We use the following SELECT statement:

SELECT * FROM PersonsWHERE City LIKE 's%'

The "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.The result-set will look like this:

Page 17: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

17

LIKE Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

Next, we want to select the persons living in a city that ends with an "s" from the "Persons" table.

We use the following SELECT statement:

SELECT * FROM PersonsWHERE City LIKE '%s'

The result-set will look like this:

Page 18: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

18

LIKE Operator Example

P_IdLastName

FirstName

Address City

3Pettersen

KariStorgt 20

Stavanger

Next, we want to select the persons living in a city that contains the pattern "tav" from the "Persons" table.

We use the following SELECT statement:

SELECT * FROM PersonsWHERE City LIKE '%tav%'

The result-set will look like this:

Page 19: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

19

LIKE Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

It is also possible to select the persons living in a city that does NOT contain the pattern "tav" from the "Persons" table, by using the NOT keyword.

We use the following SELECT statement:

SELECT * FROM PersonsWHERE City NOT LIKE '%tav%'

The result-set will look like this:

Page 20: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

20

SQL IN Operator

The IN OperatorThe IN operator allows you to specify multiple values in a WHERE clause.

SQL IN Syntax

SELECT column_name(s)FROM table_nameWHERE column_name IN (value1,value2,...)

Page 21: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

21

IN Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

2Svendson

ToveBorgvn 23

Sandnes

3Pettersen

KariStorgt 20

Stavanger

The "Persons" table:

Now we want to select the persons with a last name equal to "Hansen" or "Pettersen" from the table above.

Page 22: LOGO 1 Lab_03: Basic SQL. 2 Outline  SQL BETWEEN Operator  SQL LIKE Operator  SQL IN Operator.

22

IN Operator Example

P_IdLastName

FirstName

Address City

1 Hansen OlaTimoteivn 10

Sandnes

3Pettersen

KariStorgt 20

Stavanger

We use the following SELECT statement:

SELECT * FROM PersonsWHERE LastName IN ('Hansen','Pettersen')

The result-set will look like this:


Recommended