December 8th, 2008 by Gabriel
Basic SQL Statements :
SQL is used to query the database which are almost same for SQL Server, MS Access, Oracle and MySQL.
There are four types of basic queries :-
SELECT
INSERT
UPDATE
DELETE
ORDER BY
Different combination of parameters can be passed to a query.
1] SELECT :-
its the most common SQL statement using which data can be selected from database and the output is returned to user.The result is
stored in a result table, called the result-set.SQL is not case sensitive. SELECT is the same as select.
Here is a simple example -
select name, bdate from user;
this will return all names and bdates from the table user.
select * from user;
this will return all records of user table.
select * from user where name=’john’;
this will return all records in which name = john
select * from user where name=’john’ OR name=’paul’ OR bdate=01012007
this way you can add multiple filtering using “OR” , “AND” clause.
2] INSERT :-
it is used to take the data input from user and insert it into table of the database. Data can be taken from any form or it can be
directly inserted using query.It is used to insert a new row in a table.
Simple example-
Insert into Table1 (FirstName, LastName, Phone) Values (’Gabriel’, ‘R’, ‘1112222′);
3] UPDATE :-
Using Update, we edit / modify any exisiting table. Either all the rows can be updated, or a subset may be chosen using a condition.
Simple example -
Update Table1 Set name=@FirstName, LastName=@LastName, Phone=@Phone where id=@ID
WHERE clause us very important in Update query. If you don’t use WHERE then it will update all existing records in the table.
4] DELETE :-
It is used to delete rows in a table. It also uses WHERE claue. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted.
Here is an example of a Delete statement-
Delete From Table1 where ID=10
it will delete all records in which ID = 10
5] Order By Clauses :-
It is used to sort the output returned by a query. Using this clause, you can sort by any field in the table.
Example -
Select * from Table1 Order By name
This will list all returned records sorted by Name alphabetically.
‘ASC’ (ascending order) or ‘DESC’ (descending order) can be included in Orderby query to sort the output in ascending or descending order respectively.
Like -
Select * from Table1 Order By name DESC