Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

SQL SELECT DISTINCT statement

The SQL SELECT DISTINCT statement is used to remove duplicates from the result set of a SELECT statement and return only distinct values.

Syntax:

SELECT DISTINCT column 1,column 2…..column n
FROM table_name;



Example:

SELECT DISTINCT CategoryName,[Description] FROM Categories

Result:


Example with all columns:

SELECT DISTINCT * FROM Categories 

In this example distinct keyword compares all column values if found different all columns values then only return distinct values. 

Result:

above original diagram with duplicate values.



SQL Insert into Select

The INSERT INTO statement is used to insert new records in a RDBMS database table.
Also you can copy data from one table and insert into another table using SQL Insert into select statement.

Syntax:

INSERT INTO tableName
VALUES (value1,value2,...value n);

 

For specific columns:

 

INSERT INTO tableName (Column 1,column 2..column n)
VALUES (value1,value2,...value n);

 

How to insert records from one table to another table

INSERT INTO Products ([ProductCode],
            [Type] ,
            [Description],
            [isActive])
            
            SELECT [ProductCode],
            [ProductType],
            [Description],
            [IsAvailable] FROM SellableProducts WHERE
            countryCode='IN'

SQL DELETE statement

SQL DELETE statement is used to delete the existing records from a RDBMS database table.

Where statement is used to delete specific records.

Syntax:

DELETE FROM table_name
WHERE x_column=x_value



Delete with table join example:

DELETE FROM D
FROM dbo.designation AS D
INNER JOIN MasterDesignation U on U.UserId=D.EmployeeId 
Where DesId=