Dbmslab 2
Dbmslab 2
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:
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
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.
1. Equal to (=)
This Operator checks whether the value of the two operands is the same. If it is equal, it returns true; if
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)
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’;
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:
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
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;
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
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
Output
Employee ID Name Gender Department Education Month of Joining C
1006 Fredy M Sales UG December 10
Output
Employee ID Name Gender Department Education Month of Joining C
Output
Employee ID Name Gender Department Education Month of Joining C
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
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
SELECT *
FROM Employee
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
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.
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()
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]
Scalar Functions
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]