SQL SELECT Statement WHERE | Restricting and Sorting of Data

SQL SELECT Statement WHERE | Restricting and Sorting of Data

This tutorial about SQL SELECT Statement WHERE Restricting and Sorting of Data is the continuation of our previous topics entitled Writing Basic SQL Select Statements With Actual Demonstration.

Objectives of SQL SELECT Statement WHERE

In this lesson, you will be able to the following:

  • Limit the rows retrieved by a query
  • Sort the rows retrieved by a query

Take note: In retrieving data from the database, there are times that you need to restrict data to display. Because Not all the time you are retrieving all records from the database table.

SQL SELECT Statement WHERE Basic Syntax

  1. WHERE

    it restricts the query to rows that meet the condition

  2. CONDITION

    It is composed of column names, expressions, constants, and comparison operators.
    here’s the link below for the OTHER COMPARISON OPERATORS.

The WHERE clause can compare values in columns, literal values, arithmetic expressions, or functions .

It consist of three Elements

  • Column Name
  • Comparison condition
  • Column name, constant, or list of values

Syntax:

SELECT *|{[DISTINCT] column/expression [alias],...}
FROM TABLE
[WHERE condition(s)];

Important Note: The WHERE clause follows the FROM clause.

For example we have here a sample table named tblsampleemployee:

EMP_IDEMPNAMECONTACTSALARYDEPT_NAME
1Kobe Bryant789541250000SHIPPING
2Jordan Michael789541270000IT
3James BOnd123245645000SALES
4Anne Curtis787889835000SHIPPING
5John Wick1233455765000EXECUTIVE
6Hatch Glets6654878257000IT
7Xyre Anderson7877892348000SALES

Using the SQL Statement WHERE Clause Equal to Operator

Now, if we want to display the employee id and employee name of all employee that belongs to IT Department. here’s the sample query below:

SELECT `EMP_ID`,`EMPNAME` 
FROM `tblsampleemployee` 
WHERE `DEPT_NAME` = 'IT'; 

This will give the following result:

EMP_IDEMPNAME
2Jordan Michael
6Hatch Glets

Take note: For strings , we always enclose them in single quotes.

Comparison Conditions

OperatorMeaning
=Equal to
>Greater than
>=Greater than or Equal to
<Less than
<=Less than or equal to
<>Not Equal to

The following example, we will perform the other comparison condition operators.

Using WHERE Clause Greater than Operator

Sample Query:

 SELECT `EMP_ID`,`EMPNAME`, SALARY FROM `tblsampleemployee` WHERE `SALARY` > 50000;

Output:

EMP_IDEMPNAMESALARY
2Jordan Michael70000
5John Wick65000
6Hatch Glets57000

In the SQL Select Statement above, we display the employee id number, employee name together with their salary that is greater than 50000.

Using WHERE Clause Greater than or equal to Operator

Sample Query:

 SELECT `EMP_ID`,`EMPNAME`, SALARY FROM `tblsampleemployee` WHERE `SALARY` >= 50000;

Output:

EMP_IDEMPNAMESALARY
1Kobe Bryant50000
2Jordan Michael70000
5John Wick65000
6Hatch Glets57000

In the SQL Select Statement above, we display the employee id number, employee name together with their salary that is greater than or equal to 50000.

Using WHERE Clause Less than to Operator

Sample Query:

 SELECT `EMP_ID`,`EMPNAME`, SALARY FROM `tblsampleemployee` WHERE `SALARY` < 50000;

Output:

EMP_IDEMPNAMESALARY
3James BOnd45000
4Anne Curtis35000
7Xyre Anderson48000

In the SQL Select Statement above, we display the employee id number, employee name together with their salary that is less than 50000.

Using WHERE Clause Less than or equal to Operator

Sample Query:

 SELECT `EMP_ID`,`EMPNAME`, SALARY FROM `tblsampleemployee` WHERE `SALARY` <= 50000;

Output:

EMP_IDEMPNAMESALARY
1Kobe Bryant50000
3James BOnd45000
4Anne Curtis35000
7Xyre Anderson48000

In the above SQL Query, we display the employee id number, employee name together with their salary that is less than or equal to 50000.

Using WHERE Clause Not equal to Operator

Sample Query:

 SELECT `EMP_ID`,`EMPNAME`, SALARY FROM `tblsampleemployee` WHERE `SALARY` <> 50000

Output:

EMP_IDEMPNAMESALARY
2Jordan Michael70000
3James BOnd45000
4Anne Curtis35000
5John Wick65000
6Hatch Glets57000
7Xyre Anderson48000

In the above sql statement, we display the employee id number, employee name together with their salary that is Not equal to 50000.

Video Tutorial of SQL Restricting and Sorting Data Using WHERE Clause

Watch the video tutorial here for complete practice with actual demonstration.

For Inquiries

If you have any questions about SQL SELECT Statement WHERE Restricting and Sorting of data, please leave a comment below.

1 thought on “SQL SELECT Statement WHERE | Restricting and Sorting of Data”

Leave a Comment