How to use WHERE and ORDER BY Function? SQL Part 4

Where:

Where statement helps us to use the comparison operators like equal to, greater than, less than etc and logical operators like AND, OR, NOT.

Example of Comparison Operator:

Table 1
C1C2C3
A3Pen
B2Pencil
C4Pencil
D5Pen
E6Pen

If we want rows which have values more than 2 in C2

SELECT * FROM Table 1

WHERE C2>2

Result: 

Table 1
C1C2C3
A3Pen
C4Pencil
D5Pen
E6Pen

Example of operational and comparison operator:

Now if we want rows which have values more than 2 in C2 and have Pen in C3

SELECT * FROM Table 1

WHERE C2>2 AND C3 = ‘Pen’

Result:

Table 1
C1C2C3
A3Pen
D5Pen
E6Pen

Try this yourself on SQL dvdrental database:

Challenge 1:

A customer forgot their wallet at our store! We need to track down their email to inform them. What is the email for the customer with the name Nancy Thomas?

Challenge 2:

A customer wants to know what the movie “Outlaw Hanky” is about. Could you give them the description for the movie “Outlaw Hanky”?

Order by:

Order by Function helps us in getting results in our desired output of ascending and descending.

Order by is generally at the end of a query. It is one of the last things SQL does in a query.

ASC – Ascending 

DESC – Desending

Blank – Ascending by default

Example:

Table 1
C1C2C3
A3Pen
B2Pencil
C4Pencil
D5Pen
E6Pen

If we want rows which have values more than 2 in C2

SELECT * FROM Table 1

ORDER BY C2 ASC

Result:

Table 1
C1C2C3
B2Pencil
A3Pen
C4Pencil
D5Pen
E6Pen

Now, if we want to arrange first table by all Pencil and than by C2 ascending order:

SELECT * FROM Table 1

ORDER BY C3 DESC, C2 ASC

Result:

Table 1
C1C2C3
B2Pencil
C4Pencil
A3Pen
D5Pen
E6Pen

Credits: The concepts and materials are taken from the Udemy course:The complete SQL Bootcamp 2022: Go from Zero to Hero

Check out the other parts in the SQL series:

Next:

Why do we need Structured Query Language (SQL)? SQL PART 5

Previous:

Why do we need Structured Query Language (SQL)? SQL PART 3

 Why do we need Structured Query Language (SQL)? SQL PART 2

 Why do we need Structured Query Language (SQL)? SQL PART 1

3 thoughts on “How to use WHERE and ORDER BY Function? SQL Part 4”
  1. Thanks I have recently been looking for info about this subject for a while and yours is the greatest I have discovered so far However what in regards to the bottom line Are you certain in regards to the supply

  2. Oh my goodness! a tremendous article dude. Thank you However I am experiencing issue with ur rss . Don抰 know why Unable to subscribe to it. Is there anyone getting equivalent rss downside? Anybody who knows kindly respond. Thnkx

Leave a Reply

Your email address will not be published. Required fields are marked *