Lab Manual 09 PDF
Lab Manual 09 PDF
Lab Instructor:
Ms. Hira Kanwal
Student Name
Student Roll #
Department
Batch/Year/Section
Marks Signature
9.1. Objective
1. SELECT INTO Statement
2. Union
3. Intersect
4. Except Query
5. Database Updates
The SELECT INTO statement copies data from one table and inserts it into a new table.
SELECT *
Into [newtablename]
FROM [existingtablename]
OR
select *
into Students
from Std
The SELECT INTO statement can also be used to create a new, empty table using the schema
of another. Just add a WHERE clause that causes the query to return no data:
SELECT *
INTO newtable
FROM Student
WHERE 1=0
The SELECT INTO Statement is also used to combine data from two tables, and save them
in one new table:
select *
into StudentBackup
from Std join StdMarks
on Std.StdId = StdMarks.Roll#
9.3. UNION
The SQL UNION operator combines the result of two or more SELECT statements.
1. Notice that each SELECT statement within the UNION must have the same number
of columns.
2. The columns must also have similar data types.
3. Also, the columns in each SELECT statement must be in the same order.
4. The UNION operator selects only distinct values by default. To allow duplicate
values, use the ALL keyword with UNION.
Example:
Example:
select fax from customer
where fax is not null
union all
select fax from cust
9.4. Intersect
INTERSECT returns only common rows returned by the two SELECT statements.
Just as with the UNION operator, the same rules apply when using the INTERSECT
operator.
9.5. Except
EXCEPT returns only rows, which are not available in second SELECT statement.
Syntax:
Example:
Update Emp
Set Salary = Salary * 1.05
where EmpId = 1
9.7.2. Copy the packages table in to another table only for packages where speed is greater
than 2Mbps. Name the new table as highspeedpackages.
9.7.4. Create a report showing the distinct contact name and phone numbers for all
employees, customers, and suppliers.
9.7.5. Write a query to show all the phone numbers from customers and suppliers, with
duplicate results if any.
9.7.6. Modify the above query to include only the values in phone numbers from customers
and suppliers table that match.
9.7.7. Modify the above query to include only the values in phone numbers from customers
table that donot match suppliers table.
9.7.8. In the orders database, select all data from those suppliers and customers who belong
to state USA.
9.7.9. Based on the Customer table in ACDB database, update the First_name to 'Rehman'
for all records whose customer id is 162.
9.7.10. Delete all those rows from packages table where speed = 1kbps.
9.7.11. Insert new record in all tables of ACDB database, and then back up all data of
customers table in to new table backupcustomers.