0% found this document useful (0 votes)
7 views

SQL Operators

SQL

Uploaded by

Srinibash Patra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL Operators

SQL

Uploaded by

Srinibash Patra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Operators in MySql

The following types of operators are supported by MySql –


1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical calculations. Various
arithmetic operators in MySql are –
+ (Addition): Adds two or more arguments.

- (Subtraction): Subtracts the 2nd argument from the 1st argument.

* (Multiplication): Multiplies two or more arguments.

/ (Division): Divides 1st argument by the 2nd.

% or MOD (Remainder Division): Divides 1st argument by the 2nd and remainder is the result.

DIV (Integer Division): Divides 1st argument by the 2nd and quotient is the result (only integer
value, no decimal value)
Example:
Select 13+7-3; Select 10*30+5; Select 10*2+25-3+8;
Select 19/2; Select 19%2; Select 19 DIV 2;
We can apply these operators on database tables. Let’s consider the table STUDENT_MARK.
Q: Add 5 marks to each students’ mark and display
select TOTAL_MARKS+5 from STUDENT_MARK;
Q: Display Name, Percentage of marks of all students (assume full marks=750)
select SNAME, (TOTAL_MARKS/750)*100 PER_MARKS from STUDENT_MARK;
2. Relational Operators
Relational operators are also called as comparison operators. These are used to compare
two or more values. The comparison result is a boolean value (1 or 0) and based on the
boolean value actual result is produced.
Relational operators supported by MySql are –
= (Equal to): Compares left value with right value. If both are equal result is 1 otherwise 0.
<> or != (Not equal to): Compares left value with right value. If both are not equal result is 1
otherwise 0.
> (Greater than): Compares the left value with the right value. If left value is greater than right
value result is 1 otherwise 0.
>= (Greater than or equal): Compares the left value with the right value. If left value is greater
than or equals to right value result is 1 otherwise 0.
< (Less than): Compares the left value with the right value. If left value is smaller than right
value result is 1 otherwise 0.
<= (Less than or equal): Compares the left value with the right value. If left value is smaller
than or equals to right value result is 1 otherwise 0.

10
Select 88888=8888; Select 676767! =767676; or Select 676767 <>767676;
Select 5689>5893; Select 2356>=2458; Select 7853<6578; Select 4587<=2569;
Let’s apply these operators on the table STUDENT_MARK
Q: Retrieve the information of students who have secured B grade.
Select * from STUDENT_MARK where GRADE='B';
Q: Retrieve the Name, Branch, Mark of the students who have scored 700 or above.
Select SNAME, BRANCH, TOTAL_MARKS from STUDENT_MARK where
TOTAL_MARKS>=700;
Q: Retrieve the Name, Branch, Grade of the students who have not secured B grade.
Select * from STUDENT_MARK where GRADE!='B';
3. Logical Operators
Logical operators are used to compare two or more conditional expressions. The comparison
result is a boolean value (1 or 0) and based on the boolean value actual result is produced.
Logical operators supported by MySql are –
AND: It compares two or more conditional expressions and returns true (1) only when all
expressions evaluate to true otherwise it returns false (0).
OR: It compares two or more conditional expressions and returns true (1) when any one of
the expressions evaluate to true otherwise it returns false (0).
NOT: It operates on a single expression and inverses its value i.e., if the expression evaluates
to true (1) it converts to false (0) and vice-versa.
Select (45>50) AND (25<125); Select (45>50) OR (25<125); Select NOT ((45>50);
Select (45>50) OR (25<125) AND (12>6); Select NOT ((45>50) OR (25<125) AND (12>6)); etc.
Let’s apply these operators on the table STUDENT_MARK
Q: Retrieve the details of students who have secured A+ grade and having Electrical branch.
Select * from STUDENT_MARK where BRANCH='Electrical' AND GRADE='A+';
Q: Retrieve the details of students who have secured A grade and having branch either
Electrical or Mechanical.
Select * from STUDENT_MARK where (BRANCH='Electrical' OR
BRANCH='Mechanical') AND GRADE='A';
Q: Retrieve the Name, BRANCH, Mark and Grade of those students who have secured A+
grade but not belong to Computer Science branch.
Select SNAME, BRANCH, TOTAL_MARKS, GRADE from student_mark where
GRADE='A+' AND NOT (BRANCH='Computer Science');
4. Range Selection Operators
Range selection operators are used to retrieve information from a specified range of values.
Various range selection operators in MySql are –
BETWEEN: This operator matches the required value/information in range (a lower limit and
an upper limit) and retrieves the value along with other values fall in the range (lower limit
and upper limit values may be included).

11
IN: This operator checks the required value/information within a given list of values and
retrieves that value if it finds in the list.
Let’s apply these operators on the table STUDENT_MARK
Q: Retrieve the details of students whose marks are between 700 and 750.
select * from STUDENT_MARK where TOTAL_MARKS between 700 and 750;
Q: Retrieve the details of students whose marks in 550, 590, 620, 650, 680,700.
select * from STUDENT_MARK where TOTAL_MARKS in (550, 590,620,650,680,700);
Q: Retrieve the Name, Branch and Grade of students whose Branch in IT, Computer Science,
Electronics, Data Science;
select SNAME, BRANCH, GRADE from STUDENT_MARK where BRANCH in

('IT’, Computer Science', 'IT', 'Electronics', ‘Data Science’);

5. Pattern-matching Operator
This operator is used to retrieve data/information by matching a given pattern. The following
pattern matching operator is used in MySql.
LIKE: It retrieves the required data/information when it matches with the given pattern. This
operator is useful when we do not know the exact data to retrieve. LIKE operators uses the
following wildcard characters to retrieve information.
% (percentage): used with LIKE to match any sequence of characters.
_ (underscore): used with LIKE to match a single character.
✓ Both % and _ can be used simultaneously.
✓ LIKE is applied on the columns having data types char or varchar.
Let’s apply these operators on the table STUDENT_MARK
Q: Retrieve the details of students whose name starts with A.
select * from STUDENT_MARK where SNAME like 'A%';
Q: Retrieve the information of students whose name contains S at any position.
select * from STUDENT_MARK where SNAME like '%S%';
Q: Retrieve the information of students whose name’s 2nd character is i.
select * from STUDENT_MARK where SNAME like '_i%';
Q: Retrieve the information of students whose name’s 3rd character is a.
select * from STUDENT_MARK where SNAME like '__a%';
Soring of Records (use of ORDER BY)
When we execute SELECT command to display the contents of a table the records appear in the
same order as those were inserted. However, we can display records in ascending or
descending order of a particular column value. For this ORDER BY option can be used with the
SELECT command.
For ascending we have to use the option ASC and for descending the option DESC is to be used.
The default option is ascending.

12
Syntax: SELECT * FROM table_name ORDER BY col_name ASC/DESC;
Example: select * from STUDENT_MARK order by TOTAL_MARKS DESC;
select ROLL_NO, SNAME, BRANCH from STUDENT_MARK order by BRANCH ASC;
Column Alias
It is a process used to temporary renaming a table’s column while executing a query. There is
no effect on original column of table in the database.
Syntax: SELECT col-name, col-name… AS alias-name FROM table_name;
Example: select ROLL_NO, SNAME as STUDENT_NAME, GRADE from STUDENT_MARK;

Here, SNAME is the original name of column and STUDENT_NAME is the alias name.

13

You might also like