Ex.
No: 07
consider the following table to perform various operations. Arithmetic Operations,Comparison
Operations, Logical Operations & Set Operations
employee_id employee_name salary
1 alex 25000
2 rr 55000
3 jpm 52000
4 ggshmr 12312
Arithmetic Operators are:
+ [Addition]
- [Subtraction]
/ [Division]
* [Multiplication]
% [Modulus]
Addition (+) :
Implementation:
SELECT employee_id, employee_name, salary, salary + 100
AS "salary + 100" FROM addition;
addition of 2 columns:
SELECT employee_id, employee_name, salary, salary + employee_id
AS "salary + employee_id" FROM addition;
Subtraction (-) :
Implementation:
SELECT employee_id, employee_name, salary, salary - 100
AS "salary - 100" FROM subtraction;
subtraction of 2 columns:
SELECT employee_id, employee_name, salary, salary - employee_id
AS "salary - employee_id" FROM subtraction;
Multiplication (*) :
.
Implementation:
SELECT employee_id, employee_name, salary, salary * 100
AS "salary * 100" FROM addition;
multiplication of 2 columns:
SELECT employee_id, employee_name, salary, salary * employee_id
AS "salary * employee_id" FROM addition;
Modulus ( % ) :
Implementation:
SELECT employee_id, employee_name, salary, salary % 25000
AS "salary % 25000" FROM addition;
modulus operation between 2 columns:
SELECT employee_id, employee_name, salary, salary % employee_id
AS "salary % employee_id" FROM addition;
Concept of NULL :
Implementation:
SELECT employee_id, employee_name, salary, type, type + 100
AS "type+100" FROM addition;
Comparison Operators
StudentID FirstName LastName Age
1 Ajith Kumar 23
2 Gowtham Kumar 21
3 Rohan Jacklin 21
4 Akash Raj 20
5 Shree Hari 25
Comparison Operators
(i)equal to:
SELECT * FROM Students WHERE Age = 20;
(ii)Greater than
SELECT * FROM students WHERE Age > 23;
(iii) Not equal to
SELECT * FROM students WHERE Age <> 25;
(iV) Greater than or Equal To
SELECT * FROM students WHERE Age >= 21;
(v) Use less than or equal to
SELECT * FROM students WHERE Age <= 21;
Logical Operators
(i)AND
SELECT * FROM Student WHERE Age =21 AND LastName = 'Kumar';
(ii) ANY
SELECT * FROM Students WHERE Age > ANY (SELECT Age FROM Students WHERE
Age > 21);
(iii)BETWEEN & AND
SELECT * FROM Students WHERE Age BETWEEN 22 AND 25;
(iV) IN
SELECT * FROM Students WHERE Age IN('23', '20');
(V)OR Operator:
SELECT * FROM Student WHERE NAME = 'Gowtham' OR NAME = 'Ajith';
(Vi) Combining AND and OR:
SELECT * FROM Student WHERE Age = 23 AND (FirstName = 'Shree' OR LastName=
'Raj');
(Vii) Like
Q1. Select all students starting with “a”
SELECT * FROM students WHERE studentname LIKE 'a%';
Q2. Select all students with a studentname ending with “i”
1 Q2. Select all students with a studentname ending with “i”
SELECT * FROM students WHERE studentname LIKE '%i';
Q3. Select all students with a studentname that have “li” in any position
SELECT * FROM students WHERE studentname LIKE '%li%';
Q4. Select all students with a studentname that have “o” in the second Position
SELECT * FROM students WHERE studentname LIKE '_o%';
Q5. Select all students with a studentname that start with “a” and are at least 5 characters in
length
SELECT * FROM students WHERE studentname LIKE 'a____%'
Q6. Select all students with a studentname that start with “s” and end with “y”
SELECT * FROM students WHERE studentname LIKE 's%y';
SET Operators
Table 1: t_students
ID Name Hometown Percentage Favourite_Subject
1 Soniya Jain Udaipur 89 Physics
2 Harshada Sharma Kanpur 92 Chemistry
3 Anuja Rajput Jaipur 78 History
4 Pranali Singh Nashik 88 Geography
5 Renuka Deshmukh Panipat 90 Biology
6 Swati Kumari Faridabad 93 English
7 Prachi Jaiswal Gurugram 96 Hindi
Table 2: t2_students
ID Name Hometown Percentage Favourite_Subject
1 Soniya Jain Udaipur 89 Physics
2 Ishwari Dixit Delhi 86 Hindi
3 Anuja Rajput Jaipur 78 History
4 Pakhi Arora Surat 70 Sanskrit
5 Renuka Deshmukh Panipat 90 Biology
6 Jayshree Patel Pune 91 Maths
7 Prachi Jaiswal Gurugram 96 Hindi
1. UNION:
SELECT *FROM t_students UNION SELECT *FROM t2_students;
2. UNION ALL
SELECT *FROM t_students UNION ALL SELECT *FROM t2_students;
3. INTERSECT:
SELECT *FROM t_students INTERSECT SELECT *FROM t2_students;
4. MINUS
SELECT *FROM t_students MINUS SELECT *FROM t2_students;