Sql Select Distinct Statement
Syntax:
SELECT Distinct Column1,Column2….from table name;
Example:
In a table Customer_Data there are 10 records initially with duplicate Customer names.
- If you omit the DISTINCT keyword, the SQL statement returns the “Cust_Name” value from all the records of the “Customer_Data” table:Select Cust_Name from Customer_Data;By using the DISTINCT keyword , we can return the distinct values by removing duplicate names in customer names column.
Select distinct Cust_Name from Customer_Data;
Sql Where Statement
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
Syntax:
SELECT column1, column2, …FROM table_name WHERE condition;
Example:
In customer data table we can retrieve the customers who are from USA by using below query.
select * from Customer_Data where Cust_Country=’USA’;
Sql Order By Statement
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
Syntax:
SELECT column1, column2, …FROM table_nameORDER BY column1, column2, . ASC|DESC;
Example:
Data in our customers table before ordering is given below
Ascending of Salary column
Descending of Salary column
Sql AND Operator
The WHERE clause can contain one or many AND operators.
The AND operator is used to filter records based on more than one condition, like if you want to return all customers from India that starts with the letter ‘R’:
Syntax:
SELECT column1, column2, …
FROM table_name
WHERE condition1 AND condition2 AND condition3 …;Example:
By using below query only records with Customer ID ‘10’ and Customer Country ‘India’ will get retrieve.
select * from [dbo].[Customer_Data] where Cust_ID=10 AND Cust_Country=’India’;
Sql OR Operator
The WHERE clause can contain one or more OR operators.
The OR operator is used to filter records based on more than one condition, like if you want to return all customers from Vizag but also those from Bhopal
Syntax:
SELECT column1, column2, …
FROM table_name
WHERE condition1 OR condition2 OR condition3 …;Example:
By using below query records either Customer location ‘Vizag’ or Customer Country ‘Bhopal’ will get retrieve.
select * from [dbo].[Customer_Data] where Cust_Location=’Vizag’ or Cust_Location=’Bhopal’;