Sunteți pe pagina 1din 31

Introduction to SQL(Cont,)

CSC 220

Dr. Ghulam Mustafa

Department of Computer Sciences


Bahria University Lahore Campus

March 13, 2019


Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 1 / 31
The SQL AND, OR and NOT Operators The SQL AND, OR and NOT Operators

Outline

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


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


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

SQL AND,

AND Syntax

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 4 / 31


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

SQL OR,

OR Syntax

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 5 / 31


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

SQL NOT,

NOT Syntax

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 6 / 31


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

Demo Database

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 7 / 31


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

Scenario

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


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

Outline

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


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


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

Example

Dr. Ghulam Mustafa (BULC) Week5 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


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

Example

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 13 / 31


The SQL ORDER BY The SQL ORDER BY

Outline

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


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


The SQL ORDER BY The SQL ORDER BY

ORDER BY Syntax

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 16 / 31


The SQL ORDER BY The SQL ORDER BY

Example

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


The SQL ORDER BY The SQL ORDER BY

Example

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 18 / 31


The SQL ORDER BY The SQL ORDER BY

Scenario

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


The SQL ORDER BY The SQL ORDER BY

ORDER BY Several Columns

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


Scenarios Scenario-1

Outline

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


Scenarios Scenario-1

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

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 22 / 31


Scenarios Scenario-2

Outline

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


Scenarios Scenario-2

Scenario-2

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

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 24 / 31


Scenarios Scenario-2

Scenario-2 Soloution

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

Dr. Ghulam Mustafa (BULC) Week5 March 13, 2019 25 / 31


The SQL WHERE BETWEEN syntax The SQL WHERE BETWEEN syntax

Outline

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


The SQL WHERE BETWEEN syntax The SQL WHERE BETWEEN syntax

The SQL WHERE BETWEEN syntax

The general syntax is:

Dr. Ghulam Mustafa (BULC) Week5 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

Dr. Ghulam Mustafa (BULC) Week5 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

Dr. Ghulam Mustafa (BULC) Week5 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


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

S-ar putea să vă placă și