U3 - SQL Set Operation
U3 - SQL Set Operation
The SQL Set operation is used to combine the two or more SQL SELECT
statements.
1. Union
o The SQL Union operation is used to combine the result of two or more SQL
SELECT queries.
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 resultset.
Syntax
Example:
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:
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.
o It has no duplicates and it arranges the data in ascending order by default.
Syntax
Example:
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
ID NAME
1 Jack
2 Harry
5.Except:
Unique rows from the first table that are not found in the second table are selected.
Syntax :
Except
Result :
ID NAME
1 Jack
2 Harry
Aggregate functions are used to perform calculations on a set of values and return a single value.
1. COUNT():
Syntax;
COUNT(column_name);
2. SUM():
Syntax:
SUM(column_name);
3. AVG():
AVG(column_name);
Example:
FROM sales
GROUP BY product;
Output:
product total_sales
Apple 550
Banana 450
A nested subquery (or subquery within a subquery) is a query inside another query. It is used to
perform operations that require a second query to filter or manipulate the data of the main query.
Syntax:
FROM table_name
A join in SQL is used to combine rows from two or more tables based on a related column between
them. The join expression specifies the condition for combining the rows, typically involving
matching columns from the tables.
Types of Joins:
1. Inner Join
3. Cross Join
4. Self Join
1. Inner Join
The INNER JOIN returns only the rows where there is a match in both tables based on the join
condition.
SELECT columns
FROM table1
ON table1.column = table2.column;
Example:
Tables:
employees:
departments:
dept_id dept_name
101 HR
102 IT
Query:
FROM employees
Output:
emp_name dept_name
Alice HR
Bob IT
The LEFT JOIN returns all rows from the left table, and matching rows from the right table. If no
match is found, NULL is returned for columns from the right table.
The RIGHT JOIN is similar to the left join, but it returns all rows from the right table and matching
rows from the left table.
The FULL OUTER JOIN returns all rows from both tables. If there’s no match, NULL is returned for
missing values.
5. Cross Join
The CROSS JOIN produces a Cartesian product, meaning every row in the first table is combined with
every row in the second table.
6. Self Join
A SELF JOIN is a join of a table with itself. It is useful for comparing rows within the same table.
Transactions in SQL
A transaction in SQL is a sequence of one or more SQL statements that are executed as a single
unit of work.
Transactions are used to ensure data integrity and consistency in a database, especially in
environments with multiple concurrent users or operations.
o If one part of the transaction fails, the entire transaction is rolled back.
2. Consistency:
o Ensures that the database transitions from one valid state to another valid state.
3. Isolation:
o Changes made by one transaction are not visible to other transactions until
committed.
4. Durability:
o Ensures that once a transaction is committed, its changes are permanent, even in
the event of a system failure.
Integrity constraints are rules applied to database tables to ensure the accuracy, consistency,
and reliability of the data stored in them. These constraints enforce business rules on data,
ensuring that the database remains valid and meaningful.
A column or combination of columns marked as a primary key must contain unique and
non-null values.
Syntax:
emp_name VARCHAR(50)
);
Ensures that a value in one table corresponds to a value in another table (referential
integrity).
Syntax:
customer_id INT,
);
Note here the customer _id is common in both tables (orders and customers).
Syntax:
);
Authorization in SQL
Authorization in SQL refers to the process of granting or restricting access to database resources
based on user roles and privileges. It ensures that only authorized users can perform specific
operations, such as reading, modifying, or deleting data, maintaining data security and integrity.
Syntax:
Example :