Javatpoint Logo
Javatpoint Logo

SQL Comparison Operators

The SQL Operators which compare the values of two columns in the database tables are called as comparison operators.

In SQL, comparison operators are always used in the WHERE clause with the SELECT, UPDATE, and DELETE statements.

The comparison operators in SQL are categorized into the following six operators category:

  1. SQL Equal Operator (=)
  2. SQL Not Equal Operator (!=)
  3. SQL Greater Than Equals to Operator (>=)
  4. SQL Less Than Operator (<)
  5. SQL Greater Than Operator (>)
  6. SQL Less Than Equals to Operator (<=)

Let's discuss each comparison operator one by one in detail with examples.

SQL Equal Operator (=)

This type of comparison operator selects only those data from the table which matches the specified value.

This operator is highly used by the database users in Structured Query Language.

This operator returns TRUE rows from the database table if the value of the column is same as the value specified in the query.

The following syntax accesses the data from the table by using the Equal operator:

The syntax to update the data in the table by using the Equal operator is given below:

The syntax to delete the data from the table by using the Equal operator is given below:

Example of SQL Equal operator

The following CREATE query creates the Employee table with five fields:

The following SELECT query shows the data of the Employee table:


Employee_Id Emp_Name Emp_City Emp_Salary Emp_Bonus
101 Anuj Ghaziabad 35000 2000
102 Tushar Lucknow 29000 3000
103 Vivek Kolkata 35000 2500
104 Shivam Goa 22000 3000

The following query shows the record of those employees from the Employee table whose Emp_Salary is 35000:

Output:

Employee_Id Emp_Name Emp_City Emp_Salary Emp_Bonus
101 Anuj Ghaziabad 35000 2000
103 Vivek Kolkata 35000 2500

The following query updates the Emp_Salary of those employees whose Emp_Bonus is 3000:

To check the result of the above UPDATE query, write the following statement:

Output:

Employee_Id Emp_Name Emp_City Emp_Salary Emp_Bonus
101 Anuj Ghaziabad 35000 2000
102 Tushar Lucknow 35000 3000
103 Vivek Kolkata 35000 2500
104 Shivam Goa 35000 3000

The following query deletes the record of those employees whose Emp_City is 'Goa':

SQL NOT Equal Operator (!=)

This type of comparison operator selects only those data from the table which does not match with the specified value.

This operator returns TRUE rows from the database table if the value of the column is not same as the value specified in the query.

The syntax to access the data from the table by using the NOT Equal operator is given below:

The syntax to update the data in the table by using the NOT Equal operator is given below:

The syntax to delete the data from the table by using the NOT Equal operator is given below:

Example of SQL NOT Equal operator

The following CREATE query creates the Cars table with four fields:

The following INSERT query inserts the record of cars into the Cars table:

The following SELECT query shows the data of the Cars table:


Car_Number Car_Name Car_Amount Car_Price
2578 Creta 3 900000
9258 Audi 2 1100000
8233 Venue 6 900000
6214 Nexon 7 1000000

The following query shows the record of those cars from the Cars table whose Car_Price is not equal to 900000:

Output:

Car_Number Car_Name Car_Amount Car_Price
9258 Audi 2 1100000
6214 Nexon 7 1000000

The following query updates the Car_Name of those cars whose Car_Number is not equal to 9258 or whose Car_Amount is not equal to 6:

To check the result of the above UPDATE query, write the following statement:

Output:

Car_Number Car_Name Car_Amount Car_Price
2578 Mercedes 3 900000
9258 Audi 2 1100000
8233 Venue 6 900000
6214 Mercedes 7 1000000

The following query deletes the record of those cars whose Car_Name is not equal to Audi.

SQL Greater Than Operator (>)

This type of comparison operator selects, modifies, and deletes only those data from the table which are greater than the value specified in the query.

The following syntax accesses the data from the table by using the Greater Than operator:

The syntax to update the data in the table by using the Greater Than operator is given below:

The syntax to delete the data from the table by using the Greater Than operator is given below:

Example of SQL Greater Than Operator

The following CREATE statement creates the Cars_Details table with four fields:

The following INSERT query inserts the record of cars into the Cars_Details table:

The following SELECT query shows the data of the Cars_Details table:


Car_Number Car_Name Car_Amount Car_Price
2578 Creta 3 900000
9258 Audi 2 1100000
8233 Venue 6 900000
6214 Nexon 7 1000000

The following query shows the record of those cars whose Car_Number is greater than 6000:

Output:

Car_Number Car_Name Car_Amount Car_Price
9258 Audi 2 1100000
8233 Venue 6 900000
6214 Nexon 7 100000

SQL Greater Than Equals to Operator (>=)

This type of comparison operator retrieves, modifies, and deletes only those data from the table which are greater than and equal to the given value.

The syntax to access the data from the table by using Greater Than Equals To operator is given below:

SELECT Column_Name1, Column_Name2, ….., Column_NameN FROM Table_Name WHERE Column_Name >= Value;

The syntax to update the data in the table by using Greater Than Equals To operator is given below:

UPDATE Table_Name SET Column_Name = Value WHERE Column_Name >= Value;

The syntax to delete the data from the table by using Greater Than Equals To operator is given below:

DELETE FROM Table_Name WHERE Column_Name >= Value;

Example of SQL Greater Than Equals To Operator

The following CREATE statement creates the Student_Details table with five fields:

The following INSERT query inserts the record of student into the Student_Details table:

The following SELECT query shows the data of the Student_Details table:


Student_Id Student_Name Student_Maths Student_English Student_Total_Marks
201 Anuj 30 60 90
202 Tushar 25 100 125
203 Vivek 30 90 120
204 Shivam 40 80 120

The following query shows the record of those students from the Student_Details table whose Total_Marks is greater than and equal to 120.

Output:

Student_Id Student_Name Student_Maths Student_English Student_Total_Marks
202 Tushar 25 100 125
203 Vivek 30 90 120
204 Shivam 40 80 120

SQL Less Than Operator (<)

This type of comparison operator in SQL selects only those data from the table which are less than the given value.

The following syntax accesses the data from the table by using the Less Than operator:

The syntax to update the data in the table by using the Less Than operator is given below:

The syntax to delete the data from the table by using the Less Than operator is given below:

Example of SQL Less Than operator

The following CREATE statement creates the Cars_Details table with four fields:

The following INSERT query inserts the record of cars into the Cars_Details table:

The following SELECT query shows the data of the Cars_Details table:


Car_Number Car_Name Car_Amount Car_Price
2578 Creta 3 900000
9258 Audi 2 1100000
8233 Venue 6 900000
6214 Nexon 7 1000000

The following query shows the record of those cars whose Car_Amount is less than 6:

Output:

Car_Number Car_Name Car_Amount Car_Price
2578 Creta 3 900000
9258 Audi 2 1100000

SQL Less Than Equals to Operator (<=)

This type of comparison operator selects only those data from the table which are less than and equal to the given value.

The syntax to access the data from the table by using the Less Than Equals To operator is given below:

The syntax to update the data in the table by using the Less Than Equals To operator is given below:

The syntax to delete the data from the table by using the Less Than Equals To operator is given below:

Example of SQL Less Than Equals To Operator

The following CREATE statement creates the Student_Details table with five fields:

The following INSERT query inserts the record of student into the Student_Details table:

The following SELECT query shows the data of the Student_Details table:


Student_Id Student_Name Student_Maths Student_English Student_Total_Marks
201 Anuj 30 60 90
202 Tushar 25 100 125
203 Vivek 30 90 120
204 Shivam 40 80 120

The following query shows the record of those students from the Student_Details table whose Student_Id is less than and equal to 202.

Output:

Student_Id Student_Maths Student_English Student_Total_Marks
201 30 60 90
202 25 100 125

Next TopicSQL COUNT WHERE





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA