SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
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:
- SQL Equal Operator (=)
- SQL Not Equal Operator (!=)
- SQL Greater Than Equals to Operator (>=)
- SQL Less Than Operator (<)
- SQL Greater Than Operator (>)
- 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:
(
Employee_ID INT,
Emp_Name VARCHAR (50),
Emp_City VARCHAR (20),
Emp_Salary INT NOT NULL,
Emp_Bonus INT NOT NULL
) ;
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:
(
Car_Number INT PRIMARY KEY,
Car_Name VARCHAR (50),
Car_Price INT NOT NULL,
Car_AmountINT NOT NULL
) ;
The following INSERT query inserts the record of cars into the Cars table:
VALUES (2578, Creta, 3, 1500000),
(9258, Audi, 2, 3000000),
(8233, Venue, 6, 900000),
(6214, Nexon, 7, 1000000);
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:
(
Car_Number INT PRIMARY KEY,
Car_Name VARCHAR (50),
Car_Price INT NOT NULL,
Car_AmountINT NOT NULL
) ;
The following INSERT query inserts the record of cars into the Cars_Details table:
VALUES (2578, Creta, 3, 1500000),
(9258, Audi, 2, 3000000),
(8233, Venue, 6, 900000),
(6214, Nexon, 7, 1000000);
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:
The syntax to update the data in the table by using Greater Than Equals To operator is given below:
The syntax to delete the data from the table by using Greater Than Equals To operator is given below:
Example of SQL Greater Than Equals To Operator
The following CREATE statement creates the Student_Details table with five fields:
(
Student_ID INT PRIMARY KEY,
Student_Name VARCHAR (50),
Student_MathsINT NOT NULL,
Student_English INT NOT NULL,
Student_Total_Marks INT NOT NULL
) ;
The following INSERT query inserts the record of student into the Student_Details table:
(202, Tushar, 25, 100, 125),
(203, Vivek, 30, 90, 120),
(204, Shivam, 40, 80, 120);
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:
(
Car_Number INT PRIMARY KEY,
Car_Name VARCHAR (50),
Car_Price INT NOT NULL,
Car_AmountINT NOT NULL
) ;
The following INSERT query inserts the record of cars into the Cars_Details table:
VALUES (2578, Creta, 3, 1500000),
(9258, Audi, 2, 3000000),
(8233, Venue, 6, 900000),
(6214, Nexon, 7, 1000000);
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:
(
Student_Id INT NOT NULL,
Student_Name VARCHAR (50),
Student_MathsINT NOT NULL,
Student_English INT NOT NULL,
Student_Total_Marks INT NOT NULL
) ;
The following INSERT query inserts the record of student into the Student_Details table:
(202, Tushar, 25, 100, 125),
(203, Vivek, 30, 90, 120),
(204, Shivam, 40, 80, 120);
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 |