Operator
Operator
Let's understand the below example which explains how to execute Addition
Operator in SQL query:
This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_Monthlybonus.
o Suppose, we want to add 20,000 to the salary of each employee specified in the table.
Then, we have to write the following query in the SQL:
In this query, we have performed the SQL addition operation on the single column of the given
table.
o Suppose, we want to add the Salary and monthly bonus columns of the above table,
then we have to write the following query in SQL:
In this query, we have added two columns with each other of the above table.
Let's understand the below example which explains how to execute Subtraction
Operator in SQL query:
This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_Monthlybonus.
o Suppose we want to subtract 5,000 from the salary of each employee given in
the Employee_details table. Then, we have to write the following query in the SQL:
In this query, we have performed the SQL subtraction operation on the single column of the
given table.
o If we want to subtract the penalty from the salary of each employee, then we have to
write the following query in SQL:
1. SELECT Emp_Salary - Penalty as Emp_Total_Salary FROM Employee_details;
Let's understand the below example which explains how to execute Multiplication
Operator in SQL query:
This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_Monthlybonus.
In this query, we have performed the SQL multiplication operation on the single column
of the given table.
1.
In SQL, we can also divide the numerical values of one column by another column of
the same table by specifying both column names as the first and second operand.
We can also perform the division operation on the stored numbers in the column of the
SQL table.
Let's understand the below example which explains how to execute Division
Operator in SQL query:
o Suppose, we want to half the salary of each employee given in the Employee_details
table. For this operation, we have to write the following query in the SQL:
In this query, we have performed the SQL division operation on the single column of the
given table.
Let's understand the below example which explains how to execute Modulus
Operator in SQL query:
This example consists of a Division table, which has three columns Number,
First_operand, and Second_operand.
1 56 4
2 32 8
3 89 9
4 18 10
5 10 5
o If we want to get the remainder by dividing the numbers of First_operand column by the
numbers of Second_operand column, then we have to write the following query in SQL:
Following are the various comparison operators which are performed on the data
stored in the SQL database tables:
This operator returns TRUE records from the database table if the value of both
operands specified in the query is matched.
Let's understand the below example which explains how to execute Equal
Operator in SQL query:
This operator returns those records or rows from the database views and tables if the
value of both operands specified in the query is not matched with each other.
Let's understand the below example which explains how to execute Equal Not
Operator in SQL query:
In this example, we used the SQL equal not operator with WHERE clause for getting the
records of those employees whose salary is not 45000.
Let's understand the below example which explains how to execute Greater
ThanOperator (>) in SQL query:
Here, SQL greater than operator displays the records of those employees from the
above table whose Employee Id is greater than 202.
Let's understand the below example which explains how to execute greater than
equals to the operator (>=) in SQL query:
Here,'SQL greater than equals to operator' with WHERE clause displays the rows of
those employees from the table whose Employee Id is greater than and equals to 202.
This comparison operator checks that the left side operand is lesser than the right side
operand. If the condition becomes true, then this operator in SQL displays the data
which is less than the value of the right-side operand.
Let's understand the below example which explains how to execute less than
operator (<) in SQL query:
Here,SQL less than operator with WHERE clause displays the records of those
employees from the above table whose Employee Id is less than 204.
This comparison operator checks that the left side operand is lesser and equal to the
right side operand.
Let's understand the below example which explains how to execute less than
equals to the operator (<=) in SQL query:
Here, SQL less than equals to the operator with WHERE clause displays the rows of
those employees from the table whose Employee Id is less than and equals 202.
You can also use some aggregate functions like COUNT, SUM, MIN, MAX, AVG etc. on
the grouped column.
Syntax:
Play Video
Output:
(ii) MySQL GROUP BY Clause with SUM function
Let's take a table "employees" table, having the following data.
Now, the following query will GROUP BY the example using the SUM function and
return the emp_name and total working hours of each employee.
Output:
Output:
(iv) MySQL GROUP BY Clause with MAX function
The following example specifies the maximum working hours of the employees form the
table "employees".
Output:
(v) MySQL GROUP BY Clause with AVG function
The following example specifies the average working hours of the employees form the table
"employees".
Output:
MySQL Boolean
A Boolean is the simplest data type that always returns two possible values, either true
or false. It can always use to get a confirmation in the form of YES or No value.
MySQL does not contain built-in Boolean or Bool data type. They provide
a TINYINT data type instead of Boolean or Bool data types. MySQL considered value
zero as false and non-zero value as true. If you want to use Boolean literals, use true or
false that always evaluates to 0 and 1 value. The 0 and 1 represent the integer values.
Execute the following statement to see the integer values of Boolean literals:
4.3M
118
In the above query, we can see that the pass field is defined as a Boolean when
showing the definition of a table; it contains TINIINT as follows:
Let us add two new rows in the above table with the help of following query:
When the above query executed, immediately MySQL checks for the Boolean data type
in the table. If the Boolean literals found, it will be converted into integer values 0 and 1.
Execute the following query to get the data from the student table:
You will get the following output where the true and false literal gets converted into 0
and 1 value.
Since MySQL always use TINYINT as Boolean, we can also insert any integer values
into the Boolean column. Execute the following statement:
In some cases, you need to get the result in true and false literals. In that case, you
need to execute the if() function with the select statement as follows:
1. Mysql> SELECT studentid, name, IF(pass, 'true', 'false') completed FROM student1;
The above statement only returns the pass result if the value is equal to 1. We can fix it
by using the IS operator. This operator validates the value with the Boolean value. The
following statement explains this:
After executing this statement, you will get the following result:
If you want to see the pending result, use IS FALSE or IS NOT TRUE operator as
below:
The SQL plus (+) operator is used to add two or more expressions or numbers.
Example:
FROM customer
Example:
FROM customer
WHERE(outstanding_amt-payment_amt)=receive_amt;
Output:
CUST_NAME OPENING_AMT PAYMENT_AMT
OUTSTANDING_AMT
---------------------------------------- ----------- -----------
---------------
Stuart 6000 3000
11000
Example:
working_area, (commission*2)
FROM agents
WHERE (commission*2)>0.25;
Copy
Output:
AGENT_ AGENT_NAME WORKING_AREA
A003 Alex London
.26
A001 Subbarao Bangalore
.28
A007 Ramasundar Bangalore
.3
A011 Ravi Kumar Bangalore
.3
A010 Santakumar Chennai
.28
A005 Anderson Brisban
.26
A006 McDen London
.3
A004 Ivan Torento
Example:
FROM customer
WHERE outstanding_amt<=4000;
Copy
Output:
CUST_NAME OPENING_AMT RECEIVE_AMT
OUTSTANDING_AMT
Holmes 6000 5000
4000 250
Bolt 5000 7000
3000 350
Karl 4000 6000
3000 300
Steven 5000 7000
3000 350
The SQL MODULO operator returns the remainder (an integer) of the division.
Example:
To get the modulus of a division of 150 by 7 from the DUAL table, the following
SQL statement can be used :
SELECT 150%7;
Copy
Output: