Saturday, 17 February 2018

How to use LEFT OUTER JOIN in SQL Query

LEFT OUTER JOIN Syntax

SELECT YourColumnName FROM YourTableName
LEFT OUTER JOIN InnerTableName ON InnerTableName.Column = YourTableName.Column

Example

SELECT * FROM City
LEFT JOIN Country ON Country.CountryID = City.CountryIDF

OR

SELECT * FROM City
LEFT OUTER JOIN Country ON Country.CountryID = City.CountryIDF

How to use SUM() function in SQL Query

SUM() function return total sum of numeric, int, float, bigint value

SUM() Syntax

SELECT SUM(YourNumericValueColumn) WHERE YourTableName

Example

SELECT SUM(Salary) FROM Employee

How to user AVG() function in SQL Query

AVG() function return average value from table

AVG() Syntax

SELECT AVG(YourColumnName) FROM YourTableName

Example

SELECT AVG(Salary) FROM Employee

SQL DELETE Query

DELETE query is delete all existing record from table with condition or without condition

DELETE Syntax

DELETE FROM YourTableName WHERE Condition

Example

1. Delete specific record form table
DELETE FROM City WHERE CityID = 1

1. Delete all record from table
DELETE FROM City

SQL UPDATE Query

UPDATE Statement is use for change values in existing record

UPDATE Syntax

UPDATE YourTableName SET Column1 = Value1 WHERE Condition for update record

Example

1. Single Record Update
- UPDATE City SET CityName = 'Test 2' WHERE ID = 1

2. Multiple Record Update
- UPDATE Student SET Standard = 2 WHERE Standard = 1

3. Update all record in table
- UPDATE Student SET Standard = 1

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