Friday, 16 February 2018

SQL INSERT INTO Query

INSERT INTO use for insert record in table

There are 2 ways to use INSERT INTO statement

1st

INSERT INTO YourTableName (ColumnName1,ColumnName1) VALUES (Value1,Value2)

2nd

INSERT INTO YourTableName (Value1, Value2)

Example

1. INSERT INTO City (ID, CityName) VALUES (1, 'Test 1')

2. INSERT INTO City VALUES (1, 'Test 1')

How to Use SQL ORDER BY Keyword

ORDER BY keyword is sorting result by ascending or descending order record wise

ORDER BY is by default sort result ascending not require to type ASC but if result get in decending than type DESC keyword with ORDER BY

ORDER BY Syntax

SELECT col1, col2 FROM YourTableName ORDER BY YourTable Column Like col1 ASC or DESC

Example

SELECT * FROM City ORDER BY CityName DESC

Result sort by CityName Z to A wise if type ASC keyword result sort by CityName A to Z

Use multiple column name in ORDER BY

Example

1. SELECT * FROM Student ORDER BY StudentName, Dateofbirth DESC

2. SELECT * FROM Student ORDER BY StudentName ASC, Dateofbirth DESC

SQL use AND, OR and NOT operator in WHERE Condition

In WHERE Condition we can use AND, OR and NOT operator

AND Operator use if All condition set in WHERE is TRUE

OR Operator use if Any 1 condition set in WHERE is TRUE

NOT Operator use if condition set in WHERE is NOT TRUE

AND Operator Syntax

SELECT * FROM YourTableName WHERE Condition1 AND Condition2

OR Operator Syntax

SELECT * FROM YourTableName WHERE Condition1 OR Condition2

NOT Operator Syntax

SELECT * FROM YourTableName WHERE NOT Condition1

Example

AND Operator

1. SELECT * FROM Student WHERE FirstName = 'Test1' AND LastName = 'Test2'

OR Operator

1. SELECT * FROM Student WHERE FirstName = 'Test1' OR LastName = 'Test2'

NOT Operator

1. SELECT * FROM Student WHERE NOT FirstName = 'Test1'

SQL WHERE Condition

WHERE Condition is used for filter records

WHERE Condition is used to filter records that match in specified WHERE condition

WHERE Syntax

SELECT C1, C2 FROM YourTableName WHERE YourCondition

Example

1. SELECT * FROM City WHERE CityName = 'Test'

2. SELECT * FROM Country WHERE CountryID = 1

Operators Use in Where Condition

1. = Equal Record match

2. <> or != Not Equal Record match

3. > Grater than record match

4. < Less than record match

5. >= Grater than and equal to record match

6. <= Less than and equal to record match

7. BETWEEN record filter between range

8. LIKE filter with pattern LIKE Operator condition use like %

9. IN filter all possible record in table

Example of operator wise

1. SELECT * FROM Country WHERE Name = 'India'

2. SELECT * FROM Country WHERE ID <> 1 or SELECT * FROM Country WHERE ID != 1

3. SELECT * FROM Employee WHERE Salary < 10000

4. SELECT * FROM Employee WHERE Salary > 10000

5. SELECT * FROM Employee WHERE Salary <= 10000

6. SELECT * FROM Employee WHERE Salary >= 10000

7. SELECT * FROM Employee WHERE Salary BETWEEN 5000 AND 10000

8. SELECT * FROM Employee WHERE EmployeeName LIKE '%A' end with A

9. SELECT * FROM Employee WHERE EmployeeName LIKE 'A%' start with A

10. SELECT * FROM Employee WHERE EmployeeName LIKE '%A%' start with A or end with A

11. SELECT * FROM Employee WHERE EmployeeName IN ('A', 'B', 'C')

SQL Select statement

Select statement use for select data from MS SQL database

Syntax  : SELECT column1, column2 FROM tablename

Example : SELECT ID, NAME FROM tblStudent

Result : Return your tblStudent table data ID & Name