LECTURE-4
Mapped with CO2
SQL Basic Structure
Basic structure of an SQL expression consists of select, from and where clauses.
o select clause lists attributes to be copied - corresponds to relationalalgebra project.
o from clause corresponds to Cartesian product - lists relations to be used.
o where clause corresponds to selection predicate in relational algebra.
The SELECT statement is used to select data from a database.
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
WHERE SQL clause
WHERE clause is used to specify/apply any condition while retrieving, updating or deleting data from
a table. This clause is used mostly with SELECT, UPDATE and DELETEquery.
The basic syntax of the SELECT statement with the WHERE clause is as shown below. SELECT
column1, column2, columnN
FROM table_name
WHERE [condition]
Example
Consider the CUSTOMERS table having the following records −
Figure 1: Customer Table [1]
The following code is an example which would fetch the ID, Name and Salary fields from the
CUSTOMERS table, where the salary is greater than 2000 −
This would produce the following result −
+ + + +
| ID | NAME | SALARY |
+ + + +
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+ + + +
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
SELECT column_name FROM table1UNION
SELECT column_name FROM table2;
The First table
ID NAME
1 Jack
2 Harry
3 Jackson
Figure 2: First Table [2]
The Second table
ID NAME
3 Jackson
4 Stephan
5 David
Figure 3: Second Table [2]
Union SQL query will be:SELECT * FROM First UNION
SELECT * FROM Second;
The result set table will look like:
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
Figure 4: First & Second Table [2]
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;
The result set table will look like:
-ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
Figure 5: Union of First & Second Table [2]
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.
o It has no duplicates and it arranges the data in ascending order by default.
Syntax
SELECT column_name FROM table1INTERSECT
SELECT column_name FROM table2;
Example:
Using the above First and Second table.
Intersect query will be:
SELECT * FROM FirstINTERSECT
SELECT * FROM Second;
The result set table will look like:
ID NAME
3 Jackson
Figure 6: Intersect of First & Second Table [2]
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:
SELECT column_name FROM table1MINUS
SELECT column_name FROM table2;
Example
Using the above First and Second table.
Minus query will be: SELECT * FROM FirstMINUS
SELECT * FROM Second;
The result set table will look like:
ID NAME
1 Jack
2 Harry
Figure 7: First & Second Table [2]
References
• 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
Structured Query Language (SQL) - GeeksforGeeks
Image References
1. Structured Query Language (SQL) - GeeksforGeeks
2. DBMS SQL Set Operation - javatpoint