Lab 2 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

LAB # 2

UNION

The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It
removes duplicate rows between the various SELECT statements.
Each SELECT statement within the UNION must have the same number of fields in the result
sets with similar data types.
Syntax
The syntax for the UNION operator in SQL is:

SELECT expression1, expression2, ... expression_n


FROM tables
[WHERE conditions]
UNION
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

Parameters or Arguments
expression1, expression2, expression_n
The columns or calculations that you wish to retrieve.
TABLES:
The tables that you wish to retrieve records from. There must be at least one table listed in
the FROM clause.
WHERE conditions:
Optional. The conditions that must be met for the records to be selected.

Note:
- There must be same number of expressions in both SELECT statements.
- The corresponding expressions must have the same data type in the SELECT statements.
For example: expression1 must be the same data type in both the first and second SELECT
statement.

Example - Single Field With Same Name


Let's look at how to use the SQL UNION operator that returns one field. In this simple
example, the field in both SELECT statements will have the same name and data type.
For example:

SELECT supplier_id
FROM suppliers
UNION
SELECT supplier_id
FROM orders
ORDER BY supplier_id;

In this SQL UNION operator example, if a supplier_id appeared in both


the suppliers and orders table, it would appear once in your result set. The UNION operator
removes duplicates. If you do not wish to remove duplicates, try using the UNION ALL operator.
Now, let's explore this example further will some data.
If you had the suppliers table populated with the following records:
supplier_id supplier_name

1000 Microsoft

2000 Oracle

3000 Apple

4000 Samsung

And the orders table populated with the following records:


order_id order_date supplier_id

1 2015-08-01 2000

2 2015-08-01 6000

3 2015-08-02 7000

4 2015-08-03 8000

And you executed the following UNION statement:


SELECT supplier_id FROM suppliers
UNION
SELECT supplier_id FROM orders ORDER BY supplier_id;

You would get the following results:


supplier_id

1000

2000

3000

4000

6000

7000

8000

As you can see in this example, the UNION has taken all supplier_id values from both
the suppliers table as well as the orders table and returned a combined result set. Because the
UNION operator removed duplicates between the result sets, the supplier_id of 2000 only appears
once, even though it is found in both the suppliersand orders table. If you do not wish to remove
duplicates, try using the UNION ALL operator instead.

Example - Different Field Names


It is not necessary that the corresponding columns in each SELECT statement have the
same name, but they do need to be the same corresponding data types.
When you don't have the same column names between the SELECT statements, it gets a bit tricky,
especially when you want to order the results of the query using the ORDER BY clause.
Let's look at how to use the UNION operator with different column names and order the query
results.
For example:

SELECT supplier_id, supplier_name FROM suppliers WHERE supplier_id > 2000


UNION
SELECT company_id, company_name FROM companies WHERE company_id > 1000
ORDER BY 1;

In this SQL UNION example, since the column names are different between the two
SELECT statements, it is more advantageous to reference the columns in the ORDER BY clause
by their position in the result set. In this example, we've sorted the results by supplier_id /
company_id in ascending order, as denoted by the ORDER BY 1. The supplier_id / company_id
fields are in position #1 in the result set.
Now, let's explore this example further with data.
If you had the suppliers table populated with the following records:
supplier_id supplier_name

1000 Microsoft

2000 Oracle

3000 Apple

4000 Samsung

And the companies table populated with the following records:


company_id company_name

1000 Microsoft

3000 Apple

7000 Sony

8000 IBM

And you executed the following UNION statement:

SELECT supplier_id, supplier_name FROM suppliers WHERE supplier_id > 2000


UNION
SELECT company_id, company_name FROM companies WHERE company_id > 1000
ORDER BY 1;

You would get the following results:


supplier_id supplier_name

3000 Apple

4000 Samsung

7000 Sony

8000 IBM

First, notice that the record with supplier_id of 3000 only appears once in the result set because
the UNION query removed duplicate entries.
Second, notice that the column headings in the result set are called supplier_id and supplier_name.
This is because these were the column names used in the first SELECT statement in the UNION.
If you had wanted to, you could have aliased the columns as follows:

SELECT supplier_id AS ID_Value, supplier_name AS Name_Value


FROM suppliers
WHERE supplier_id > 2000
UNION
SELECT company_id AS ID_Value, company_name AS Name_Value
FROM companies
WHERE company_id > 1000
ORDER BY 1;

Now the column headings in the result will be aliased as ID_Value for the first column
and Name_Value for the second column.
ID_Value Name_Value

3000 Apple

4000 Samsung
7000 Sony

8000 IBM

INTERSECTION:

The SQL INTERSECT operator is used to return the results of 2 or more SELECT statements.
However, it only returns the rows selected by all queries or data sets. If a record exists in one query
and not in the other, it will be omitted from the INTERSECT results.

Explanation: The INTERSECT query will return the records in the blue shaded area. These are
the records that exist in both Dataset1 and Dataset2.
Each SQL statement within the SQL INTERSECT must have the same number of fields in the
result sets with similar data types.
NOTE: All rules are same for UNION and INTERSECTION except output.

Examples

Supplier table has following records


supplier_id supplier_name

1000 Microsoft

2000 Oracle

3000 Apple

4000 Samsung

And the orders table populated with the following records:


order_id order_date supplier_id

1 2015-08-01 2000

2 2015-08-01 6000

3 2015-08-02 7000

4 2015-08-03 8000

And you executed the following UNION statement:

SELECT supplier_id FROM suppliers


INTESECT
SELECT supplier_id FROM orders;

You would get the following results:


supplier_id

2000

Example 2:
If you had the suppliers table populated with the following records:
supplier_id supplier_name

1000 Microsoft

2000 Oracle

3000 Apple

4000 Samsung

And the companies table populated with the following records:


company_id company_name

1000 Microsoft
3000 Apple

4000 Sony

8000 Samsung

And you executed the following UNION statement:

SELECT supplier_id, supplier_name FROM suppliers


INTERSECT
SELECT company_id, company_name FROM companies

You would get the following results:


supplier_id supplier_name

3000 Apple

1000 Microsoft

SET DIFFERENCE:

The SQL EXCEPT operator is used to return all rows in the first SELECT statement that are not
returned by the second SELECT statement. Each SELECT statement will define a dataset. The
EXCEPT operator will retrieve all records from the first dataset and then remove from the results
all records from the second dataset.

Except Query

Explanation: The EXCEPT query will return the records in the blue shaded area. These are the
records that exist in Dataset1 and not in Dataset2.
Each SELECT statement within the EXCEPT query must have the same number of fields in the
result sets with similar data types.

NOTE: All rules of SET DIFFERENCE and UNION are same except OUTPUT.

Examples:
Supplier table has following records
supplier_id supplier_name

1000 Microsoft

2000 Oracle

3000 Apple

4000 Samsung

And the orders table populated with the following records:


order_id order_date supplier_id

1 2015-08-01 2000

2 2015-08-01 3000

3 2015-08-02 7000

4 2015-08-03 8000

And you executed the following UNION statement:

SELECT supplier_id FROM suppliers


EXCEPT
SELECT supplier_id FROM orders;

You would get the following results:


supplier_id

1000

4000

Example 2:
If you had the suppliers table populated with the following records:
supplier_id supplier_name

1000 Microsoft

2000 Oracle

3000 Apple

4000 Samsung

And the companies table populated with the following records:


company_id company_name

1000 Microsoft

3000 Apple

4000 Sony

8000 Samsung

And you executed the following UNION statement:

SELECT supplier_id, supplier_name FROM suppliers


EXCEPT
SELECT company_id, company_name FROM companies

You would get the following results:


supplier_id supplier_name

2000 Oracle

4000 Samsung

TASK 2.1
Assume that you have following database relations.
BIIT_Student ( CNIC, sName, sAge, sCity, Semester)
NUST_Student ( CNIC, sName, sAge, sCity, Semester, phoneNo)
UAAR_Student ( CNIC, sName, sAge,CGPA, sCity, Semester)

Write SQL query for the following statements.


NOTE: you must only use UNION / INTERSECTION / SET DIFFERENCE to achieve desired
results.

1- Display all CNIC numbers of BIIT students and NUST students. CNIC number must not
repeat.

2- Display distinct student names from all above three tables.

3- Display all those students who are studying in BIIT and UAAR at the same time.

4- Display names of those student who are only studying in BIIT but not in NUST.

5- Display all data of students of BIIT who are also enrolled in UAAR and NUST at the
same time.

6- Display list of all cities from all tables. ( city name must not repeat)

7- Display name of cities which exists in all three tables.

8- Display total number of students of BIIT who are only enrolled in BIIT.

9- Display total number of students who are enrolled in all three universities.

Display all those students of NUST who are also enrolled in BIIT but not in UAAR.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy