SQL Server Power Search - Get More Relevant Results

Basic SQL Commands

This article covers 4 basic SQL commands and explains how to use them. If you already know some SQL, you probably want info on more complicated commands than I have listed here. I am writing further tutorials, or you could look in Books Online or the MSDN.




SQL commands fall into one of 4 categories:

The examples assume a table called Customer with 3 columns: Id, FirstName and LastName. I'm trying to keep this simple so I won't go into any details about data types, primary keys, etc. This will be described/explained in a future article. For the purpose of the examples the Id column is numeric and the other 2 columns contain character data.

SQL SELECT

The basic syntax for the SELECT statement is:

SELECT Id, FirstName, LastName
FROM Customer
WHERE LastName = 'Fryar'

This example retrieves id, first and last names for all customers whose last name is "Fryar".

The list of columns can be replaced by a wildcard (*) to retrieve all columns from the table.
The conditional part of the statement can be extended so that rows matching several criteria are retrieved:

SELECT *
FROM Customer
WHERE LastName = 'Fryar'
AND FirstName = 'Richard'

SQL INSERT

The syntax for the second of my basic SQL commands is:

INSERT INTO Customer (Id, FirstName, LastName)
VALUES (100, 'John', 'Smith')

This could have been replaced with the following, which does exactly the same:

INSERT INTO Customer (Id, FirstName, LastName)
SELECT 100, 'John', 'Smith'

This can be combined with the SELECT statement to add multiple rows to the table. For example, you may want to copy all customers into another table if their Id is less than 500:

INSERT INTO CopyCustomer (Id, FirstName, LastName)
SELECT Id, FirstName, LastName
FROM Customer
WHERE Id < 500

SQL UPDATE

The basic syntax for the UPDATE statement is:

UPDATE Customer
SET LastName = 'Wilkins'
WHERE Id = 12345

This changes the last name of customer 12345.

SQL DELETE

The basic syntax for the DELETE statement is:

DELETE Customer
WHERE Id = 12345

This deletes customer 12345. Multiple customers could be deleted with the following:

DELETE Customer
WHERE LastName < 500

This deletes all customers if their Id is less than 500. OK, this is a contrived example, but it illustrates the power of the command.

This quick summary of basic SQL commands has been kept deliberately simple. All these commands have some very complicated variations, some of which I will be covering in future articles.

See the next article, SQL SELECT Query, which describes the SELECT statement in a little more detail.

If you were looking for syntax specific to Oracle, have a look at these Oracle Tutorials from AskTheOracle.net.

 
Not found what you're looking for?
Use this search box. It is tuned for SQL Server searches. Try it and see!



Do you have a question about SQL Server? Would you like to answer a question?

Go to the SQL FAQ to get started.