Lab4 - DML3 - DML4
Lab4 - DML3 - DML4
1- UNION OPERATOR: combines the results of two SQL queries into a single table of all
matching rows. The two queries must result in the same number of columns and
compatible data types in order to unite. Any duplicate records are automatically removed
unless UNION ALL is used.
2- INTERSECT OPERATOR: takes the results of two queries and returns only rows that
appear in both result sets. The INTERSECT operator removes duplicate rows from the final
result set. The INTERSECT ALL operator does not remove duplicate rows from the final
result set.
3- EXCEPT OPERATOR: takes the distinct rows of one query and returns the rows that do not
appear in a second result set. The EXCEPT ALL operator does not remove duplicates.
Aggregate functions are used to summarize information from multiple tuples into a single-tuple
summary.
Grouping is used to create subgroups of tuples before summarization
INTERSECT
SELECT Suppliers.CompanyName
WHERE Suppliers.SupplierID
=Products.SupplierID
AND Products.CategoryID
=Categories.CategoryID
FROM Employees
Comments:
A Subquery is a query within another query. Subqueries can reside in WHERE Clause, FROM
Clause or SELECT Clause.
1. Display the first name, title of all employees who live in UK and have the
same job (title) as Janet or Steven. Sort the result by the first name of the
employees in a descending order.
2. Get a list of orders (with order dates) that were processed by employees
who are not sales representatives:
1. Get the distinct names for products that are always ordered with quantity
greater than 10.
2. Get the customer name and title for customers that have not placed any orders.
SELECT ContactName,ContactTitle
FROM Customers AS c
WHERE Not EXISTS(SELECT *
FROM [Orders] AS o
WHERE o.CustomerID = c.CustomerID)
Note:
• When using subquery in the where clause , take care when using operators ; If
the subquery returns more than one value you can’t use operator such as = , but
you may use the IN operator.
• (IN) predicate can either return TRUE, FALSE or NULL :
o TRUE is returned when the required value is NOT NULL and could be
found.
o FALSE is returned when the required value is NOT NULL and is NOT found
and the list DOES NOT contain NULL values
o NULL is returned when the required value is NULL, or when the required
values is NOT NULL and was not found in the list and the list contains at
least one NULL value
• EXISTS condition is considered "to be met" if the sub query returns at least one
row, it always returns TRUE or FALS
o TRUE as soon as it finds only a single matching row in the sub query
o FALSE, if it find none.
• NOT EXISTS will return TRUE only if no row is returned from the sub query.
4. Using Sub-queries in the where clause – “All”
1. Get the last name, all emplyees who were hired before all emplyees
working as ‘Sales Managers’ and ‘Inside Sales Coordinators’.
SELECT LastName
FROM Employees
where HireDate < ALL (SELECT HireDate
FROM Employees
WHERE Title IN ('Sales Manager', 'Inside Sales Coordinator'))
Note:
• Mainly we use this way to shrink the size of the table and decrease the cost of
working with the whole table (considering all columns in it). So, we choose only the
set of columns we need from the table(s) related to our query.
• As shown in the above example, we can give an alias to the subquery and use it in
the main query.