Professional Documents
Culture Documents
Unit 3
Unit 3
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Derived Operations:
1. Natural Join (⋈)
2. Left, Right, Full outer join (⟕, ⟖, ⟗)
3. Intersection (∩)
4. Division (÷)
σ Condition/Predicate(Relation/Table name)
Select Operator (σ) Example
Table: CUSTOMER
---------------
σ Customer_City="Agra" (CUSTOMER)
Output:
σ (CUSTOMER)
Table: CUSTOMER
Customer_Name Customer_City
------------- -------------
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Carl Delhi
∏ Customer_Name (CUSTOMER)
Output:
table_name1 ∪ table_name2
Union Operator (∪) Example
Table 1: COURSE
Student_Name
------------
Aditya
Carl
Paul
Lucy
Rick
Steve
Note: As you can see there are no duplicate names present in the output even
though we had few common names in both the tables, also in the COURSE table
we had the duplicate name itself.
Note: Only those rows that are present in both the tables will appear in the result
set.
table_name1 ∩ table_name2
Intersection Operator (∩) Example
Lets take the same example that we have taken above.
Table 1: COURSE
Student_Name
------------
Aditya
Steve
Paul
Lucy
table_name1 - table_name2
Set Difference (-) Example
Lets take the same tables COURSE and STUDENT that we have seen above.
Query:
Lets write a query to select those student names that are present in STUDENT
table but not present in COURSE table.
Student_Name
------------
Carl
Rick
R1 X R2
Cartesian product (X) Example
Table 1: R
R
Col_A Col_B
----- ------
AA 100
BB 200
CC 300
Table 2: S
S
Col_X Col_Y
----- -----
XX 99
YY 11
ZZ 101
Query:
Lets find the cartesian product of table R and S.
RXS
Output:
Rename (ρ)
Rename (ρ) operation can be used to rename a relation or an attribute of a relation.
Rename (ρ) Syntax:
ρ(new_relation_name, old_relation_name)
Table: CUSTOMER
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
----------
Steve
Raghu
Chaitanya
Ajeet
Carl
Join Operations
Types of JOIN:
Inner Joins:
Theta join
EQUI join
Natural join
Outer join:
Inner Join:
In an inner join, only those tuples that satisfy the matching criteria are included, while
the rest are excluded. Let's study various types of Inner Joins:
Theta Join:
The general case of JOIN operation is called a Theta join. It is denoted by symbol θ
Example : A ⋈θ B
Table A Table B
1 1 1 1
1 2 1 3
For example e: A ⋈ A.column 2 > B.column 2 (B)
column 1 column 2
1 2
EQUI JOIN:
When a theta join uses only equivalence condition, it becomes a equi join.
column 1 column 2
1 1
Natural join can only be performed if there is a common attribute (column) between
the relations. The name and type of the attribute must be same.
Example :
Num Square
2 4
3 9
D
Num Cube
2 8
3 27
C⋈D
C⋈D
2 4 4
3 9 27
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria, we also include some or all
tuples that do not match the criteria.
In the left outer join, operation allows keeping all tuple in the left relation.
if there is no matching tuple is found in right relation, then the attributes of right relation
in the join result are filled with null values.
Consider the following 2 Tables
Num Square
2 4
3 9
4 16
Num Cube
2 8
3 18
5 75
2 4 4
3 9 9
4 16 -
Right Outer Join: ( A B )
In the right outer join, operation allows keeping all tuple in the right relation.
if there is no matching tuple is found in the left relation, then the attributes of the left
relation in the join result are filled with null values.
2 8 4
3 18 9
5 75 -
In a full outer join, all tuples from both relations are included in the result, irrespective of
the matching condition.
Result of outer join contains all rows of bpth tables
2 4 8
3 9 18
4 16 -
5 - 75
SQL supports few Set operations which can be performed on the table data. These are used to get
meaningful results from data stored in the table, under different special conditions.
UNION
UNION ALL
INTERSECT
MINUS
UNION OPERATION
The First table,
ID Name
1 abhi
2 adam
The Second table,
ID Name
2 adam
3 Chester
ID NAME
1 abhi
2 adam
3 Chester
UNION ALL
This operation is similar to Union. But it also shows the duplicate rows.
The First table,
ID NAME
1 abhi
2 adam
The Second table,
ID NAME
2 adam
3 Chester
ID NAME
1 abhi
2 adam
2 adam
3 Chester
INTERSECT
Intersect operation is used to combine two SELECT statements.
it only retuns the records which are common from both SELECT statements.
In case of Intersect the number of columns and datatype must be same.
Example of Intersect
The First table,
ID NAME
1 abhi
2 adam
The Second table,
ID NAME
2 adam
3 Chester
2 adam
MINUS
The Minus operation combines results of two SELECT statements and return only those in the
final result, which belongs to the first set of the result.
Example of Minus
The First table,
ID NAME
1 abhi
2 adam
The Second table,
ID NAME
2 adam
3 Chester
ID NAME
1 abhi
SQL aggregation function is used to perform the calculations on multiple rows of a single
column of a table.
It returns a single value.
It is also used to summarize the data.
COUNT function is used to Count the number of rows in a database table. It can work on
both numeric and non-numeric data types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and Null.
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example: COUNT( )
SELECT COUNT(*) FROM PRODUCT_MAST;
Output: 10
1. SELECT COUNT(*) FROM PRODUCT_MAST WHERE RATE>=20;
Output: 7
SELECT COUNT(DISTINCT COMPANY) FROM PRODUCT_MAST;
Output: 3
SUM FUNCTION
Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.
Example: SUM()
SELECT SUM(COST) FROM PRODUCT_MAST;
Output: 670
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3;
Output: 320
AVG FUNCTION (AVERAGE)
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Example: SELECT AVG(COST) FROM PRODUCT_MAST;
Output: 67.00
MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column.
Syntax : MAX() or MAX( [ALL|DISTINCT] expression )
Example: SELECT MAX(RATE) FROM PRODUCT_MAST;
OUTPUT:30
MIN function is used to find the minimum value of a certain column. This function determines
the smallest value of all selected values of a column.
Syntax :MIN() or MIN( [ALL|DISTINCT] expression )
Example: SELECT MIN(RATE) FROM PRODUCT_MAST;
Output: 10
The GROUP BY clause is a SQL command that is used to group rows that have the
same values.
The GROUP BY clause is used in the SELECT statement.
it is used in conjunction with aggregate functions to produce summary reports from the
database.
The queries that contain the GROUP BY clause are called grouped queries and only
return a single row for every grouped item.
gender
Female
Female
Male
Female
Male
Male
Male
Male
Male
Suppose we want to get the unique values for genders. We can use a following query -
gender
Female
Male
Note :
Only two results have been returned. This is because we only have two gender types Male and
Female. The GROUP BY clause in SQL grouped all the "Male" members together and returned
only a single row for it. It did the same with the "Female" members.
Suppose that we want to get a list of movie category_id and corresponding years in which they
were released.
category_id year_released
1 2011
2 2008
NULL 2008
NULL 2010
8 2007
6 2007
6 2007
8 2005
NULL 2012
7 1920
8 NULL
8 1920
Executing the above script in MySQL workbench against the myflixdb gives us the
following results shown below.
category_id year_released
NULL 2008
NULL 2010
NULL 2012
1 2011
2 2008
6 2007
7 1920
8 1920
8 2005
8 2007
The GROUP BY clause operates on both the category id and year released to
identify unique rows in our above example.
If the category id is the same but the year released is different, then a row is treated as a
unique one .If the category id and the year released is the same for more than one row, then
it's considered a duplicate and only one row is shown.
Suppose we want total number of males and females in our database. We can use the following
script shown below to do that.
gender COUNT('membership_number')
Female 3
Male 5
The results shown below are grouped by every unique gender value posted and the number of
grouped rows is counted using the COUNT aggregate function.
It's not always that we will want to perform groupings on all the data in a given table.
There will be times when we will want to restrict our results to a certain given criteria.
In such cases , we can use the HAVING clause
Suppose we want to know all the release years for movie category id 8. We would use the
following script to achieve our results.
SELECT * FROM `movies` GROUP BY `category_id`,`year_released` HAVING
`category_id` = 8;
Note only movies with category id 8 have been affected by our GROUP BY clause.
Points To Remember
The GROUP BY Clause SQL is used to group rows with same values.
The GROUP BY Clause is used together with the SQL SELECT statement.
The SELECT statement used in the GROUP BY clause can only be used contain column
names, aggregate functions, constants and expressions.
SQL Having Clause is used to restrict the results returned by the GROUP BY clause.
SQL GROUP BY Clause is used to collect data from multiple records and returned
record set by one or more columns.
The GROUP BY clause is a SQL command that is used to group rows that have the
same values.
The GROUP BY clause is used in the SELECT statement.
it is used in conjunction with aggregate functions to produce summary reports from the
database.
The queries that contain the GROUP BY clause are called grouped queries and only
return a single row for every grouped item.
gender
Female
Female
Male
Female
Male
Male
Male
Male
Male
Suppose we want to get the unique values for genders. We can use a following query -
gender
Female
Male
Note :
Only two results have been returned. This is because we only have two gender types Male and
Female. The GROUP BY clause in SQL grouped all the "Male" members together and returned
only a single row for it. It did the same with the "Female" members.
category_id year_released
1 2011
2 2008
NULL 2008
NULL 2010
8 2007
6 2007
6 2007
8 2005
NULL 2012
7 1920
8 NULL
8 1920
Executing the above script in MySQL workbench against the myflixdb gives us the
following results shown below.
category_id year_released
NULL 2008
NULL 2010
NULL 2012
1 2011
2 2008
6 2007
7 1920
8 1920
8 2005
8 2007
The GROUP BY clause operates on both the category id and year released to
identify unique rows in our above example.
If the category id is the same but the year released is different, then a row is treated as a
unique one .If the category id and the year released is the same for more than one row, then
it's considered a duplicate and only one row is shown.
Suppose we want total number of males and females in our database. We can use the following
script shown below to do that.
gender COUNT('membership_number')
Female 3
Male 5
The results shown below are grouped by every unique gender value posted and the number of
grouped rows is counted using the COUNT aggregate function.
It's not always that we will want to perform groupings on all the data in a given table.
There will be times when we will want to restrict our results to a certain given criteria.
In such cases , we can use the HAVING clause
Suppose we want to know all the release years for movie category id 8. We would use the
following script to achieve our results.
Points To Remember
The GROUP BY Clause SQL is used to group rows with same values.
The GROUP BY Clause is used together with the SQL SELECT statement.
The SELECT statement used in the GROUP BY clause can only be used contain column
names, aggregate functions, constants and expressions.
SQL Having Clause is used to restrict the results returned by the GROUP BY clause.
SQL GROUP BY Clause is used to collect data from multiple records and returned
record set by one or more columns.
A Subquery or Nested Query is a query within another SQL query and embedded within
the WHERE clause.
IMPORTANT RULE:
A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.
You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
A subquery is a query within another query. The outer query is known as the main query,
and the inner query is known as a subquery.
Subqueries are on the right side of the comparison operator.
A subquery is enclosed in parentheses.
In the Subquery, ORDER BY command cannot be used. But GROUP BY command can
be used to perform the same function as ORDER BY command.
SQL subqueries are most frequently used with the Select statement.
SYNTAX:
SELECT column_name FROM table_name WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );
Example:
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
SELECT * FROM EMPLOYEE WHERE ID IN (SELECT ID FROM EMPLOYEE W
HERE SALARY > 4500);
4 Alina 29 UK 6500.00
SQL subquery can also be used with the Insert statement. In the insert statement, data
returned from the subquery is used to insert into another table.
In the subquery, the selected data can be modified with any of the character, date
functions.
Syntax:
INSERT INTO table_name (column1, column2, column3....) SELECT * FROM table_nam
e
WHERE VALUE OPERATOR
Example
Consider a table EMPLOYEE_BKP with similar as EMPLOYEE.
Now use the following syntax to copy the complete EMPLOYEE table into the EMPLOYEE_BKP table.
INSERT INTO EMPLOYEE_BKP SELECT * FROM EMPLOYEE WHERE ID IN
(SELECT ID FROM EMPLOYEE);
The subquery of SQL can be used in conjunction with the Update statement.
When a subquery is used with the Update statement, then either single or multiple columns in a
table can be updated.
Syntax
UPDATE table SET column_name = new_value WHERE VALUE OPERATOR (SELE
CT COLUMN_NAME FROM TABLE_NAME WHERE condition);
Example
UPDATE EMPLOYEE SET SALARY = SALARY * 0.25 WHERE AGE IN
(SELECT AGE FROM CUSTOMERS_BKP WHERE AGE >= 29);
This would impact three rows, and finally, the EMPLOYEE table would have the following records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
The subquery of SQL can be used in conjunction with the Delete statement just
like any other statements mentioned above.
Syntax :
DELETE FROM TABLE_NAME WHERE VALUE OPERATOR (SELECT COLUMN_
NAME FROM TABLE_NAME WHERE condition);
Example
DELETE FROM EMPLOYEE WHERE AGE IN
(SELECT AGE FROM EMPLOYEE_BKP WHERE AGE >= 29 );
This would impact three rows, and finally, the EMPLOYEE table would have the following records.
1 John 20 US 2000.00