0% found this document useful (0 votes)
14 views

Unit-9 - Computation On Table Data

Uploaded by

zeelsoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Unit-9 - Computation On Table Data

Uploaded by

zeelsoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

SQL PART-III

Computation Done on Table Data


SQL Arithmetic Operators

Operator Description Example


+ Add
- Subtract
* Multiply
/ Divide
% Modulo
SQL Bitwise Operators

Operator Description

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR
SQL Comparison Operators

Operator Description

= Equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

<> Not equal to


SQL Compound Operators

Operator Description

+= Add equals

-= Subtract equals

*= Multiply equals

/= Divide equals

%= Modulo equals

&= Bitwise AND equals

^-= Bitwise exclusive equals

|*= Bitwise OR equals


SQL Logical Operators

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

Comparison IN, BETWEEN, LIKE "% _"

Grouping GROUP BY, HAVING,


COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )

Display Order ORDER BY, ASC / DESC

Logical AND, OR, NOT


Operators

Output INTO TABLE / CURSOR


TO FILE [ADDITIVE], TO PRINTER, TO SCREEN

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 >

Example : select emp_name Employeename from Employee;


SQL SELECT AS
• SQL 'AS' is used to assign a new name temporarily to a table column or
even a table.
• It makes an easy presentation of query results and allows the developer to
label results more accurately without permanently renaming table columns
or even the table itself.
• Let's see the syntax of select as:
1.SELECT Column_Name1 AS New_Column_Name, Column_Name2 As New
_Column_Name FROM Table_Name;
• Here, the Column_Name is the name of a column in the original table, and
the New_Column_Name is the name assigned to a particular column only
for that specific query. This means that New_Column_Name is a temporary
name that will be assigned to a query.
• Assigning a temporary name to the column of a table:
• Example : Table – Order
Day_of_order Customer Product Quantity

11-09-2001 Ajeet Mobile 2


13-12-2001 Mayank Laptop 20
26-12-2004 Balaswamy Water cannon 35

• SELECT day_of_order AS 'Date', Customer As 'Client', Product, Quantity FROM orders;

Date Client Product Quantity

11-09-2001 Ajeet Mobile 2


13-12-2001 Mayank Laptop 20
26-12-2004 Balaswamy Water cannon 35

• Note: SQL AS is the same as SQL ALIAS.


• Customer Table

Customer CustomerName ContactName Address City PostalCode Country


ID
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedados y Constitución
helados 2222

3 Antonio Moreno Antonio Mataderos México D.F. 05023 Mexico


Taquería Moreno 2312
4 Around the Horn Thomas Hardy 120 Hanover London WA1 1DP UK
Sq.

5 Berglunds Christina Berguvsvägen Luleå S-958 22 Sweden


snabbköp Berglund 8
AS keyword/ for column
SELECT CustomerName AS Customer, ContactName AS Contact Person
FROM Customers;
Customer Contact Person
Alfreds Futterkiste Maria Anders
Ana Trujillo Emparedados y helados Ana Trujillo
Antonio Moreno Taquería Antonio Moreno
Around the Horn Thomas Hardy
Berglunds snabbköp Christina Berglund
Blauer See Delikatessen Hanna Moos
Blondel père et fils Frédérique Citeaux
Bólido Comidas preparadas Martín Sommer
Bon app' Laurence Lebihans
Bottom-Dollar Marketse Elizabeth Lincoln
B's Beverages Victoria Ashworth
Cactus Comidas para llevar Patricio Simpson
Centro comercial Moctezuma Francisco Chang
MVC
AS keyword/ for column
SELECT CustomerName, Address || ‘,’||City||', ‘||PostalCode||', ‘||Country AS Address
FROM Customers;

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;

CustomerID CustomerName ContactName


1 Alfreds Futterkiste Maria Anders
2 Ana Trujillo Emparedados y helados Ana Trujillo

3 Antonio Moreno Taquería Antonio Moreno


4 Around the Horn Thomas Hardy
5 Berglunds snabbköp Christina Berglund
6 Blauer See Delikatessen Hanna Moos
7 Blondel père et fils Frédérique Citeaux
8 Bólido Comidas preparadas Martín Sommer
9 Bon app' Laurence Lebihans
MVC
Order table(fk-CustomerID)
OrderID CustomerID EmployeeID OrderDate ShipperID
10248 90 5 7/4/1996 3
10249 81 6 7/5/1996 1
10250 34 4 7/8/1996 2
10251 84 3 7/9/1996 1
10252 76 4 7/10/1996 2

MVC
AS keyword/ for table
SELECT C.CustomerID, C.CustomerName, O.OrderID
FROM Customers C, Orders O
WHERE C.CustomerID = O.CustomerID;

CustomerID CustomerName OrderID


90 Wilman Kala 10248
81 Tradição Hipermercados 10249
34 Hanari Carnes 10250
84 Victuailles en stock 10251
76 Suprêmes délices 10252

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;

• Addition Operator with WHERE Clause


• The addition operator can also be used with the WHERE clause in the SQL SELECT
query.
• The syntax for using the WHERE clause with the addition operator is given below:

1. SELECT Column_Name_1 Addition_Operator Column_Name2 FROM Table_Name WHERE Condition;


• SQL Subtraction 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.

• Syntax of SQL Subtraction Operator:


1.SELECT Column_Name_1 Subtraction_Operator Column_Name2 FROM Table_Name;
• Subtraction Operator with WHERE Clause
• The subtraction operator can also be used with the WHERE clause in the SELECT query.

• 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.

• Syntax of SQL Multiplication Operator:


1.SELECT Column_Name_1 Multiplication_Operator Column_Name2 FROM Table_Na
me;

• Multiplication Operator with WHERE Clause


• The multiplication operator (*) can also be used with the WHERE clause in the
SELECT query.
• The syntax for using the WHERE clause with the multiplication operator is given
below:
1.SELECT Column_Name_1 Multilplication_Operator Column_Name2 FROM Table_Na
me WHERE Condition;
• SQL Division Operator (/)
• The SQL Division operator divides the numerical values of one column
by the numerical values of another column.
• Syntax of SQL Division Operator:
1.SELECT Column_Name_1 Division_Operator Column_Name2 FROM T
able_Name;
• Division Operator with WHERE Clause
• The SQL division operator can also be used with the WHERE clause in
the SELECT query.
• The syntax for using the WHERE clause with the division operator is
given below:
1.SELECT Column_Name_1 Division_Operator Column_Name2 FROM
Table_Name <strong>WHERE Condition;</strong>
• SQL Modulus 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:


1.SELECT Column_Name_1 Modulus_Operator Column_Name2 FROM T
able_Name;
SQL Logical Operators

Operator Description Example

ALL TRUE if all of the subquery SELECT ProductName


values meet the condition FROM Products
WHERE ProductID = ALL (SELECT ProductID FROM OrderDetails WHERE Quantity = 10);
AND TRUE if all the conditions SELECT * FROM Customers
separated by AND is TRUE WHERE City = "London" AND Country = "UK";

ANY TRUE if any of the subquery SELECT * FROM Products


values meet the condition WHERE Price > ANY (SELECT Price FROM Products WHERE Price > 50);
BETWEEN TRUE if the operand is within SELECT * FROM Products
the range of comparisons WHERE Price BETWEEN 50 AND 60;
EXISTS TRUE if the subquery returns SELECT SupplierName
one or more records FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID =
Suppliers.supplierID AND Price < 20);
IN TRUE if the operand is equal to SELECT * FROM Customers
one of a list of expressions WHERE City IN ('Paris','London');
LIKE TRUE if the operand matches a SELECT * FROM Customers
pattern WHERE City LIKE 's%';

NOT Displays a record if the SELECT * FROM Customers


condition(s) is NOT TRUE WHERE City NOT LIKE 's%';
OR TRUE if any of the conditions SELECT * FROM Customers
separated by OR is TRUE WHERE City = "London" OR Country = "UK";

SOME TRUE if any of the subquery SELECT * FROM Products


values meet the condition WHERE Price > SOME (SELECT Price FROM Products WHERE Price > 20);
• SQL OR
• SELECT columns FROM tables WHERE condition 1 OR condition 2;
ID First_Name Last_Name Department Location

1 Harshad Kuwar Marketing Pune


2 Anurag Rajput IT Mumbai
3 Chaitali Tarle IT Chennai
4 Pranjal Patil IT Chennai
5 Suraj Tripathi Marketing Pune
6 Roshni Jadhav Finance Bangalore
7 Sandhya Jain Finance Bangalore

1. SELECT *FROM emp WHERE Department = "IT" OR Location = "Chennai";


• ID First_Name Last_Name Department Location

2 Anurag Rajput IT Mumbai


3 Chaitali Tarle IT Chennai
4 Pranjal Patil IT Chennai
• SQL AND
• The SQL AND condition is used in SQL query to create two or more conditions to be met.
• It is used in SQL SELECT, INSERT, UPDATE and DELETE
• Let's see the syntax for SQL AND:
• SELECT columns FROM tables WHERE condition 1 AND condition 2;
• The SQL AND condition require that both conditions should be met.
• The SQL AND condition also can be used to join multiple tables in a SQL statement.
• To understand this concept practically, let us see some examples.

ID First_Name Last_Name Department Location

1 Harshad Kuwar Marketing Pune


2 Anurag Rajput IT Mumbai
3 Chaitali Tarle IT Chennai
4 Pranjal Patil IT Chennai
5 Suraj Tripathi Marketing Pune
6 Roshni Jadhav Finance Bangalore
7 Sandhya Jain Finance Bangalore
• SELECT *FROM emp WHERE Department = "IT" AND Location = "Chennai";
ID First_Name Last_Name Department Location

3 Chaitali Tarle IT Chennai


4 Pranjal Patil IT Chennai

UPDATE emp SET Location = "Delhi" WHERE Department = "Marketing" AND First_Na
me = "Suraj";

ID First_Name Last_Name Department Location

1 Harshad Kuwar Marketing Pune


2 Anurag Rajput IT Mumbai
3 Chaitali Tarle IT Chennai
4 Pranjal Patil IT Chennai
5 Suraj Tripathi Marketing Delhi
6 Roshni Jadhav Finance Bangalore
7 Sandhya Jain Finance Bangalore
• SQL NOT Operator
• Range Searching (BETWEEN)
Roll_No Name Marks Age

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

• SELECT * FROM STUDENT WHERE Marks BETWEEN 80 and 100 ;


Roll_No Name Marks Age

1 Raman 95 20
2 Kapil 92 19
3 Arun 85 17
4 Ram 92 18
6 Sanjay 88 18
• NOT BETWEEN

• SELECT * FROM STUDENT WHERE Marks NOT BETWEEN 80 and 100 ;

Roll_No Name Marks Age

5 Suman 55 20
7 Sheetal 65 19
8 Rakesh 64 20
Emp_ID Name Emp_Salary Emp_Joining

1001 Vivek 9000 2021/01/02


1002 Saket 4000 2019/06/05
1003 Raman 3000 2020/02/05
1004 Suraj 6000 2020/04/01
1005 Seenu 5000 2019/12/04
1006 Shubham 10000 2021/02/05
1007 Anaya 4000 2020/12/20
1008 Parul 8000 2019/04/15

• UPDATE EMPLOYEE SET Emp_Salary = 25000 WHERE Emp_Joining BE


TWEEN '2019/06/05' and '2020/12/20' ;
Emp_ID Name Emp_Salary Emp_Joining

1002 Saket 25000 2019/06/05


1005 Seenu 25000 2019/12/04
1007 Anaya 25000 2020/12/20
1008 Parul 25000 2019/04/15
• Pattern Matching (LIKE)
• The LIKE is a logical operator in the Structured Query Language. This SQL operator is used
in the WHERE clause with the following three statements:
1.SELECT Statement
2.UPDATE Statement
3.DELETE Statement
• It filters the records from the columns based on the pattern specified in the SQL query.

• 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.

• Syntax of the LIKE operator in SQL

SELECT column_Name1, column_Name2 ...., column_NameN


FROM table_Name WHERE column_name LIKE pattern;
• Table : Employee
Emp_ID Name Emp_Salary Emp_Dept

1001 Vivek 9000 Finance


1002 Saket 4000 HR
1003 Raman 3000 Coding
1004 Suraj 6000 Coding
1005 Seenu 5000 HR
1006 Shubham 10000 Marketing
1007 Anaya 4000 Coding
1008 Parul 8000 Finance

• SELECT * FROM Employee WHERE Name LIKE 'S%' ;


Emp_ID Name Emp_Salary Emp_Dept

1002 Saket 4000 HR


1004 Suraj 6000 Coding
1005 Seenu 5000 HR
1006 Shubham 10000 Marketing
• SELECT * FROM Employee WHERE Emp_Dept LIKE '%g’ ;
Emp_ID Name Emp_Salary Emp_Dept

1003 Raman 3000 Coding


1004 Suraj 6000 Coding
1006 Shubham 10000 Marketing
1007 Anaya 4000 Coding

• SELECT Name, Emp_Salary FROM Employee WHERE Emp_Dept LIKE '


C%g' ;
•Name Emp_Salary

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

You might also like