0% found this document useful (0 votes)
12 views11 pages

PCS 203-SM03

Uploaded by

arkkuniyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

PCS 203-SM03

Uploaded by

arkkuniyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Practical No.

3
Write SQL queries using SQL operators

The operators are symbols (and keywords) that are used to perform operations with values.

These operators are used with SQL clauses such as: SELECT, WHERE, ON etc.

The operators in SQL can be categorized as:

• Arithmetic operators

• Comparison operators

• Logical operators

SQL Arithmetic Operators

Arithmetic operators perform simple arithmetic operations such as addition,


subtraction, multiplication etc.

Operator Description

+ Addition

- Subtraction

* Multiplication

/ Divide

% Modulo (Remainder)
Addition in SQL

Substraction in SQL

Multiplication in SQL

Divide in SQL
Modulo (Remainder)

Arithmetic operator
The SQL Addition Operator performs the addition on the numerical columns in the table.

If you want to add the values of two numerical columns in the table, then you have to specify both
columns as the first and second operand. You can also add the new integer value in the value of the
integer column.

Syntax of SQL Addition Operator:

SELECT Column_Name_1 Addition_Operator Column_Name2 FROM Table_Name;

Example Table for use arithmetic operator:- 1)

The following query adds the emp_salary and emp_bonus of each employee of the Employee table
using the addition operator:
Example Table for use arithmetic operator:- 2)

The following query adds 1000 to the salary of each employee in the emp_salary column of the
Employee table:

Example Table for use arithmetic operator:- 3)

The following query performs the addition operation on the above Employee table with the WHERE
clause:
It shows only records of those employees whose emp_salary is greater than 20000:

Substraction operator
The SQL Subtraction Operator performs the subtraction on the numerical columns in the table.

If we want to subtract the values of one numerical column from the values of another numerical
column, then we have to specify both columns as the first and second operand. We can also subtract
the integer value from the values of the integer column.

The following query subtracts the values of the emp_bonus column from the emp_salary column of
the Employee table using the subtraction operator:

Syntax of SQL Subtraction Operator:

SELECT Column_Name_1 Subtraction_Operator Column_Name2 FROM Table_Name;

Example Table for use substraction operator:- 1)

Example Table for use substraction operator:- 2)


Multiplication operator
The SQL Multiplication Operator performs the multiplication on the numerical columns in the table.

If you want to multiply the values of two numerical columns, then you have to specify both columns
as the first and second operand. You can also multiply the integer value with the values of an integer
column.

Syntax of SQL Multiplication Operator:

SELECT Column_Name_1 Multiplication_Operator Column_Name2 FROM Table_Name;

Example Table for use Multiplication operator:- 1)

The following query multiplies the values of the Car_Amount column with the Car_Price column of
the Cars table using the Multiplication operator:
Divide operator
The SQL Division operator divides the numerical values of one column by the numerical values of
another column.

Syntax of SQL Division Operator:

SELECT Column_Name_1 Division_Operator Column_Name2 FROM Table_Name;

Example Table for use Divide operator:- 1)

The following query divides the values of the Car_Price column by the Car_Amount column of the
Cars table using the Multiplication operator:

Modulo operator
The SQL Modulus Operator provides the remainder when the numerical values of one column are
divided by the numerical values of another column.

Syntax of Modulus Operator in SQL:

SELECT Column_Name_1 Modulus_Operator Column_Name2 FROM Table_Name;

Example Table for use Modulo operator:- 1)

The following query divides the price car_price column by quantity of car_Amount of each entry in
the cars table:
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 (<=)

1) SQL Equal Operator (=)

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

2) SQL NOT Equal Operator (!=)

This type of comparison operator selects only those data from the table which does not match with
the specified value.
3) 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.

4) 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.
5) 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.

6) 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.

You might also like