Lecture 4 Unit 2
Lecture 4 Unit 2
Basic structure of an SQL expression consists of select, from and where clauses.
The data returned is stored in a result table, called the result-set.To fetch the entire table or all the
fields in the table:
SELECT * FROM table_name;To fetch individual column data
SELECT column1,column2 FROM table_name
Example
From clause:
From clause can be used to specify a sub-query expression in SQL. The relation produced by the
sub-query is then used as a new relation on which the outer query is applied.
Sub queries in the from clause are supported by most of the SQL implementations.
The correlation variables from the relations in from clause cannot be used in the sub-queries in
the from clause.
Syntax:
SELECT column1, column2 FROM
(SELECT column_x as C1, column_y FROM table WHERE PREDICATE_X)as table2
WHERE PREDICATE;
SET Operations
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.
In this tutorial, we will cover 4 different types of SET operations, along with example:
1.UNION
2.UNION ALL
3.INTERSECT
4.MINUS
1.Union
o The SQL Union operation is used to combine the result of two or more SQL SELECTqueries.
o In the union operation, all the number of datatype and columns must be same in both the tables
on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its result set.
Syntax
ID NAME
1 Jack
2 Harry
3 Jackson
ID NAME
3 Jackson
4 Stephan
5 David
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
2. Union All
Union All operation is equal to the Union operation. It returns the set without removing duplication
and sorting the data.
Syntax:
SELECT column_name FROM table1UNION ALL
SELECT column_name FROM table2;
Example: Using the above First and Second table. Union All query will be like:
SELECT * FROM FirstUNION ALL
SELECT * FROM Second;
-ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
3.Intersect
o It is used to combine two SELECT statements. The Intersect operation returns the common rows
from both the SELECT statements.
o In the Intersect operation, the number of datatype and columns must be the same.
Syntax
Example:
Using the above First and Second table.
ID NAME
3 Jackson
4.Minus
o It combines the result of two SELECT statements. Minus operator is used to display the rows which
are present in the first query but absent in the second query.
o It has no duplicates and data arranged in ascending order by default.
Syntax:
Example
Using the above First and Second table.
ID NAME
1 Jack
2 Harry
• Books References
Database System Concepts by Abraham Silberschatz, Henry Korth, and S. Sudarshan (7th Edition)
• E-book link
://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://mrcet.com/downlo
ads/digital_notes/ECE/III%2520Year/DATABASE%2520MANAGEMENT%2520SYSTEMS.pdf&ved=2
ahUKEwiT0uTilpOKAxW_- jgGHSSwKGUQFnoECBcQAQ&usg=AOvVaw1BuxUTRDGvwmONTzSsw5M4
• YouTube link
https://youtu.be/6Iu45VZGQDk?si=jezs0C8I30dZwlyR
• Web link
Image References