0% found this document useful (0 votes)
9 views17 pages

Dbmslab 2

Uploaded by

aiml3b2023
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)
9 views17 pages

Dbmslab 2

Uploaded by

aiml3b2023
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/ 17

Comparison Operators or Relational Operators

Being conditional takes two expressions, makes a useful comparison, and returns either True or False.
It does the operations such as equal to, lesser than, greater than, or greater than similar to and other
advanced concepts. This Operator joins their hands with the ‘where’ clause to select the particular
columns in the records. Here in Below Section, we describe different types of relational operators and
a few examples of them along with the syntax.

Syntax:

SELECT column FROM table WHERE condition1 Relational Operator condition2;

Comparison operators are one of the most basic ways to put filters during writing queries to
extract the data, and these operators are used with the WHERE clause in SQL.
Here is the list of comparison operators in SQL
Operators Description

= Equal to

< > , != Not Equal to

> Greater than

>= Greater than or Equal

< Less than

<= Less than or Equal

Now we will take the Employee dataset (that contains Employee ID, Name, Gender,
Department, Education, Month of Joining, and CTC) to do some example to get more
understanding about these comparison operators.

Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)

1001 Ajay M Engineering Doctoral January 25


1002 Babloo M Engineering UG February 23

1003 Chhavi F HR PG March 15

1004 Dheeraj M HR UG January 12

1005 Evina F Marketing UG March 16

1006 Fredy M Sales UG December 10

1007 Garima F Sales PG March 10

1008 Hans M Admin PG November 8

1009 Ivanka F Admin Intermediate April 7

1010 Jai M Peon December 4

1. Equal to (=)
This Operator checks whether the value of the two operands is the same. If it is equal, it returns true; if

not, it returns false.

Example 1: Find the employee detail who joined in the month of January.
Query

SELECT *

FROM Employee
WHERE Month of Joining = ‘January’;

Output
Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)

1001 Ajay M Engineering Doctoral January 25

1004 Dheeraj M HR UG January 12


Example 2: Find the employee details where the CTC is greater than or equal to 16

Query

SELECT *

FROM Employee
WHERE CTC >= 16;

output
Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)
1001 Ajay M Engineering Doctoral January 25
1002 Babloo M Engineering UG February 23
1003 Chhavi F HR PG March 15
1005 Evina F Marketing UG March 16

Example 3: Find the employee details where the CTC is less than 8.
Query

SELECT *

FROM Employee
WHERE CTC < 8;
Output
Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)
1009 Ivanka F Admin Intermediate April 7
1010 Jai M Peon December 4

Example 4: Find the employee detail who have not joined in the month of January.
Query
SELECT *
FROM Employee
WHERE Month of Joining != ‘January’;

Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)

1002 Babloo M Engineering UG February 23

1003 Chhavi F HR PG March 15

1005 Evina F Marketing UG March 16

1006 Fredy M Sales UG December 10

1007 Garima F Sales PG March 10

1008 Hans M Admin PG November 8

1009 Ivanka F Admin Intermediate April 7

1010 Jai M Peon December 4

Output
Now, let’s discuss Arithmetic operators in SQL.

Arithmetic Operators
Arithmetic Operators
In the SQL query, these operators manipulate mathematical calibrations like addition, multiplication,
division, subtraction, and other modulus numeric values.

Syntax:

Select <expression 1> operator <expression 2>

Similar to Comparison Operator, SQL has five different Arithmetic Operators, let’s explore
them:
Operator Description

+ Add

– Subtract

* Multiply
Month North (in lacs) East (in lacs) West (in lacs) South (in lacs)
/ Divide
January 10.4 11.5 12.3 12.1
% Modulo
February 11.3 13.4 17.6 19.3

March 10.7 32.3 23.3 21.7

April 12.6 21.2 23.5 7.45

May 13.8 23.4 33.7 43.4

June 12.3 31.4 11.5 11.4

July 15.8 12.3 12.02 14.7

August 13.4 17.3 23.5 22.7

September 23.7 43.8 45.6 44.9

October 34.1 24.7 23.8 22.7

November 32.5 25.6 31.7 41.9

December 30.6 29.8 28.7 32.5

SQL> select 11+ 20;

Example 1: Find the sum of sales of the region North and South.
Query
SELECT Month, North, South, North + South
FROM Sales;
Month North South North + South
January 10.4 12.1 22.5
February 11.3 19.3 30.6
March 10.7 21.7 32.4
April 12.6 7.45 20.05
May 13.8 43.4 57.2
June 12.3 11.4 23.7
July 15.8 14.7 30.5
August 13.4 22.7 36.1
September 23.7 44.9 68.6
October 34.1 22.7 56.8
November 32.5 41.9 74.4
December 30.6 32.5 63.1

Example 2: Subtract 10 from the combined sales of North + South and then multiply 2
in the final result.
Query
SELECT Month, North, South,
(North + South -10) * 2 AS New Record
FROM Sales;

Month North South New Record


January 10.4 12.1 25
February 11.3 19.3 41.2
March 10.7 21.7 44.8
April 12.6 7.45 20.10
May 13.8 43.4 94.4
June 12.3 11.4 27.4
July 15.8 14.7 41
August 13.4 22.7 52.2
September 23.7 44.9 117.2
October 34.1 22.7 93.6
November 32.5 41.9 128.8
December 30.6 32.5 106.2

Example 3: Find the average sales of North and South region.


Query

SELECT Month, North, South


(North + South) / 2 AS Avg Sales
FROM Sales;

Output
Month North South Avg Sales
January 10.4 12.1 11.25
February 11.3 19.3 15.3
March 10.7 21.7 16.2
April 12.6 7.45 10.025
May 13.8 43.4 28.6
June 12.3 11.4 11.85
July 15.8 14.7 15.25
August 13.4 22.7 18.05
September 23.7 44.9 34.3
October 34.1 22.7 28.4
November 32.5 41.9 37.2
December 30.6 32.5 31.55

Logical Operators
Logical operators are symbols or words that are used to connect two or more expressions.
Some of the common logical operators are AND, OR, and NOT. SQL has more than these
logical operators.
Here, is the list of Logical Operators in SQL.
Operators Description

AND It will allow selecting only those rows that satisfy both the given conditions

OR It will allow selecting only those rows that satisfy either of the given two conditions

NOT Allows to select the rows that do not satisfy the specified conditions

LIKE Allows to match the similar value or the string


IN It will select a list of values that you want like to include

BETWEEN It will select the rows defined between a certain range. It has to be paired with AND.

ISNULL Allows excluding rows with the missing data from your result

Must Read: SQL SELECT


Must Read: SQL WHERE
Must Read: SQL LIMITS
Now we will use the Employee dataset (that contains Employee ID, Name, Gender,
Department, Education, Month of Joining, and CTC) to do some examples to get a clear
understanding of different logical operators.
Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)

1001 Ajay M Engineering Doctoral January 25

1002 Babloo M Engineering UG February 23

1003 Chhavi F HR PG March 15

1004 Dheeraj M HR UG January 12

1005 Evina F Marketing UG March 16

1006 Fredy M Sales UG December 10

1007 Garima F Sales PG March 10

1008 Hans M Admin PG November 8

1009 Ivanka F Admin Intermediate April 7

1010 Jai M Peon December 4

Must Read: Introduction to SQL


AND
Example 1: Find the details of all the employees who are working in Sales department
and are undergraduates.
Query
SELECT *
FROM Employee
WHERE department = ‘Sales’ AND Education = ‘UG’;

Output
Employee ID Name Gender Department Education Month of Joining C
1006 Fredy M Sales UG December 10

Must Read: What is the difference between SQL and MySQL?


OR
Example 2: Find the employee detail of all the employees who joined the month of march or
CTC is greater than 20 lacs.
Query
SELECT *
FROM Employee
WHERE Month of Joining = ‘March’ OR CTC > 20;

Output
Employee ID Name Gender Department Education Month of Joining C

1001 Ajay M Engineering Doctoral January 2

1002 Babloo M Engineering UG February 2

1003 Chhavi F HR PG March 1

1005 Evina F Marketing UG March 1

1007 Garima F Sales PG March 1

Must Read: How to Create, Insert, and Delete SQL views?


NOT
Example 3: Find the details of all the employee who are undergraduate but not working
in the HR department.
Query
SELECT *
FROM Employee
WHERE Education = ‘UG’ AND Department NOT ‘HR’;

Output
Employee ID Name Gender Department Education Month of Joining C

1002 Babloo M Engineering UG February 23

1005 Evina F Marketing UG March 16

1006 Fredy M Sales UG December 10

Must Read: Types of Keys in Database


Must Read: Introduction to Normalization
LIKE
LIKE Operator Description

LIKE ‘n%’ Find the values that start with n


LIKE ‘%g’ Find the values that end with g

LIKE ‘%learn%’ Find the value that has learn in any position

LIKE ‘_n%’ Find the letter that has n at the second position

LIKE ‘n_%’ Find the word that starts with n but has at least two characters in length

LIKE ‘n%g’ Find the word that starts with n and ends with g

Example 4: Find the detail of the employees who are working in the department that
ends with ‘ing’.
Query
SELECT *
FROM Employee
WHERE Department LIKE ‘%ing’;

Output
Employee ID Name Gender Department Education Month of Joining C

1001 Ajay M Engineering Doctoral January 25

1002 Babloo M Engineering UG February 23

1005 Evina F Marketing UG March 16

Example 5: Find the details of the employees who are working in HR and Engineering
department.
Query
SELECT *
FROM Employee
WHERE Department IN (‘Engineering’, ‘HR’);
Output
Employee ID Name Gender Department Education Month of Joining C

1001 Ajay M Engineering Doctoral January 2

1002 Babloo M Engineering UG February 2

1003 Chhavi F HR PG March 1

1004 Dheeraj M HR UG January 1

Must Read: Subqueries in SQL


Must Read: Aggregate and Scalar Function in SQL
BETWEEN
Example 6: Find the employee details whose CTC is in BETWEEN 8 and 15.
Query

SELECT *

FROM Employee

WHERE CTC BETWEEN 8 AND 15;

Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)

1003 Chhavi F HR PG March 15

1004 Dheeraj M HR UG January 12

1006 Fredy M Sales UG December 10

1007 Garima F Sales PG March 10

1008 Hans M Admin PG November 8

Must Read: Difference between SQL and NoSQL


Must Check: SQL Online Course and Certifications
ISNULL
Example 7: Find the detail of the employees where Education is NULL.

SELECT *

FROM Employee
WHERE Education IS NULL;

Output
Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)
1010 Jai M Peon December 4

SQL Functions: Aggregate and Scalar


Patients Table
Patient Patient Postal
Sex Age Address State Country Hemoglobin
ID Name Code

Flat no 201, Vasavi Heights,


01 Sheela F 23 500023 Telangana India 82.321
Yakutapura

02 Rehan M 21 Building no 2, Yelahanka 560063 Karnataka India 83.231

03 Anay M 56 H No 1, Panipat 132140 Haryana India 94.567

04 Mahira F 42 House no 12, Gandhinagar 382421 Gujarat India 78.567

05 Nishant M 12 Sunflower Heights, Thane 400080 Maharashtra India 65.234

What are SQL functions?


SQL functions are used to perform operations such as mathematical calculations, string
concatenation, etc. They are categorized into Aggregate Functions and Scalar Functions.
To know more about, Database and SQL Online Courses and Certifications, click here.
Let us start by understanding the aggregate functions.

Aggregate Functions
Aggregate functions are used to perform mathematical calculations on data. They usually
return a single value as output.

An aggregate function in SQL performs a calculation on multiple values and returns a single
value. SQL provides many aggregate functions that include avg, count, sum, min, max, etc.
An aggregate function ignores NULL values when it performs the calculation, except for the
count function.

What is an Aggregate Function in SQL?

An aggregate function in SQL returns one value after calculating multiple values of a
column. We often use aggregate functions with the GROUP BY and HAVING clauses of the
SELECT statement.
There are 5 types of SQL aggregate functions:

 Count()

 Sum()

 Avg()

 Min()

 Max()

The most common aggregate functions are:


COUNT()
Used to return the number of records in a table based on the mentioned conditions.
Syntax:
[code]
SELECT COUNT(Column_Name)FROM TABLE_NAME
WHERE Condition;
[/code]

Example:
[code]
SELECT COUNT(PatientID) FROM Patients;
[/code

SUM()
Used to return an arithmetic addition of numeric values of a column in a table.
Syntax:
[code]
SELECT SUM(Column_Name) FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT SUM(Age)FROM Patients;
[/code]

AVG()
Used to return an average of numeric values of a column in a table.
Syntax:
[code]
SELECT AVG(Column_Name) FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT AVG(Age)FROM Patients;
[/code]

MIN()
Used to return the least numeric values from a column in a table.
Syntax:
[code]
SELECT MIN(Column_Name) FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT MIN(Age)FROM Patients;
[/code]

MAX()
Used to return the greatest numeric values from a column in a table.
Syntax:
[code]
SELECT MAX(Column_Name) FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT MAX(Age)FROM Patients;
[/code]

FIRST()
Used to return the first value from a column in a table.
Syntax:
[code]
SELECT FIRST(Column_Name) FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT FIRST(PatientName)FROM Patients;
[/code]

LAST()
Used to return the last value from a column in a table.
Syntax:
[code]
SELECT LAST(Column_Name) FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT LAST(PatientName)FROM Patients;
[/code]

Now, we will understand the Scalar functions.

Scalar Functions

Scalar Functions in SQL


We can perform different operations on the data stored in the SQL database. This
can be done with the help of built-in functions provided by SQL. Scalar functions
are the built-in functions in SQL, and whatever be the input provided to the
scalar functions, the output returned by these functions will always be a single
value.

In SQL, each record is operated independently by the scalar functions.

Some of the commonly used scalar functions in SQL includes:

1. UCASE()
2. LCASE()
3. MID()
4. LENGTH()
5. ROUND()
6. NOW()
7. FORMAT()

Scalar functions are also used to return a single value after performing data operation.
The most common Scalar Functions are:
FORMAT()
Used to define a format of the fields.
Syntax:
[code]
SELECT FORMAT(INPUT, Format);
[/code]

Example:
[code]
SELECT FORMAT(ABCDEF, “##-##-##”);
[/code]

LCASE()
Used to convert values of a string column to lowercase.
Syntax:
[code]
SELECT LCASE(Column_Name) FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT LCASE(PatientName) FROM Patients;
[/code]

UCASE()
Used to convert values of a string column to uppercase.
Syntax:
[code]
SELECT UCASE(Column_Name) FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT UCASE(PatientName) FROM Patients;
[/code]

LEN()
Used to retrieve the length of the specified value or length of values in a column.
Syntax:
[code]
SELECT LEN(Column_Name) AS LenCol FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT UCASE(PatientName) AS PatientLen FROM Patients;
[/code]

MID()
Used to retrieve substring from the string type of values.
Syntax:
[code]
SELECT MID(Column_Name, START, LENGTH)
FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT MID(PatientName, 3, 4)
FROM Patients;
[/code]

ROUND()
Used to round off a numeric value.
Syntax:
[code]
SELECT ROUND(Column_Name, Decimals)
FROM TABLE_NAME;
[/code]

Example:
[code]
SELECT ROUND(Hemoglobin, 2)
FROM Patients;
[/code]

NOW()
Used to return the current date and time. By default, the current date and time are retrieved in
the format “YY-MM-DD HH:MM:SS”
Syntax:
[code]
SELECT NOW();
[/code]

Example:
[code]
SELECT NOW();
[/code]

You might also like