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
- WHERE
it restricts the query to rows that meet the condition
- 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_ID | EMPNAME | CONTACT | SALARY | DEPT_NAME |
1 | Kobe Bryant | 7895412 | 50000 | SHIPPING |
2 | Jordan Michael | 7895412 | 70000 | IT |
3 | James BOnd | 1232456 | 45000 | SALES |
4 | Anne Curtis | 7878898 | 35000 | SHIPPING |
5 | John Wick | 12334557 | 65000 | EXECUTIVE |
6 | Hatch Glets | 66548782 | 57000 | IT |
7 | Xyre Anderson | 78778923 | 48000 | SALES |
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_ID | EMPNAME |
2 | Jordan Michael |
6 | Hatch Glets |
Take note: For strings , we always enclose them in single quotes.
Comparison Conditions
Operator | Meaning |
= | 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_ID | EMPNAME | SALARY |
2 | Jordan Michael | 70000 |
5 | John Wick | 65000 |
6 | Hatch Glets | 57000 |
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_ID | EMPNAME | SALARY |
1 | Kobe Bryant | 50000 |
2 | Jordan Michael | 70000 |
5 | John Wick | 65000 |
6 | Hatch Glets | 57000 |
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_ID | EMPNAME | SALARY |
3 | James BOnd | 45000 |
4 | Anne Curtis | 35000 |
7 | Xyre Anderson | 48000 |
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_ID | EMPNAME | SALARY |
1 | Kobe Bryant | 50000 |
3 | James BOnd | 45000 |
4 | Anne Curtis | 35000 |
7 | Xyre Anderson | 48000 |
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_ID | EMPNAME | SALARY |
2 | Jordan Michael | 70000 |
3 | James BOnd | 45000 |
4 | Anne Curtis | 35000 |
5 | John Wick | 65000 |
6 | Hatch Glets | 57000 |
7 | Xyre Anderson | 48000 |
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.
I geta a little bit but I watch again and again the video