Write Basic Sql Statements in Sql Server

Many of us have used and worked with Databases one way or another. Often times, when a DBA or Database Programmer, are not available in companies, then it is up to you to get your hands dirty by writing SQL Statements. This tutorial, will walk you through writing Basic SQL Statements in retrieving and manipulating data. We shall be taking a look at 4 primary arenas



1) SELECT - This command is used to retrieve information from a table

2) INSERT - This command is used to add information to a table

3) UPDATE - This command is used to modify information to a table

4) DELETE - This command is used to remove information from a table

Steps

  1. Click on Start --> All Programs --> Microsoft SQL Server (2005/2008) --> SQL Server Management Studio
  2. Next Login with your credentials to the Server
  3. Now right click on the Northwind Database and choose New Query
  4. In the new Query Window, enter the following command for SELECT
  5. This is the syntax for SELECT -

    SELECT * FROM Employees
  6. This is the syntax for INSERT -

    INSERT INTO Employees VALUES('col1', 'col2') - Replace col1 and col2 with actual Values as shown below

    INSERT INTO Employees values('Anil','anil@company.com')
    This inserts a single row into the table.
    In the even you wish to enter multiple rows at one go, you see the following command

    INSERT INTO Employees values('Anna','anna@company.com'), INSERT INTO Employees values('Krystel','krystel@company.com'), INSERT INTO Employees values('Lines','lines@company.com'). The key difference here is that every value is appended by a comma
  7. This is the syntax for UPDATE -

    UPDATE Employees
    SET col1 = 'new value' WHERE col1 = 'old value' - Replace col1 with actual Values as shown below
  8. UPDATE Employees SET Name = 'Anil Mahadev' WHERE Name = 'Anil'
  9. This is the syntax for DELETE -

    DELETE FROM Employees
    WHERE col1 = 'value' WHERE value = 'actual data row' - Replace actual data row with actual Values as shown below
  10. DELETE FROM Employees WHERE Name = 'Anil Mahadev'
  11. This completes this short How-To, I hope this has been beneficial to you and like to Thank You for viewing it.

Tips

  • Use the SQL Server Management Studio Code Snippets as hints to better your SQL Writing Skills
  • As you become more comfortable writing Queries, Use the SQL Query Designer to build sophisticated queries

Warnings

  • Never use DELETE without a WHERE Clause in your Statements to prevent accidental deletion of rows
  • Same rule applies to UPDATE and INSERT as well
  • Always use Caution when working with the DELETE command.

You may like