Unit-9 - Computation On Table Data
Unit-9 - Computation On Table Data
Operator Description
| Bitwise OR
^ Bitwise exclusive OR
SQL Comparison Operators
Operator Description
= Equal to
Operator Description
+= Add equals
-= Subtract equals
*= Multiply equals
/= Divide equals
%= Modulo equals
Operator Description
ALL TRUE if all of the subquery values meet the condition
AND TRUE if all the conditions separated by AND is TRUE
ANY TRUE if any of the subquery values meet the condition
BETWEEN TRUE if the operand is within the range of comparisons
EXISTS TRUE if the subquery returns one or more records
IN TRUE if the operand is equal to one of a list of expressions
LIKE TRUE if the operand matches a pattern
NOT Displays a record if the condition(s) is NOT TRUE
OR TRUE if any of the conditions separated by OR is TRUE
SOME TRUE if any of the subquery values meet the condition
TYPES OF KEYWORDS/OPERATORS
General SELECT, ALL / DISTINCT, *,
Structure AS, FROM, WHERE
Union UNION
MVC
Employee table
emp_n emp_name emp_sal emp_comm dept _no
101 Smith 800 20
102 Snehal 1600 300 25
103 Adama 1100 0 20
104 Aman 3000 15
105 Anita 5000 50,000 10
106 Sneha 2450 24,500 10
107 Anamika 2975 30
Renaming columns used with Expression lists.
• Rename the default output column name with an alias, when
required.
• Syntax
Select <column1><Alias Name >,<column2><Alias name > from <table name >
CustomerName Address
Alfreds Futterkiste Obere Str. 57, Berlin, 12209, Germany
Ana Trujillo Emparedados y helados Avda. de la Constitución 2222, México D.F., 05021,
Mexico
Antonio Moreno Taquería Mataderos 2312, México D.F., 05023, Mexico
SELECT
Around the Horn CustomerName, Address+', '+City+', '+PostalCode+',
120 Hanover Sq., London, WA1 1DP, UK
'+Country AS Address
Berglunds snabbköp Berguvsvägen 8, Luleå, S-958 22, Sweden
FROM Customers;
Blauer See Delikatessen Forsterstr. 57, Mannheim, 68306, Germany
Note: To get the SQL statement above to work in MySQL use the following:
SELECT CustomerName, CONCAT(Address,', ',City,', ',PostalCode,', ',Country) AS Address
FROM Customers;
MVC
AS keyword/ for table
SELECT C.CustomerID, C.CustomerName, C.ContactName
FROM Customers C;
MVC
AS keyword/ for table
SELECT C.CustomerID, C.CustomerName, O.OrderID
FROM Customers C, Orders O
WHERE C.CustomerID = O.CustomerID;
MVC
• SQL Addition 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:
1. SELECT Column_Name_1 Addition_Operator Column_Name2 FROM Table_Name;
• The syntax for using the WHERE clause with the subtraction operator is given below:
1.SELECT Column_Name_1 Subtraction_Operator Column_Name2 FROM Table_Name
WHERE Condition;
•SQL 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.
UPDATE emp SET Location = "Delhi" WHERE Department = "Marketing" AND First_Na
me = "Suraj";
•
ID First_Name Last_Name Department Location
1 Raman 95 20
2 Kapil 92 19
3 Arun 85 17
4 Ram 92 18
5 Suman 55 20
6 Sanjay 88 18
7 Sheetal 65 19
8 Rakesh 64 20
1 Raman 95 20
2 Kapil 92 19
3 Arun 85 17
4 Ram 92 18
6 Sanjay 88 18
• NOT BETWEEN
5 Suman 55 20
7 Sheetal 65 19
8 Rakesh 64 20
Emp_ID Name Emp_Salary Emp_Joining
• Following are two wildcard characters that are used either in conjunction or
independently with the SQL LIKE operator:
1.% (percent sign): This wildcard character matches zero, one, or more than one character.
2._ (underscore sign): This wildcard character matches only one or a single character.
Raman 3000
Suraj 6000
Anaya 4000
• Table : Student
Roll_No Name Marks Age
1 Raman 95 20
2 Kapil 60 19
3 Arun 85 17
4 Ram 92 18
5 Suman 55 20
6 Sanjay 88 18
7 Sheetal 65 19
8 Rakesh 64 20
• Suppose, you want to show all records of those students whose Name contains "a" at the second position.
For this operation, you have to type the following query with underscore sign:
1. SELECT * FROM Student WHERE Name LIKE '_a%' ;
Roll_No Name Marks Age
• 1 Raman 95 20
2 Kapil 60 19
4 Ram 92 18
6 Sanjay 88 18
8 Rakesh 64 20
• ii) Suppose, you want to access records of those students whose names contain at least 3
characters and starts with the letter "S". For this operation, you have to type the following
query:
1.SELECT * FROM Student WHERE Name LIKE 'S___%' ;
Roll_No Name Marks Age
5 Suman 55 20
6 Sanjay 88 18
7 Sheetal 65 19
iii) Suppose, you want to access Roll_No, Name, and Marks of those students whose Marks is 2
digits long and ends with '5’:
SELECT * FROM Student WHERE Name LIKE '_5' ;
Roll_No Name Marks Age
1 Raman 95 20
3 Arun 85 17
5 Suman 55 20
7 Sheetal 65 19
Thank You