Вы находитесь на странице: 1из 31
Introduction to SQL(Cont,) CSC 220
Introduction to SQL(Cont,)
CSC 220

Dr. Ghulam Mustafa (BULC)

Dr. Ghulam Mustafa

Department of Computer Sciences Bahria University Lahore Campus

March 13, 2019

Week5

March 13, 2019 1 / 31
March 13, 2019
1 / 31

The SQL AND, OR and NOT Operators

Outline

The SQL AND, OR and NOT Operators

1

The SQL AND, OR and NOT Operators The SQL AND, OR and NOT Operators

2

Combining AND, OR and NOT Combining AND, OR and NOT

3

The SQL ORDER BY The SQL ORDER BY

4

Scenarios

Scenario-1

Scenario-2

5

The SQL WHERE BETWEEN syntax The SQL WHERE BETWEEN syntax

6

Takeaway!

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 2 / 31
March 13, 2019
2 / 31

The SQL AND, OR and NOT Operators

The SQL AND, OR and NOT Operators

The SQL AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition

The AND operator displays a record if all the conditions separated by AND are TRUE.

The OR operator displays a record if any of the conditions separated by OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 3 / 31
March 13, 2019
3 / 31

The SQL AND, OR and NOT Operators

SQL AND,

AND Syntax

The SQL AND, OR and NOT Operators

NOT Operators SQL AND, AND Syntax The SQL AND, OR and NOT Operators Dr. Ghulam Mustafa

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 4 / 31
March 13, 2019
4 / 31

The SQL AND, OR and NOT Operators

SQL OR,

OR Syntax

The SQL AND, OR and NOT Operators

NOT Operators SQL OR, OR Syntax The SQL AND, OR and NOT Operators Dr. Ghulam Mustafa

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 5 / 31
March 13, 2019
5 / 31

The SQL AND, OR and NOT Operators

SQL NOT,

NOT Syntax

The SQL AND, OR and NOT Operators

NOT Operators SQL NOT, NOT Syntax The SQL AND, OR and NOT Operators Dr. Ghulam Mustafa

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 6 / 31
March 13, 2019
6 / 31

The SQL AND, OR and NOT Operators

Demo Database

The SQL AND, OR and NOT Operators

and NOT Operators Demo Database The SQL AND, OR and NOT Operators Dr. Ghulam Mustafa (BULC)

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 7 / 31
March 13, 2019
7 / 31

The SQL AND, OR and NOT Operators

Scenario

The SQL AND, OR and NOT Operators

Selects all the customers from the country "Mexico", in the "Customers" table?

Select all records where the City column has the value "Berlin".

selects all fields from "Customers" where country is "Germany" AND city is "Berlin"

selects all fields from "Customers" where city is "Berlin" OR "München"

selects all fields from "Customers" where country is NOT "Germany":

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 8 / 31
March 13, 2019
8 / 31

Outline

Combining AND, OR and NOT

Combining AND, OR and NOT

1

The SQL AND, OR and NOT Operators The SQL AND, OR and NOT Operators

2

Combining AND, OR and NOT Combining AND, OR and NOT

3

The SQL ORDER BY The SQL ORDER BY

4

Scenarios

Scenario-1

Scenario-2

5

The SQL WHERE BETWEEN syntax The SQL WHERE BETWEEN syntax

6

Takeaway!

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 9 / 31
March 13, 2019
9 / 31

Combining AND, OR and NOT

Combining AND, OR and NOT

Combining AND, OR and NOT

You can also combine the AND, OR and NOT operators.

Write SQL statement that selects all fields from "Customers" where country is "Germany" AND city must be "Berlin" OR "München" (use parenthesis to form complex expressions):

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 10 / 31
March 13, 2019
10 / 31

Example

Combining AND, OR and NOT

Combining AND, OR and NOT

Example Combining AND, OR and NOT Combining AND, OR and NOT Dr. Ghulam Mustafa (BULC) Week5

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 11 / 31
March 13, 2019
11 / 31

Combining AND, OR and NOT

Combining AND, OR and NOT

Combining AND, OR and NOT

Write SQL statement that selects all fields from "Customers" where country is NOT "Germany" and NOT "USA":.

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 12 / 31
March 13, 2019
12 / 31

Example

Combining AND, OR and NOT

Combining AND, OR and NOT

Example Combining AND, OR and NOT Combining AND, OR and NOT Dr. Ghulam Mustafa (BULC) Week5

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 13 / 31
March 13, 2019
13 / 31

Outline

The SQL ORDER BY

The SQL ORDER BY

1

The SQL AND, OR and NOT Operators The SQL AND, OR and NOT Operators

2

Combining AND, OR and NOT Combining AND, OR and NOT

3

The SQL ORDER BY The SQL ORDER BY

4

Scenarios

Scenario-1

Scenario-2

5

The SQL WHERE BETWEEN syntax The SQL WHERE BETWEEN syntax

6

Takeaway!

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 14 / 31
March 13, 2019
14 / 31

The SQL ORDER BY

The SQL ORDER BY

The SQL ORDER BY

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default.

To sort the records in descending order, use the DESC keyword.

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 15 / 31
March 13, 2019
15 / 31

The SQL ORDER BY

ORDER BY Syntax

The SQL ORDER BY

The SQL ORDER BY ORDER BY Syntax The SQL ORDER BY Dr. Ghulam Mustafa (BULC) Week5

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 16 / 31
March 13, 2019
16 / 31

Example

The SQL ORDER BY

The SQL ORDER BY

Write the SQL statement that selects all customers from the "Customers" table, sorted by the "Country" column?

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 17 / 31
March 13, 2019
17 / 31

Example

The SQL ORDER BY

The SQL ORDER BY

Example The SQL ORDER BY The SQL ORDER BY Dr. Ghulam Mustafa (BULC) Week5 March 13,

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 18 / 31
March 13, 2019
18 / 31

Scenario

The SQL ORDER BY

The SQL ORDER BY

Write the SQL statement that selects all customers from the "Customers" table, sorted DESCENDING by the "Country" column?

Write the SQL statement that selects all customers from the "Customers" table, sorted by the "Country" and the "CustomerName" column.

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 19 / 31
March 13, 2019
19 / 31

The SQL ORDER BY

ORDER BY Several Columns

The SQL ORDER BY

Write SQL statement that selects all customers from the "Customers" table, sorted ascending by the "Country" and descending by the "CustomerName" column?

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 20 / 31
March 13, 2019
20 / 31

Outline

Scenarios

Scenario-1

1 The SQL AND, OR and NOT Operators The SQL AND, OR and NOT Operators

2 Combining AND, OR and NOT Combining AND, OR and NOT

3 The SQL ORDER BY The SQL ORDER BY

4 Scenarios

Scenario-1

Scenario-2

5 The SQL WHERE BETWEEN syntax The SQL WHERE BETWEEN syntax

6 Takeaway!

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 21 / 31
March 13, 2019
21 / 31

Scenario-1

Scenarios

Scenario-1

Problem: List all customers from Spain or France from the table given below

Problem: List all customers that are not from the USA

given below Problem: List all customers that are not from the USA Dr. Ghulam Mustafa (BULC)

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 22 / 31
March 13, 2019
22 / 31

Outline

Scenarios

Scenario-2

1 The SQL AND, OR and NOT Operators The SQL AND, OR and NOT Operators

2 Combining AND, OR and NOT Combining AND, OR and NOT

3 The SQL ORDER BY The SQL ORDER BY

4 Scenarios

Scenario-1

Scenario-2

5 The SQL WHERE BETWEEN syntax The SQL WHERE BETWEEN syntax

6 Takeaway!

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 23 / 31
March 13, 2019
23 / 31

Scenario-2

Scenarios

Scenario-2

Problem: List all orders that not between $50 and $15000

Problem: List all orders that not between $50 and $15000 Dr. Ghulam Mustafa (BULC) Week5 March

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 24 / 31
March 13, 2019
24 / 31

Scenarios

Scenario-2

Scenario-2 Soloution

Problem: List all orders that not between $50 and $15000

Soloution Problem: List all orders that not between $50 and $15000 Dr. Ghulam Mustafa (BULC) Week5

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 25 / 31
March 13, 2019
25 / 31

Outline

The SQL WHERE BETWEEN syntax

The SQL WHERE BETWEEN syntax

1

The SQL AND, OR and NOT Operators The SQL AND, OR and NOT Operators

2

Combining AND, OR and NOT Combining AND, OR and NOT

3

The SQL ORDER BY The SQL ORDER BY

4

Scenarios

Scenario-1

Scenario-2

5

The SQL WHERE BETWEEN syntax The SQL WHERE BETWEEN syntax

6

Takeaway!

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 26 / 31
March 13, 2019
26 / 31

The SQL WHERE BETWEEN syntax

The SQL WHERE BETWEEN syntax

The SQL WHERE BETWEEN syntax

The general syntax is:

BETWEEN syntax The SQL WHERE BETWEEN syntax The general syntax is: Dr. Ghulam Mustafa (BULC) Week5

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 27 / 31
March 13, 2019
27 / 31

The SQL WHERE BETWEEN syntax

The SQL WHERE BETWEEN syntax

SQL WHERE BETWEEN Examples

Problem: List all products between $10 and $20

BETWEEN Examples Problem: List all products between $10 and $20 Dr. Ghulam Mustafa (BULC) Week5 March

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 28 / 31
March 13, 2019
28 / 31

The SQL WHERE BETWEEN syntax

The SQL WHERE BETWEEN syntax

SQL WHERE BETWEEN Examples (Solution)

Problem: List all products between $10 and $20

Examples (Solution) Problem: List all products between $10 and $20 Dr. Ghulam Mustafa (BULC) Week5 March

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 29 / 31
March 13, 2019
29 / 31

Takeaway!

1 The SQL AND, OR and NOT Operators The SQL AND, OR and NOT Operators

2 Combining AND, OR and NOT Combining AND, OR and NOT

3 The SQL ORDER BY The SQL ORDER BY

4 Scenarios

Scenario-1

Scenario-2

5 The SQL WHERE BETWEEN syntax The SQL WHERE BETWEEN syntax

6 Takeaway!

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 30 / 31
March 13, 2019
30 / 31

Takeaway!

Takeaway!

Strcuture definition Syntax of SQL DML Operations How to read a statement for a possible query

Dr. Ghulam Mustafa (BULC)

Week5

March 13, 2019 31 / 31
March 13, 2019
31 / 31