Thursday, 31 October 2013

How to CREATE a Copy or Duplicate Table of another Table in SQL

SQL Query to Copy a Table Structure from another Table

Method 1 : INSERT INTO SELECT

This method is used when table is already created in the database earlier and data is to be inserted into this table from another table. If columns listed in insert clause and select clause are of same data type, they are are not required to list them. I always list them for readability and scalability purpose.

Copying Specific subset of columns into an Existing Table from another Existing Table : 

CREATE TABLE SampleTable(PID Int,FirstName VARCHAR(100), LastNameVARCHAR(100))

INSERT INTO 
SampleTable(PID,FirstName, LastName) SELECT PNo FName, LName FROM Persons. 


Notes :
Here the columns names may different but the data type should be same.

Copying all columns into an Existing Table from another Existing Table : 
To do this bothe tables should have same no.of columns and same data type.

INSERT INTO SampleTable SELECT * FROM Persons. 

Method 2 : SELECT INTO 

This method is used when table is not created earlier and needs to be created when data from one table is to be inserted into newly created table from another table. New table is created with same data types as selected columns.


The easiest way to create a copy of a table is to use a Transact-SQL command SELECT INTO to extract all the rows from an existing table into the new table. The new table must not exist already. The following example will copy the Customers table under the Sales Schema to a new table called NewCustomers under the TPR Schema:


We can create a Table from an existing Table by copying the existing Table's Columns.
It is important to note that when creating a Table in this way, the New Table will be populated with the records from the existing Table (based on the SQL SELECT Statement).

SELECT * INTO TPR.NewCustomers FROM Sales.Customers;

Copying Specific subset of columns from Existing Table to New Table
You can also create the new table from a specific subset of columns in the original table. In this case, you specify the names of the columns to copy after the SELECT keyword. Any columns not specified are excluded from the new table. The following example copies specific columns to a new table:

SELECT CustName, Address, Telephone, Email INTO TPR.NewCustomers

FROM Sales.Customers;

Copying only Table Structure from Existing Table to New Table :
Creating a duplicate table with only the structure duplicated with a new name as follows :
SELECT * INTO TPR.NewCustomers FROM Sales.Customers where 1=2;

Oracle/PL-SQL :

Copying all Columns from another Table
The syntax for CREATING a Table by copying all Columns from another Table is:
CREATE TABLE New_Table
  AS (SELECT * FROM Old_Table);
For Example:
CREATE TABLE Vendors
AS (SELECT *
    FROM Customers
    WHERE id > 1000);
This would create a New Table called Vendors that included all Columns from the Customers Table.
If there were records in the Customers Table, then the New Vendors Table would also contain the records selected by the SELECT statement.

Copying selected Columns from another Table
The syntax for CREATING a Table by copying selected Columns from another Table is:
CREATE TABLE New_Table
  AS (SELECT Column_1, Column2, ... Column_n FROM Old_Table);
For Example:
CREATE TABLE Vendors
  AS (SELECT id, address, city, state, zip
      FROM Customers
      WHERE id > 1000);
This would create a New Table called Vendors, but the New Table would only include the specified Columns from the Customers Table.
Again, if there were records in the Customers Table, then the New Vendors Table would also contain the records selected by the SELECT statement.

Copying selected Columns from multiple Tables
The syntax for CREATING a Table by copying selected Columns from multiple Tables is:
CREATE TABLE New_Table
  AS (SELECT Column_1, Column2, ... Column_n
      FROM Old_Table_1, Old_Table_2, ... Old_Table_n);
For Example:
CREATE TABLE Vendors
  AS (SELECT Customers.id, Customers.address, Categories.cat_type
      FROM Customers, Categories
      WHERE Customers.id = Categories.id
      AND Customers.id > 1000);
This would create a New Table called Vendors based on Columns from both the Customers and Categories Tables.

F A Qs :
Question: How can I create an SQL Table from another Table without copying any values from the Old Table?
Answer: To do this, the syntax is:
CREATE TABLE New_Table
  AS (SELECT * FROM Old_Table WHERE 1=2);
For Example:
CREATE TABLE Vendors
  AS (SELECT * FROM Customers WHERE 1=2);


This would create a New Table called Vendors that included all Columns from the Customers Table, but no data from the CustomersTable

Wednesday, 23 October 2013

What is the Difference between SQL Inner Join and Outer Join

What is SQL JOIN Clause :
The SQL JOIN is a clause that enables a SELECT statement to access more than one table. The JOIN clause controls how tables are linked. It is a qualifier of the SQL FROM clause.

In SQL joins are used to get data from two or more tables based on relationship between some of the columns in tables. In most of the cases we will use primary key of first table and foreign key of secondary table to get data from tables by using this relationship we can reduce the duplication of data in every table.

Why and How to use JOIN Clause :
Use the SQL JOIN whenever multiple tables must be accessed through a SQL SELECT statement and no results should be returned if there is not a
match between the JOINed tables.

The ON clause describes the conditions of the JOIN.

SQL JOIN Syntax :

SELECT <column_name1>, <column_name2> <aggregate_function> FROM <table_name> JOIN <table_name> ON <join_conditions>

SQL JOIN Example : 
Let us suppose there are two tables called CUSTOMERS and ORDERS as follows

Customers Table 
CREATE TABLE CUSTOMERS(CID INT PRIMARY KEY IDENTITY(111,1) ,CNAME VARCHAR(15),LOCATION VARCHAR(15));

INSERT INTO CUSTOMERS VALUES ('JAN','HYDERABAD'),('FEB','BANGALORE'),('MAR','CHENNAI'),
('APR','PUNE'),('MAY','HYDERABAD'),('JUN','BANGALORE'),('JULY','BANGALORE');
  
CID
CNAME
LOCATION
111
JAN
HYDERABAD
112
FEB
BANGALORE
113
MAR
CHENNAI
114
APR
PUNE
115
MAY
HYDERABAD
116
JUN
BANGALORE
117
JULY
BANGALORE

 Orders Table
CREATE TABLE ORDERS(OID INT IDENTITY(1234,3),PRODUCT VARCHAR(15),CID INT);

INSERT INTO ORDERS VALUES('LAPTOP',113),('TV',115),('COOLER',117),
('FRIDGE',143),('PEN DRIVE',789);

OID
PRODUCT
CID
1234
LAPTOP
113
1237
TV
115
1240
COOLER
117
1243
FRIDGE
143
1246
PEN DRIVE
789

Now we perform the join on the above tables as follows 
SELECT C.CID,C.CNAME,C.LOCATION,O.OID,O.PRODUCT FROM CUSTOMERS C JOIN ORDERS O ON C.CID=O.CID ;

This join is also known as Normal Join or Inner Join.
The result of the Join is as follows :

CID
CNAME
LOCATION
OID
PRODUCT
113
MAR
CHENNAI
1234
LAPTOP
115
MAY
HYDERABAD
1237
TV
117
JULY
BANGALORE
1240
COOLER

Inner Join Vs Outer Join
The standard JOIN clause (also known as the INNER JOIN clause) differs from the OUTER JOIN in that rows are returned only when there are matches for the JOIN critieria on the second table.If there are no matches on the JOIN criteria then no rows will be returned. This is known an "INNER JOIN".

An INNER JOIN should be used when you only want to pull data that exists in both of the tables. Any entry found in the first table, that does not also have an entry in the second table, will not be included in the result set.


An OUTER JOIN should be used when you want to pull all of the matching data in the first table, regardless of whether or not it exists in the second table. Your result set will contain NULL for the variables that would have been found in the second table.


Use the "OUTER JOIN" in cases where rows should be returned when one side of the join is missing.

Notes :
A "Cartesian product" can result if there is no relation between the tables for the join. A row would be included for each combination between the two tables so if one table has 1,000 rows and the second table has 2,000 rows then 2,000,000 rows would be returned.

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts