Know your SELECT, INSERT, UPDATE and DELETE Commands for SQL


People always use SQL commands (Select, Insert, Update and Delete), but have you think whether it is in appropriate way or not.
Here are the common points which you need to remember while writing the commands:
                SELECT Statements:
1.       While using SELECT statement you should always use only those columns  which you need to retrieve from database
2.       Always try to use ORDER BY or TOP (conditional) keywords  in your query
3.       Avoid using un-necessary fields in Select list
4.       Avoid using * to get all fields
5.       SELECT empId,  empName,  empAdd1, empAdd2, empCity, empZip FROM EmpMaster ORDER by empName  ASC –Correct Way
6.       SELECT * FROM EmpMaster  –Wrong Way


INSERT Statements:
1.       While using INSERT statement you should always pass only those columns which you need to supply the values into database not all which is having default values.
2.       Always try to use Insert into statements with fields
3.       Avoid using direct Insert statement because it creates ambiguity while inserting data in to columns
4.       INSERT INTO EmpMaster( empName,  empAdd1, empAdd2, empCity, empZip) VALUES (‘xyz’, ‘abc’, ‘xxx’, ‘abc’, ‘00000’) –Correct Way
5.       INSERT  EmpMaster  VALUES (‘xyz’, ‘abc’, ‘xxx’, ‘abc’, ‘00000’) –Wrong Way


UPDATE Statements:
1.       While using UPDATE statement you should always use WHERE clause on unique columns
2.       Always  recheck before executing the Update commands
3.       UPDATE EmpMaster SET empName = ‘xyz’,  empAdd1 = ‘abc’ WHERE empID = 2  –Correct Way
4.       UPDATE EmpMaster SET empName = ‘xyz’,  empAdd1 = ‘abc’ WHERE empName = ‘abc’ –Wrong Way

DELETE Statements:
1.       While using DELETE statement you should always use WHERE clause on unique columns
2.       Always  recheck before executing the DELETE commands
3.       DELETE EmpMaster WHERE empID = 2  –Correct Way
4.       DELETE EmpMaster WHERE empName = ‘abc’ –Wrong Way

Comments