Friday 5 July 2013

Basic SQL Commands with Example



Select Command :

The SELECT statement is used to select data from a Table.

Insert Command :

The first form does not specify the column names where the data will be inserted, only their values:

INSERT INTO Employee
VALUES ('Ravi','21/04/1986','Male','21/04/2011');





The second form specifies both the column names and the values to be inserted:

INSERT INTO Employee (EmpName,Gender)
VALUES ('Bharath','Male');
select * from Employee



UPDATE COMMAND


The UPDATE statement is used to update existing records in a table.

UPDATE Employee
SET DOB='21/11/1989',DOJ='21/11/2012'
WHERE ID=7
select * from Employee


DELETE COMMAND

The DELETE statement is used to delete rows in a table.

DELETE FROM Employee
WHERE ID=6;
select * from Employee





WHERE COMMAND


The WHERE clause is used to extract only those records that fulfill a specified criterion.

SELECT * FROM Employee WHERE ID=7


DISTINCT COMMAND 

The DISTINCT keyword can be used to return only distinct (different) values.

SELECT DISTINCT EmpName,Gender,DOJ,DOB, ID
FROM Employee



AND & OR COMMAND

The AND operator displays a record if both the first condition AND the second condition are true.
The OR operator displays a record if either the first condition OR the second condition is true.

SELECT * FROM Employee WHERE EmpName='Aswani'
AND ID=3

SELECT * FROM Employee WHERE EmpName='Madhu'
OR EmpName='Lavanya';




ORDER BY COMMAND


The ORDER BY keyword is used to sort the result-set by one or more columns.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword.

SELECT EmpName,Gender FROM Employee
ORDER BY EmpName DESC

LIKE COMMAND


The LIKE operator is used to search for a specified pattern in a column.

SELECT EmpName,Gender,DOJ,DOB
FROM Employee
WHERE EmpName LIKE Bharath;



The following SQL statement selects all customers with a City starting with the letter "s":

The "%" sign is used to define wildcards (missing letters) both before and after the pattern. You will learn more about wildcards in the next chapter.

SELECT EmpName,Gender,DOJ,DOB
FROM Employee
WHERE EmpName LIKE 'L%';

SELECT EmpName,Gender,DOJ,DOB
FROM Employee
WHERE EmpName LIKE '%a';

SELECT EmpName,Gender,DOJ,DOB
FROM Employee
WHERE EmpName LIKE '%a%';








BETWEEN  COMMAND


The BETWEEN operator selects values within a range. The values can be numbers, text, or dates.

SELECT EmpName,Gender,DOJ,DOB
FROM Employee
WHERE ID BETWEEN 1 AND 7;





No comments:

Post a Comment